当前位置:编程学习 > 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);
           &n

补充:软件开发 , C语言 ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,