程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> 用C做的電子時鐘程序

用C做的電子時鐘程序

編輯:關於C語言

效果圖

\


/*-----------------------------電子時鐘源代碼---------------------------------*/

/*------------------------------預處理----------------------------------------*/
#include<graphics.h>
#include<math.h>
#include<dos.h>
#include<stdio.h>
/*-----------------------------宏定義-----------------------------------------*/
#define PI   3.1415926
#define Up   0x4800
#define Down 0x5000
#define Tab  0xf09
#define Esc  0x11b
/*-----------------------------定義全局變量-----------------------------------*/
 double Hour,Minute,Second;
 int Radius=130,Center_x=300,Center_y=265;
 double Hour_x,Hour_y,Minute_x,Minute_y,Second_x,Second_y;
 struct time Time[1];
/*-----------------------------函數的聲明-------------------------------------*/
void ColockHandle();
int  DigitalHour(double Hour);
int  DigitalMinute(double Minute);
int  DigitalSecond(double Second);
void DigitalClock(int x,int y,int clock);
int Up_Key(int Count);
int Down_Key(int Count);
void ClearCursor(int Count);
void DrawCursor(int Count);
/*----------------------------主函數------------------------------------------*/
main()
{
/*--------------繪底板----------------------*/
 int Driver,Mode;
 int i,j;
 Driver=DETECT;
 initgraph(&Driver,&Mode,"");
 setcolor(RED);
 line(300,0,5,120);
 line(300,0,595,120);
 line(5,120,595,120);
 setfillstyle(SOLID_FILL,RED);
 floodfill(300,100,RED);
 setcolor(BLUE);
 line(65,120,535,120);
 line(65,120,65,470);
 line(535,120,535,470);
 line(65,470,535,470);
 line(125,120,125,470);
 line(475,120,475,470);
 line(125,410,475,410);
 setfillstyle(SOLID_FILL,BLUE);
 floodfill(80,130,BLUE);
 setfillstyle(SOLID_FILL,BLUE);
 floodfill(500,130,BLUE);
 setfillstyle(SOLID_FILL,BLACK);
 floodfill(300,450,BLUE);
/*-------------繪時間間隔符-----------------*/
 for(i=275;i<=305;i=i+30)
    {
      for(j=434;j<=442;j=j+8)
         {
           setcolor(WHITE);
           setlinestyle(0,0,3);
           circle(i,j,1);
         }
    }
/*------------繪制光標----------------------*/
 setcolor(WHITE);
 line(270,430,270,445);
/*-------------繪制表軸---------------------*/
 setlinestyle(0,0,3);
 circle(Center_x,Center_y,1);
/*-------------繪制表框---------------------*/
 setlinestyle(0,0,1);
 circle(Center_x,Center_y,Radius+3);
/*--------------繪制分針刻度----------------*/
 for(i=0,Minute=0;i<=59;i++,Minute++)
    {
      Minute_x=Center_x+Radius*sin(Minute/30*PI);
      Minute_y=Center_y-Radius*cos(Minute/30*PI);
      setlinestyle(0,0,1);
      circle(Minute_x,Minute_y,1);
    }
/*--------------繪制時針刻度----------------*/
 setcolor(RED);
 for(i=0,Hour=0,Minute=0;i<=11;i++,Hour++)
    {
      Hour_x=Center_x+Radius*sin((Hour*60+Minute)/360*PI);
      Hour_y=Center_y-Radius*cos((Hour*60+Minute)/360*PI);
      setlinestyle(0,0,3);
      circle(Hour_x,Hour_y,1);


    }
/*--------------添加文本--------------------*/
 setcolor(GREEN);
 outtextxy(283,50,"CLOCK");
 outtextxy(320,110,"Designed by Zhang Cheng Jin");
 outtextxy(80,130,"Help");
 outtextxy(70,150,"Up:");
 outtextxy(78,168,"Time++");
 outtextxy(70,216,"Down:");
 outtextxy(78,234,"Time--");
 outtextxy(70,286,"Tab:");
 outtextxy(78,304,"Move");
 outtextxy(70,356,"Esc:");
 outtextxy(78,374,"Exit");
 outtextxy(500,190,"No");
 outtextxy(490,215,"time");
 outtextxy(490,240,"like");
 outtextxy(495,265,"the");
 outtextxy(480,290,"present");
 outtextxy(500,315,"!");
/*----------調用函數-----------------------*/
 ColockHandle();
/*-----------關閉圖形----------------------*/
 closegraph();
/*-----------返回0,程序結束---------------*/
 return 0;
}


