龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > 软件开发 > C/C++开发 >

一个简单易懂的时钟程序

时间:2009-12-22 15:42来源:未知 作者:admin 点击:
分享到:
/***********简单的时钟程序,界面不是很美观,您可以根据自己的爱好加以修改,如给表盘加上刻度,将指针改为其它外形等*/ #include #include #include #define PI 3.1415926 #define x0 320 /*定义钟表

/***********简单的时钟程序,界面不是很美观,您可以根据自己的爱好加以修改,如给表盘加上刻度,将指针改为其它外形等*/

#include

  #include

  #include

#define PI 3.1415926

  #define x0 320 /*定义钟表中心坐标*/

  #define y0 240

void DrawClock(int x,int y,int color) /*画表盘*/

  { int r=150; /*表盘的半径*/

   float th;

   setcolor(color);

   circle(x,y,r);

   circle(x,y,2);

  }

void DrawHand(int x,int y,float th,int l,int color)

  {

   int x1,y1;

   x1=x+l*sin(th);

   y1=y-l*cos(th);

   setcolor(color);

   line(x,y,x1,y1);

  }

void main()

  {int gdriver=DETECT,gmode;

   strUCt time curtime;

   float th_hour,th_min,th_sec;

   initgraph(&gdriver,&gmode,"");

setbkcolor(0);

while(! kbhit())

   {

   DrawClock(x0,y0,14);

   gettime(&curtime); /*得到当前系统时间*/

gotoxy(35,20); /*定位输出位置*/

   if((float)curtime.ti_hour<=12) /*午前的处理*/

   {printf("AM ");

   if((float)curtime.ti_hour<10) printf("0"); /*十点之前在小时数前加零*/

   printf("%.0f:",(float)curtime.ti_hour);

   }

   else /*午后的处理*/

   {printf("PM ");

   if((float)curtime.ti_hour-12<10) printf("0");

   printf("%.0f:",(float)curtime.ti_hour-12);

   }

   if((float)curtime.ti_min<10) printf("0");

   printf("%.0f:",(float)curtime.ti_min);

   if((float)curtime.ti_sec<10) printf("0");

   printf("%.0f",(float)curtime.ti_sec);

/*以下三行计算表针转动角度,以竖直向上为起点,顺时针为正*/

th_sec=(float)curtime.ti_sec*0.1047197551; /*2π/60=0.1047197551*/

   th_min=(float)curtime.ti_min*0.1047197551+th_sec/60.0;

   th_hour=(float)curtime.ti_hour*0.523598775+th_min/12.0; /* 2π/12=0.5235987755 */

   DrawHand(x0,y0,th_hour,70,2); /*画时针*/

   DrawHand(x0,y0,th_min,110,3); /*分针*/

   DrawHand(x0,y0,th_sec,140,12); /*秒针*/

  

sleep(1); /*延时一秒后刷新*/

   cleardevice();

   }

closegraph();

  }

点这里下载

  

精彩图集

赞助商链接