用c++的话还是弄成类比较好的,下面是我的解决方法:
#include <ctime>
#include <cstring>
class Timer
{
private:
static const int size = 50;
std::time_t time;
std::tm localTime;
char Ascll[size];
char flag;
public:
Timer()
{
std::time(&time);
flag = 0;
}
~Timer() {}
const char printTime()
{
if(!flag)
{
localTime = std::localtime(&time);
std::strcpy(Ascll, std::asctime(&localTime));
}
flag++;
return Ascll;
}
double timeDiff(Timer time2) const
{
return difftime(time, time2 -> time);
}
} ;
int main()
{
Timer t1;
for(int i = 0; i < 10000; i++ )
{
cout << i;
}
Timer t2;
std::cout << t1printTime() << std::endl;
std::cout << t2printTime() << std::endl;
std::cout << t2timeDiff(&t1) << std::endl;
return 0;
}
#include"graphicsh"
#define PI 31416
#include"mathh"
#include"dosh"
main()
{
int x0=320,y0=240,r0=150;
void init_sceen();
void sec();
init_sceen(x0,y0,r0);
sec();
closegraph();
}
void init_sceen(int x0,int y0,int r0)//
{
int i,x,y,graphdriver,graphmode;
char s[10];
float alpha,a0=90;
graphdriver=DETECT;
initgraph(&graphdriver,&graphmode,"");
setbkcolor(3);
setcolor(2);
circle(x0,y0,r0);
circle(x0,y0,r0+30);
setfillstyle(SOLID_FILL,10);
floodfill(x0-r0-10,y0,2);
/please input the time/
for(i=12;i>=1;i--)
{
alpha=(a0+30(11-i)PI/180);
x=x0+cos(alpha)r0-16;
y=y0-sin(alpha)r0;
sprintf(s,"%2d",i);
setcolor(4);
settextstyle(0,0,2);
outtextxy(x,y,s);
}
/input second/
for(i=60;i>=1;i--)
{
alpha=(a0+6(60-i)PI/180);
x=x0+cos(alpha)(r0-20);
y=y0-sin(alpha)(r0-20);
setcolor(14);
if(i%5==0)
circle(x,y,5);
else circle(x,y,2);
floodfill(x,y,14);
}
setlinestyle(0,0,3);
}
void sec(void) //
{
int x,y,i,j,k,xj,yj,xk,yk,xi,yi,x0=320,y0=240,r0=150;
union REGS r;
unsigned char shijie="";
unsigned char daa="";
struct time tim;
struct date dat;
float alphai,alphak,alphaj,a0=90;
xi=x0;yi=y0;xj=x0;yj=y0;xk=x0;yk=y0;
do
{
/intput the time/
x=38;y=12;
gettime(&tim);
sprintf(shijie,"%02d:%02d:%02d",timti_hour,timti_min,timti_sec);
setfillstyle(SOLID_FILL,0);
bar(245,190,375,210);
setcolor(15);
outtextxy(245,190,shijie);
/input the date/ //
getdate(&dat);
sprintf(daa,"%02d--%02d--%02d",datda_year,datda_mon,datda_day);
/setfillstyle(SOLID_FILL,3);/
bar(225,290,395,310);
setcolor(RED);
outtextxy(225,290,daa);
x=190;y=430;
setcolor(RED);
outtextxy(x-26,y,"Designed by GuoLiuTa0");
setcolor(LIGHTRED);
outtextxy(x+76,y0+20,"NBA GAME");
setlinestyle(0,0,3);
k=timti_hour;
j=timti_min;
i=timti_sec;
alphak=(a0+30(12-k)-j5/606)PI/180;
alphaj=(a0-6j)PI/180;
/write second hand/
alphai=(a0+6(60-i))PI/180;
x=x0+cos(alphai)(r0-32);
y=y0-sin(alphai)(r0-32);
setcolor(BLACK);
line(x0,y0,xi,yi);
setcolor(YELLOW);
line(x0,y0,x,y);
xi=x;
yi=y;
/write minute hand/
x=x0+cos(alphaj)(r0-60);
y=y0-sin(alphaj)(r0-60);
setcolor(BLACK);
line(x0,y0,xj,yj);
setcolor(BLUE);
line(x0,y0,x,y);
xj=x;
yj=y;
/write hour hand/
x=x0+cos(alphak)(r0-99);
y=y0-sin(alphak)(r0-99);
setcolor(BLACK);
line(x0,y0,xk,yk);
setcolor(RED);
line(x0,y0,x,y);
xk=x;
yk=y;
delay(10000);
}
while(!kbhit());
}
这个是简单的数码管时钟显示如果有需要,我写过比较完善的1602显示时钟显示控制年月日等等#include\x0d\sbit Begin=P2^0;\x0d\sbit Hour=P2^1;\x0d\sbit Mus=P2^2;\x0d\sbit End=P2^3;\x0d\unsigned char code Tab[]={ 0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,\x0d\ 0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0x40};\x0d\unsigned char code num[]={ 0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};\x0d\unsigned char Time[]={0,0,16,0,0,16,0,0};\x0d\unsigned char a;\x0d\unsigned int x,m,th;\x0d\void init()\x0d\{\x0d\ TMOD=0x01;\x0d\ TH0=(65535/50000)/256;\x0d\ TL0=(65535/50000)%256;\x0d\ EA=1;\x0d\ ET0=1;\x0d\ TR0=1;\x0d\}\x0d\void delay(unsigned int z) \x0d\{ \x0d\ unsigned int x,y;\x0d\ for(x=z;x>0;x--)\x0d\ for(y=110;y>0;y--); \x0d\}\x0d\unsigned char keyboard()\x0d\{\x0d\ if(Begin==0){\x0d\ delay(5);\x0d\ if(Begin==0)\x0d\ return 1;\x0d\ }\x0d\ if(Hour==0){\x0d\ delay(30);\x0d\ if(Hour==0)\x0d\ return 2;\x0d\ }\x0d\ if(Mus==0)\x0d\ return 3;\x0d\ if(End==0)\x0d\ return 4;\x0d\}\x0d\void display()\x0d\{\x0d\ unsigned char i; for(i=0;i=20)\x0d\ {\x0d\ x++;\x0d\ m=x;\x0d\ th=m/3600; //设置小时\x0d\ Time[0]=th/10;\x0d\ Time[1]=th%10;\x0d\ m=m%3600; Time[2]=16; th=m/60; //设置分钟\x0d\ Time[3]=th/10;\x0d\ Time[4]=th%10;\x0d\ m=m%60; Time[5]=16; th=m; //设置秒\x0d\ Time[6]=th/10;\x0d\ Time[7]=th%10; a=0;\x0d\ }\x0d\}
/ Note:Your choice is C IDE /
#include "stdioh"
#include "graphicsh"
#include "mathh"
#include "timeh"
#include "dosh"
#include "stdlibh"
#include "conioh"
#define R 200
#define PI 31415926
#define X(a,b,c) x=acos(bcPI/180-PI/2)+300
#define Y(a,b,c) y=asin(bcPI/180-PI/2)+240
#define d(a,b,c) X(a,b,c);Y(a,b,c);line(300,240,x,y)
void init(void);
void draw_bp(void);
void close(void);
void draw_kt(void);
int main(void)
{
int x,y,hc=0;
int bx,by,ax,ay;
int h,m,s;
struct time t[1];
init();
draw_bp();
draw_kt();
setwritemode(1);
gettime(t);
h=t[0]ti_hour;
m=t[0]ti_min;
s=t[0]ti_sec;
setcolor(7);
d(150,h,30);
setcolor(14);
d(170,h,6);
setcolor(4);
d(190,s,6);
while(!kbhit())
{
while(t[0]ti_sec==s)
gettime(t);
sound(400);
delay(70);
sound(200);
delay(30);
nosound();
setcolor(4);
d(190,s,6);
s=t[0]ti_sec;
setfillstyle(SOLID_FILL,13);
floodfill(1,380,14);
setcolor(4);
d(190,s,6);
if(t[0]ti_min!=m)
{
setcolor(14);
d(170,m,6);
m=t[0]ti_min;
d(170,m,6);
}
if(t[0]ti_hour!=h)
{
setcolor(7);
d(150,h,30);
h=t[0]ti_hour;
d(150,h,30);
sound(1000);
delay(240);
nosound();
delay(140);
sound(2000);
delay(240);
nosound();
}
setcolor(hc++);
if(hc==12) hc=0;
ellipse(300,300,160,340,23,23);
ellipse(220,170,0,360,23,65);
ellipse(380,170,0,360,23,65);
ellipse(300,180,0,180,23,23);
ellipse(380,180,0,180,23,23);
arc(150,80,20,250,59);
}
close();
return 0;
}
void init(void)
{
int driver,mode;
driver=DETECT;
initgraph(&driver,&mode,"c:\\tc");
cleardevice();
}
void draw_bp(void)
{
int x=300,y=240,i,n,l,ax,ay,bx,by;
for(n=0;n<27;n++)
printf("\n");
for(n=0;n<29;n++)
printf(" ");
setbkcolor(0);
setcolor(14);
circle(x,y,R);
setcolor(12);
circle(x,y,190);
setfillstyle(SOLID_FILL,14);
floodfill(x,y,12);
setfillstyle(SOLID_FILL,13);
floodfill(1,1,14);
setcolor(2);
circle(x,y,2);
circle(x,y,5);
for(i=0;i<60;i++)
{
if(i%5==0)
l=15;
else
l=5;
ax=200cos(i6PI/180)+300;
ay=200sin(i6PI/180)+240;
bx=(200-l)cos(i6PI/180)+300;
by=(200-l)sin(i6PI/180)+240;
line(ax,ay,bx,by);
}
settextstyle(3,0,5);
outtextxy(260,380,"MADEIN CHINA");
}
void draw_kt(void)
{
int x=300,y=240;
ellipse(x-80,y-70,0,360,23,65);
ellipse(x+80,y-70,0,360,23,65);
ellipse(x-80,y-60,0,180,23,23);
setfillstyle(SOLID_FILL,0);
floodfill(x-80,y-60,2);
ellipse(x+80,y-60,0,180,23,23);
setfillstyle(SOLID_FILL,0);
floodfill(x+80,y-70,2);
setcolor(5);
circle(150,80,60);
setfillstyle(SOLID_FILL,14);
floodfill(150,80,5);
circle(450,80,60);
setfillstyle(SOLID_FILL,14);
floodfill(450,80,5);
setcolor(14);
arc(150,80,0,360,60);
arc(450,80,0,360,60);
setcolor(0);
ellipse(x,y+60,160,340,23,23);
circle(x+120,y+10,23);
setfillstyle(SOLID_FILL,12);
floodfill(x+120,y+10,0);
setcolor(14);
circle(x+120,y+10,23);
setcolor(0);
circle(x-120,y+10,23);
setfillstyle(SOLID_FILL,12);
floodfill(x-120,y+10,0);
setcolor(14);
circle(x-120,y+10,23);
ellipse(x,y+60,0,180,23,23);
}
void close(void)
{
getch();
closegraph();
}
/源程序默认硬件环境:52单片机,12MHz晶振,四位共阳数码管,P0段选,P20-P23低电平位选,P23最高位,P20最低位/#include"reg52h"//包含52头文件#defineTRUE1//定义布尔量'1':真#defineFALSE0//定义布尔量'0':假#defineucharunsignedchar//定义无符号字符型数据简称#defineuintunsignedint//定义无符号整型数据简称#defineth00x3c#definetl00xb0//50msat12MHz(定时器工作模式1状态)#defineth10xfc#definetl10x18//1msat12MHz(定时器工作模式1状态)#defineT1sAt50msCount20//定义1s在50ms计时基准状态下的计数值为20#defineSEG_Num4#defineSEG_DataP0//数码管段驱动接口#defineSEG_EnP2//数码管位驱动接口#defineSEG_AllOff(SEG_En|=0x0f)//关闭所有数码管(位驱动)#defineDisTimeAt1msCount4//单'位'数码管显示时间,数码管刷新频率f=1/(Nt),其中N为数码管位数,t为单'位'数码管显示时间ucharSEG_B_List[10]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};//共阳数码管代码表"0-9"ucharSec,Min;ucharbdataFlag=2;sbitT1sTimesUpFlag=Flag^0;sbitDisplayFlag=Flag^1;voidTimer0()interrupt1//定时器0中断函数{staticuchart50ms;TL0=tl0;TH0=th0;t50ms=++t50ms%T1sAt50msCount;//先对50ms计时变量加1,后对变量范围进行限制(0~19)(即对20取模)if(!t50ms)T1sTimesUpFlag=TRUE;//若计时变量归0,表示计时变量曾经到达20(1s),则对1s计时标志位置位}voidTimer1()interrupt3{staticuchart1ms;TL1=tl1;TH1=th1;t1ms=++t1ms%DisTimeAt1msCount;//先计数值加1,后对计数范围进行限制0~(DisTimeAt1msCount-1)if(!t1ms)DisplayFlag=TRUE;//若定时计数值归0,则表示计数值曾到达单'位'显示时间(DisTimeAt1msCount),显示标志置位}voidTimerInit(){TMOD=0x11;//开启定时器0、定时器1,并都工作在模式1TH0=th0;TL0=tl0;TR0=1;//启动T0定时器计时ET0=1;//允许定时器0中断TH1=th1;TL1=tl1;TR1=1;//启动T1定时器计时ET1=1;//允许定时器1中断EA=1;//开启系统中断功能}voidTimesUpdata()//时间更新函数{if(T1sTimesUpFlag)//若1s计时标志位为真,即1s定时时间到{Sec=++Sec%60;//秒Sec在0-59范围内加1if(!Sec)Min=++Min%60;//若秒Sec重归0,则分Min在0-59范围内加1T1sTimesUpFlag=FALSE;//清1s计时标志位}}floatPow_Self(floatx,uinty)//自编简易x的y次方函数,y只能是非负整数{floatsum;if(x==0&&y==0)return;//0的0次方无意义elseif(x==0)sum=0;//可有可无,y!=0的情况已经包含x=0,不加不影响结果,但影响运算速度elseif(y==0)sum=1;//除上述情况外,任何数的0次方均为1elseif(y==1)sum=x;//任何数的1次方均为本身elseif(y>1)sum=Pow_Self(x,--y)x;//递归调用,降幂returnsum;//返回计算结果}voidDisplay(uintdis_num)//显示函数,显示内容为无符号整型数据dis_num{staticdis_loca;//定义静态变量显示位置dis_loca=++dis_loca%SEG_Num;//先对显示位置加1,后对变量范围进行限制0~(SEG_Num-1)SEG_AllOff;//关闭所有数码管显示(位驱动)SEG_Data=SEG_B_List[(dis_num/(uint)(Pow_Self(10,dis_loca)))%10];//将显示内容(dis_num)本次需显示的位(dis_loca)上的数值转成代码,并送到数据端口if(dis_loca==2)SEG_Data&=0x7f;//显示小数点,用于区分Min跟SecSEG_En&=~(1<
可以使用C语言标准库中的timeh头文件中的clock()函数来获取程序运行时间。具体的方法如下:
在程序开始运行时,调用clock()函数,获取当前系统时间,并将结果保存在一个变量中,如start_time。
程序执行完毕后,再次调用clock()函数,获取当前系统时间,并将结果保存在另一个变量中,如end_time。
计算程序运行时间,可以通过end_time - start_time来得到程序运行时间的时钟数,再除以CLOCKS_PER_SEC,即每秒钟的时钟数,得到程序运行时间的秒数。
代码示例:
#include <stdioh>
#include <timeh>
int main() {
int n = 16; // 盘子数量
int A = 1, B = 2, C = 3; // 三个柱子的编号
clock_t start_time, end_time;
start_time = clock(); // 记录开始时间
// 执行汉诺塔 *** 作
//
end_time = clock(); // 记录结束时间
double duration = (double)(end_time - start_time) / CLOCKS_PER_SEC; // 计算运行时间
printf("程序运行时间为 %2f 秒\n", duration);
return 0;
}
以上就是关于c++时钟程序代码全部的内容,包括:c++时钟程序代码、用"C语言"编写一个指针型时钟模拟的程序、单片机C语言该怎么编写时钟程序等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)