用C做的电子时钟程序
/*-----------------------------电子时钟源代码---------------------------------*/
/*------------------------------预处理----------------------------------------*/
#include<graphics.h>
#include<math.h>
#include<.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
上一篇: 复盘与回顾:移动VR内容创业者的这一年
下一篇: 为何互联网企业主选“996加班”制度?