用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);
}
/*----------对时针变化的处理---------------*/
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;
}
}
/*----------------------------------------------------------------------------*/