天天看點

C語言鐘表【改進版】

改進版源代碼:

#include<stdio.h>

#include<graphics.h>

#include<math.h>

#include<dos.h>

#define PI 3.1415926

#define x 320

#define y 240

int main()

{

 int gdriver = DETECT,gmode,i,l;

 float th_hour,th_min,th_sec,m,n,x1,y1,x2,y2;

 struct time curtime;

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

 while(! kbhit())

 {

  for(i=0;i<60;i++)

      {

   if(i%5==0)

   {

    l=15 ;

    setcolor(13);

   }

   else

    l=5;

          x1=200*cos(i*6*PI/180)+320;

          y1=200*sin(i*6*PI/180)+240;

         x2=(200-l)*cos(i*6*PI/180)+320;

          y2=(200-l)*sin(i*6*PI/180)+240;

   line(x1,y1,x2,y2);

    switch(i)

    {

     case 0 :

      outtextxy(x2-9,y2-2,"3");

      break;

     case 5 :

      outtextxy(x2-10,y2-6,"4");

     case 10 :

      outtextxy(x2-7,y2-9,"5");

     case 15 :

      outtextxy(x2-2,y2-10,"6");

     case 20 :

      outtextxy(x2-1,y2-10,"7");

     case 25 :

      outtextxy(x2+3,y2-7,"8");

     case 30 :

      outtextxy(x2+4,y2-2,"9");

     case 35 :

      outtextxy(x2+2,y2+1,"10");

     case 40 :

      outtextxy(x2+1,y2+3,"11");

     case 45 :

      outtextxy(x2-7,y2+4,"12");

     case 50 :

      outtextxy(x2-5,y2+3,"1");

     case 55 :

      outtextxy(x2-9,y2,"2");

    }

   setcolor(9);

     }

  gettime(&curtime);

  /*得到目前系統時間*/

  gotoxy(40,18);      /*定位輸出位置*/

  setcolor(7);

  outtextxy(getmaxx()/2-30,getmaxy()/4,"UESTC_TerryLi");

  setcolor(2);

  rectangle(0,0,getmaxx(),getmaxy());

  rectangle(20,20,getmaxx()-20,getmaxy()-20);

  line(0,0,20,20);

  line(getmaxx(),0,getmaxx()-20,20);

  line(0,getmaxy(),20,getmaxy()-20);

  line(getmaxx(),getmaxy(),getmaxx()-20,getmaxy()-20);

  printf("Now Time:");

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

      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);

  /*獲得三個

  圓*/

  setcolor(11);

    circle(x,y,200);

  circle(x,y,12);

  setcolor(4);         /*the first  */

  circle(x,y,3);       /*the second */

  setfillstyle(1,4);   /*the third  */

  floodfill(x,y,4);    /*fouth      */ /*si bu chan sheng zhong jian de hong yuan xin*/

  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 */

  /*draw hour point*/

  m = x + 90*sin(th_hour);     /*70*/

  n = y - 90*cos(th_hour);

  setcolor(13);

  line(x,y,m,n);

  /*draw minute point*/        /*110*/

  m = x + 130*sin(th_min);

  n = y - 130*cos(th_min);

  /*draw second point*/         /*140*/

  m = x + 170*sin(th_sec);

  n = y - 170*cos(th_sec);

  setcolor(5);

  sleep(1);

  cleardevice();

 }

 closegraph();

 return 0;

}

本文轉自terryli51CTO部落格,原文連結: http://blog.51cto.com/terryli/520624,如需轉載請自行聯系原作者

繼續閱讀