/********************************************************
这个程序是显示时钟的程序,电子钟和石英钟都有显示。
********************************************************/
/********************************************************
头文件
********************************************************/
#include<stdio.h>
#include<graphics.h>
#include<dos.h>
#include<conio.h>
#include<math.h>
#include<time.h>
#define pi 3.141592
/********************************************************
全局变量
********************************************************/
int hcolor=9;/*时针颜色*/
int mcolor=6;/*分针颜色*/
int scolor=4;/*秒针颜色*/
/********************************************************
函数声明
*********************************************************/
void bellface();/*钟表界面*/
void hour(int);/*画时针*/
void min(int);/*画分针*/
void sec(int);/*画秒针*/
void eclock();/*显示电子时钟*/
/*主函数*/
main()
{
char *buf;
long size;
struct time tim;
int angleHou,angleMin,angleSec;/*时针,分针,秒针的角度*/
int driver,mode;
driver=VGA;
mode=VGAHI;
registerbgidriver(EGAVGA_driver);
initgraph(&driver,&mode,"");
cleardevice();
bellface();/*钟表界面*/
size=imagesize(220,140,420,340);/*截取屏幕图像*/
buf=(char *)malloc(size);
getimage(220,140,420,340,buf);
for(;;)
{
gettime(&tim);
/*下面三行是核心代码,每一个小格6度*/
angleMin=(tim.ti_min%60)*6+180;/*计算当前分针的角度*/
angleHou=(tim.ti_hour%12)*30+180+angleMin/24;/*计算当前时针的角度*/
angleSec=(tim.ti_sec%60)*6+180;/*计算当前秒针的角度*/
putimage(220,140,buf,COPY_PUT);
eclock();/*显示电子时钟*/
hour(angleHou);/*画时针*/
min(angleMin);/*画分针*/
sec(angleSec);/*画秒针*/
eclock();/*显示电子时钟*/
}
}
void bellface()
{
int i;
int x,y;
char text[3];
setbkcolor(0);
setcolor(2);
rectangle(120,40,520,440);
rectangle(5,5,630,470);
line(5,40,120,40);
line(5,440,120,440);
line(520,40,630,40);
line(520,440,630,440);
setcolor(2);
setfillstyle(7,2);
circle(320,240,100);/*表的外圆*/
floodfill(210,240,2);
for(i=1;i<=12;i++)/*显示十二个小时的表针*/
{
x=320+90*sin(-((i*30+180)*pi)/180);
y=238+90*cos(-((i*30+180)*pi)/180);
sprintf(text,"%d",i);
setcolor(13);
outtextxy(x,y,text);
}
for(i=1;i<=60;i++)/*显示小格*/
{
x=319+98*sin(-((i*6)*pi)/180);
y=235+98*cos(-((i*6)*pi)/180);
outtextxy(x,y,".");
}
setfillstyle(1,6);
floodfill(120,20,2);
settextstyle(0,0,2);
setcolor(10);
outtextxy(270,20,"CLOCK");
setfillstyle(1,4);
floodfill(20,240,2);
setcolor(14);
settextstyle(0,0,1);
outtextxy(20,150," Copyright");
outtextxy(20,170," Flower me");
settextstyle(0,0,0);
outtextxy(8,240,"Jia Shou Liang");
outtextxy(20,260,"Han Jian");
outtextxy(20,280,"Zhu Hua Shu");
outtextxy(18,300,"Xu Chuan Zhi");
setcolor(5);
outtextxy(90,450,"Time,like a bird's wings. It will over silent. Treasure it.");
}
void hour(int angleHou) /*时针*/
{
int x,y;
setcolor(hcolor);
setlinestyle(0,0,3);
x=320+45*sin(-(angleHou*pi)/180);/*计算时针的坐标*/
y=240+45*cos(-(angleHou*pi)/180);
line(320,240,x,y);
}
void min(int angleMin) /*分针*/
{
int x,y;
setcolor(mcolor);
setlinestyle(0,0,3);
x=320+60*sin(-(angleMin*pi)/180);/*计算分针的坐标*/
y=240+60*cos(-(angleMin*pi)/180);
line(320,240,x,y);
}
void sec(int angleSec) /*秒针*/
{
int x,y;
while(kbhit())
{
closegraph();
exit(0);
}
setcolor(scolor);
setlinestyle(0,0,1);
x=320+80*sin(-(angleSec*pi)/180);/*计算秒针的坐标*/
y=240+80*cos(-(angleSec*pi)/180);
line(320,240,x,y);
sleep(1);
}
void eclock()
{
char ty[5],tm[3],td[3];
char th[3],tmm[3],ts[3];
struct tm *current_date;
time_t seconds;
setfillstyle(1,2);
bar(522,200,629,280);
time(&seconds);
current_date=localtime(&seconds);
sprintf(ty,"%d",current_date->tm_year+1900);
sprintf(tm,"%d",current_date->tm_mon+1);
sprintf(td,"%d",current_date->tm_mday);
sprintf(th,"%d",current_date->tm_hour);
sprintf(tmm,"%d",current_date->tm_min);
sprintf(ts,"%d",current_date->tm_sec);
setcolor(4);
settextstyle(0,0,1);
outtextxy(530,160,"Electronic");
outtextxy(530,180," Clock");
setcolor(5);
settextstyle(0,0,1);
outtextxy(530,220,ty);
outtextxy(563,220,"-");
outtextxy(575,220,tm);
outtextxy(585,220,"-");
outtextxy(595,220,td);
outtextxy(530,260,th);
outtextxy(550,260,":");
outtextxy(560,260,tmm);
outtextxy(575,260,":");
outtextxy(585,260,ts);
}
这个程序是显示时钟的程序,电子钟和石英钟都有显示。
********************************************************/
/********************************************************
头文件
********************************************************/
#include<stdio.h>
#include<graphics.h>
#include<dos.h>
#include<conio.h>
#include<math.h>
#include<time.h>
#define pi 3.141592
/********************************************************
全局变量
********************************************************/
int hcolor=9;/*时针颜色*/
int mcolor=6;/*分针颜色*/
int scolor=4;/*秒针颜色*/
/********************************************************
函数声明
*********************************************************/
void bellface();/*钟表界面*/
void hour(int);/*画时针*/
void min(int);/*画分针*/
void sec(int);/*画秒针*/
void eclock();/*显示电子时钟*/
/*主函数*/
main()
{
char *buf;
long size;
struct time tim;
int angleHou,angleMin,angleSec;/*时针,分针,秒针的角度*/
int driver,mode;
driver=VGA;
mode=VGAHI;
registerbgidriver(EGAVGA_driver);
initgraph(&driver,&mode,"");
cleardevice();
bellface();/*钟表界面*/
size=imagesize(220,140,420,340);/*截取屏幕图像*/
buf=(char *)malloc(size);
getimage(220,140,420,340,buf);
for(;;)
{
gettime(&tim);
/*下面三行是核心代码,每一个小格6度*/
angleMin=(tim.ti_min%60)*6+180;/*计算当前分针的角度*/
angleHou=(tim.ti_hour%12)*30+180+angleMin/24;/*计算当前时针的角度*/
angleSec=(tim.ti_sec%60)*6+180;/*计算当前秒针的角度*/
putimage(220,140,buf,COPY_PUT);
eclock();/*显示电子时钟*/
hour(angleHou);/*画时针*/
min(angleMin);/*画分针*/
sec(angleSec);/*画秒针*/
eclock();/*显示电子时钟*/
}
}
void bellface()
{
int i;
int x,y;
char text[3];
setbkcolor(0);
setcolor(2);
rectangle(120,40,520,440);
rectangle(5,5,630,470);
line(5,40,120,40);
line(5,440,120,440);
line(520,40,630,40);
line(520,440,630,440);
setcolor(2);
setfillstyle(7,2);
circle(320,240,100);/*表的外圆*/
floodfill(210,240,2);
for(i=1;i<=12;i++)/*显示十二个小时的表针*/
{
x=320+90*sin(-((i*30+180)*pi)/180);
y=238+90*cos(-((i*30+180)*pi)/180);
sprintf(text,"%d",i);
setcolor(13);
outtextxy(x,y,text);
}
for(i=1;i<=60;i++)/*显示小格*/
{
x=319+98*sin(-((i*6)*pi)/180);
y=235+98*cos(-((i*6)*pi)/180);
outtextxy(x,y,".");
}
setfillstyle(1,6);
floodfill(120,20,2);
settextstyle(0,0,2);
setcolor(10);
outtextxy(270,20,"CLOCK");
setfillstyle(1,4);
floodfill(20,240,2);
setcolor(14);
settextstyle(0,0,1);
outtextxy(20,150," Copyright");
outtextxy(20,170," Flower me");
settextstyle(0,0,0);
outtextxy(8,240,"Jia Shou Liang");
outtextxy(20,260,"Han Jian");
outtextxy(20,280,"Zhu Hua Shu");
outtextxy(18,300,"Xu Chuan Zhi");
setcolor(5);
outtextxy(90,450,"Time,like a bird's wings. It will over silent. Treasure it.");
}
void hour(int angleHou) /*时针*/
{
int x,y;
setcolor(hcolor);
setlinestyle(0,0,3);
x=320+45*sin(-(angleHou*pi)/180);/*计算时针的坐标*/
y=240+45*cos(-(angleHou*pi)/180);
line(320,240,x,y);
}
void min(int angleMin) /*分针*/
{
int x,y;
setcolor(mcolor);
setlinestyle(0,0,3);
x=320+60*sin(-(angleMin*pi)/180);/*计算分针的坐标*/
y=240+60*cos(-(angleMin*pi)/180);
line(320,240,x,y);
}
void sec(int angleSec) /*秒针*/
{
int x,y;
while(kbhit())
{
closegraph();
exit(0);
}
setcolor(scolor);
setlinestyle(0,0,1);
x=320+80*sin(-(angleSec*pi)/180);/*计算秒针的坐标*/
y=240+80*cos(-(angleSec*pi)/180);
line(320,240,x,y);
sleep(1);
}
void eclock()
{
char ty[5],tm[3],td[3];
char th[3],tmm[3],ts[3];
struct tm *current_date;
time_t seconds;
setfillstyle(1,2);
bar(522,200,629,280);
time(&seconds);
current_date=localtime(&seconds);
sprintf(ty,"%d",current_date->tm_year+1900);
sprintf(tm,"%d",current_date->tm_mon+1);
sprintf(td,"%d",current_date->tm_mday);
sprintf(th,"%d",current_date->tm_hour);
sprintf(tmm,"%d",current_date->tm_min);
sprintf(ts,"%d",current_date->tm_sec);
setcolor(4);
settextstyle(0,0,1);
outtextxy(530,160,"Electronic");
outtextxy(530,180," Clock");
setcolor(5);
settextstyle(0,0,1);
outtextxy(530,220,ty);
outtextxy(563,220,"-");
outtextxy(575,220,tm);
outtextxy(585,220,"-");
outtextxy(595,220,td);
outtextxy(530,260,th);
outtextxy(550,260,":");
outtextxy(560,260,tmm);
outtextxy(575,260,":");
outtextxy(585,260,ts);
}