/*---------------------------時鐘動畫處理函數---------------------------------*/
void ColockHandle()
{
  int Key=0,Count;
  setcolor(WHITE);
/*----------------獲取系統時間-------------*/
  gettime(Time);
  Hour=Time[0].ti_hour;
  Minute=Time[0].ti_min;
/*----------------繪制分針-----------------*/
  Minute_x=Center_x+100*sin(Minute/30*PI);
  Minute_y=Center_y-100*cos(Minute/30*PI);
  line(Center_x,Center_y,Minute_x,Minute_y);
/*----------------繪制時針-----------------*/
  Hour_x=Center_x+80*sin((Hour*60+Minute)/360*PI);
  Hour_y=Center_y-80*cos((Hour*60+Minute)/360*PI);
  line(Center_x,Center_y,Hour_x,Hour_y);
/*----------------繪制數字時鐘-------------*/
  DigitalClock(255,435,DigitalHour(Hour));
  DigitalClock(285,435,DigitalMinute(Minute));


  setwritemode(1);
  for(Count=2;Key!=Esc;)
       {
/*----------------繪制秒針-----------------*/
         setcolor(RED);
         Hour=Time[0].ti_hour;
         Minute=Time[0].ti_min;
         Second=Time[0].ti_sec;


         Second_x=Center_x+120*sin(Second/30*PI);
         Second_y=Center_y-120*cos(Second/30*PI);
         line(Center_x,Center_y,Second_x,Second_y);
/*-----------------------------------------*/
         while(Hour==Time[0].ti_hour&&Minute==Time[0].ti_min&&Second==Time[0].ti_sec)
              {
                gettime(Time);
                if(bioskey(1)!=0)
                  {
                    Key=bioskey(0);
                    Count=KeyHandle(Key,Count);
                    if(Count==5)
                      Count=1;
                  }
              }
/*----------繪制數字時鐘的秒---------------*/
         setcolor(WHITE);
         DigitalClock(315,435,DigitalSecond(Second)+1);
/*----------清除前一個秒針的圖形-----------*/
         setcolor(RED);
         Second_x=Center_x+120*sin(Second/30*PI);
         Second_y=Center_y-120*cos(Second/30*PI);
         line(Center_x,Center_y,Second_x,Second_y);


         setcolor(WHITE);
/*---------對分針變化的處理----------------*/
         if(Minute!=Time[0].ti_min)
           {
             Minute_x=Center_x+100*sin(Minute/30*PI);
             Minute_y=Center_y-100*cos(Minute/30*PI);
             line(Center_x,Center_y,Minute_x,Minute_y);


             Minute=Time[0].ti_min;
             DigitalClock(285,435,DigitalMinute(Minute));
             Minute_x=Center_x+100*sin(Minute/30*PI);
             Minute_y=Center_y-100*cos(Minute/30*PI);
             line(Center_x,Center_y,Minute_x,Minute_y);


           }
/*----------對時針變化的處理 www.2cto.com ---------------*/
         if(Hour!=Time[0].ti_hour)
           {
             Hour_x=Center_x+80*sin((Hour*60+Minute)/360*PI);
             Hour_y=Center_y-80*cos((Hour*60+Minute)/360*PI);
             line(Center_x,Center_y,Hour_x,Hour_y);


             Hour=Time[0].ti_hour;
             DigitalClock(255,435,DigitalHour(Hour));
             Hour_x=Center_x+80*sin((Hour*60+Minute)/360*PI);
             Hour_y=Center_y-80*cos((Hour*60+Minute)/360*PI);
             line(Center_x,Center_y,Hour_x,Hour_y);
           }
       }
}
/*------------------------數字時鐘模塊----------------------------------------*/
int DigitalHour(double Hour)
{
  int h;
  for(h=0;h<24;h++)
     {
       if(h==Hour)
         {
           return h;
         }
     }
}


int DigitalMinute(double Minute)
{
  int m;
  for(m=0;m<60;m++)
     {
       if(m==Minute)
         {
           return m;
         }
     }
}


int DigitalSecond(double Second)
{
  int s;
  for(s=0;s<60;s++)
     {
       if(s==Second)
         {
           return s;
         }
     }
}


void DigitalClock(int x,int y,int clock)
{


  char buffer[10];
  setfillstyle(0,2);
  bar(x,y,x+14,449);
  if(clock==60)
    clock=0;
  sprintf(buffer,"%d",clock);
  outtextxy(x,y,buffer);
}
/*----------------------按鍵處理函數------------------------------------------*/
int KeyHandle(int Key,int Count)
{
  switch(Key)
        {
          case   Up: Up_Key(Count-1);
                     break;
          case Down: Down_Key(Count-1);
                     break;
          case  Tab: ClearCursor(Count);
                     DrawCursor(Count);
                     Count++;
                     break;
        }
  return Count;
}
/*--------------------向上方向鍵函數------------------------------------------*/
int Up_Key(int Count)
{
  if(Count==1)
    {
      Time[0].ti_hour--;
      if(Time[0].ti_hour==24)
        Time[0].ti_hour=0;
      settime(Time);
    }


  if(Count==2)
    {
      Time[0].ti_min--;
      if(Time[0].ti_min==60)
        Time[0].ti_min=0;
      settime(Time);
    }


  if(Count==3)
    {
      Time[0].ti_sec--;
      if(Time[0].ti_sec==60)
        Time[0].ti_sec=0;
      settime(Time);
    }
}
/*--------------------向下方向鍵函數------------------------------------------*/
int Down_Key(int Count)
{
  if(Count==1)
    {
      Time[0].ti_hour++;
      if(Time[0].ti_hour==24)
        Time[0].ti_hour=0;
      settime(Time);
    }


  if(Count==2)
    {
      Time[0].ti_min++;
      if(Time[0].ti_min==60)
        Time[0].ti_min=0;
      settime(Time);
    }


  if(Count==3)
    {
      Time[0].ti_sec++;
      if(Time[0].ti_sec==60)
        Time[0].ti_sec=0;
      settime(Time);
    }
}
/*---------------------光標處理函數-------------------------------------------*/
void ClearCursor(int Count)
{ setcolor(WHITE);
  switch(Count)
        {
          case 2: line(270,430,270,445);
                  break;
          case 3: line(300,430,300,445);
                  break;
          case 1: line(330,430,330,445);
                  break;
        }
}


void DrawCursor(int Count)
{
  switch(Count)
        {
          case 1: line(270,430,270,445);
                  break;
          case 2: line(300,430,300,445);
                  break;
          case 3: line(330,430,330,445);
                  break;
        }
}
/*----------------------------------------------------------------------------*/

 

摘自 wyzhangchengjin123

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved