有关时钟的程序
#include <stdio.h>#include <conio.h>
#include <graphics.h>
#define PI 3.1415926
#include <math.h>
#include <dos.h>
#include <stdlib.h>
#define GETX(a,l) l*sin(PI-a*PI/30)+320 /*取得以320,240为圆心,l为半径,a*PI/30为偏移角度(以y正轴为准)的圆上的x坐标*/
#define GETY(a,l) l*cos(PI-a*PI/30)+240 /*取得以320,240为圆心,l为半径,a*PI/30为偏移角度(以y正轴为准)的圆上的y坐标*/
void xianshi(int x,int y,int number,int w)
{ switch(number)
{case 0:{line(x,y,x+w,y); /*1*/
line(x,y,x,y+w); /*2*/
line(x,y+w,x,y+2*w); /*3*/
line(x,y+2*w,x+w,y+2*w); /*4*/
line(x+w,y+2*w,x+w,y+w); /*5*/
line(x+w,y+w,x+w,y); /*6*/
/*line(x,y+w,x+w,y+w);7*/ }
break;
case 1:{line(x+w,y+2*w,x+w,y+w); /*5*/
line(x+w,y+w,x+w,y); /*6*/}
break;
case 2:{line(x,y,x+w,y); /*1*/
line(x,y+w,x,y+2*w); /*3*/
line(x,y+2*w,x+w,y+2*w); /*4*/
line(x+w,y+w,x+w,y); /*6*/
line(x,y+w,x+w,y+w);/*7*/
}
break;
case 3:{line(x,y,x+w,y); /*1*/
line(x,y+2*w,x+w,y+2*w); /*4*/
line(x+w,y+2*w,x+w,y+w); /*5*/
line(x+w,y+w,x+w,y); /*6*/
line(x,y+w,x+w,y+w); /*7*/}
break;
case 4:{
line(x,y,x,y+w); /*2*/
line(x+w,y+2*w,x+w,y+w); /*5*/
line(x+w,y+w,x+w,y); /*6*/
line(x,y+w,x+w,y+w); /*7*/}
break;
case 5:{line(x,y,x+w,y); /*1*/
line(x,y,x,y+w); /*2*/
line(x,y+2*w,x+w,y+2*w); /*4*/
line(x+w,y+2*w,x+w,y+w); /*5*/
line(x,y+w,x+w,y+w); /*7*/}
break;
case 6:{line(x,y,x+w,y); /*1*/
line(x,y,x,y+w); /*2*/
line(x,y+w,x,y+2*w); /*3*/
line(x,y+2*w,x+w,y+2*w); /*4*/
line(x+w,y+2*w,x+w,y+w); /*5*/
line(x,y+w,x+w,y+w); /*7*/}
break;
case 7:{line(x,y,x+w,y); /*1*/
line(x+w,y+2*w,x+w,y+w); /*5*/
line(x+w,y+w,x+w,y); /*6*/
}
break;
case 8:{line(x,y,x+w,y); /*1*/
line(x,y,x,y+w); /*2*/
line(x,y+w,x,y+2*w); /*3*/
line(x,y+2*w,x+w,y+2*w); /*4*/
line(x+w,y+2*w,x+w,y+w); /*5*/
line(x+w,y+w,x+w,y); /*6*/
line(x,y+w,x+w,y+w); /*7*/}
break;
case 9:{line(x,y,x+w,y); /*1*/
line(x,y,x,y+w); /*2*/
line(x,y+2*w,x+w,y+2*w); /*4*/
line(x+w,y+2*w,x+w,y+w); /*5*/
line(x+w,y+w,x+w,y); /*6*/
line(x,y+w,x+w,y+w); /*7*/}
break;
}
}
/*图形初始化函数定义*/
void Init()
{int gdriver, gmode;
/*registerbgidriver(EGAVGA_driver);*/
gdriver=DETECT;
initgraph(&gdriver, &gmode, "c:\\tc");
}
main()
{ float jiao;
int i=0,r;
int h,m,s;
struct time t;
Init();
rectangle(15,15,120,45);
/*circle(320,240,2);*/
setcolor(4);
for(jiao=PI,i=0;i<60;i++,jiao-=PI/30)
{if(i%5==0)
r=7;
else
r=2;
{setfillstyle(1,random(15));
circle(200*sin(jiao)+320,200*cos(jiao)+240,r);
floodfill(200*sin(jiao)+320,200*cos(jiao)+240,4);}/*画周边的60个圆(12个半径为7的大圆,48个半径为2的小圆)*/
}
{gettime(&t);
h=t.ti_hour;
m=t.ti_min;
s=t.ti_sec;}
setcolor(4);
settextstyle(0,0,1);
outtextxy(490,410,"myname:maqingqin");
setcolor(3);
outtextxy(500,430,"QICQ:812154952");
do{
{setcolor(4);
setlinestyle(0,0,3);
settextstyle(0,0,2);
outtextxy(308,60,"12");}
if(h<10)
{xianshi(20,20,0,10);
xianshi(35,20,h,10);}
else
{xianshi(20,20,h/10,10);
xianshi(35,20,h%10,10);}
{line(50,24,50,27);
line(50,34,50,37);}
if(m>=0&&m<12)
{setcolor(4);
setlinestyle(0,0,3);
line(320,240,GETX(h*5,100),GETY(h*5,100));}
else if(m>=12&&m<24)
{setcolor(4);
setlinestyle(0,0,3);
line(320,240,GETX((h*5+1),100),GETY((h*5+1),100));}
else if(m>=24&&m<36)
{setcolor(4);
setlinestyle(0,0,3);
line(320,240,GETX((h*5+2),100),GETY((h*5+2),100)); }
else if(m>=36&&m<48)
{setcolor(4);
setlinestyle(0,0,3);
line(320,240,GETX((h*5+3),100),GETY((h*5+3),100));}
else
{setcolor(4);
setlinestyle(0,0,3);
line(320,240,GETX((h*5+4),100),GETY((h*5+4),100));}
/*画时钟的位置*/
setcolor(5);
outtextxy(315,410,"6");
if(m<10)
{xianshi(55,20,0,10);
xianshi(70,20,m,10);}
else
{xianshi(55,20,m/10,10);
xianshi(70,20,m%10,10);}
{line(85,24,85,27);
line(85,34,85,37);}
setlinestyle(0,0,3);
line(320,240,GETX(m,150),GETY(m,150));/*画时钟的分针*/
setcolor(3);
outtextxy(140,235,"9");
outtextxy(490,235,"3");
if(s<10)
{xianshi(90,20,0,10);
xianshi(105,20,s,10);}
else
{xianshi(90,20,s/10,10);
xianshi(105,20,s%10,10);}
setlinestyle(0,0,1);
line(320,240,GETX(s,192),GETY(s,192)); /*画时钟的秒针*/
gettime(&t);
switch(t.ti_min)
{ case 0: if(t.ti_sec==0)
{setcolor(0);
setlinestyle(0,0,3);
line(320,240,GETX((h*5-1),100),GETY((h*5-1),100));}
break;
case 12: if(t.ti_sec==0)
{setcolor(0);
setlinestyle(0,0,3);
line(320,240,GETX((h*5),100),GETY((h*5),100));}
break;
case 24:{setcolor(0);
setlinestyle(0,0,3);
line(320,240,GETX((h*5+1),100),GETY((h*5+1),100));}
break;
case 36:if(t.ti_sec==0)
{setcolor(0);
setlinestyle(0,0,3);
line(320,240,GETX((h*5+2),100),GETY((h*5+2),100));}
break;
case 48:if(t.ti_sec==0)
{setcolor(0);
setlinestyle(0,0,3);
line(320,240,GETX((h*5+3),100),GETY((h*5+3),100));}
break;
default:break;
}
if(t.ti_hour!=h)
{setcolor(0);
setlinestyle(0,0,3);
if(h<10)
{xianshi(20,20,0,10);
xianshi(35,20,h,10);}
else
{xianshi(20,20,h/10,10);
xianshi(35,20,h%10,10);}
h=t.ti_hour;}
if(t.ti_min!=m)
{setcolor(0);
setlinestyle(0,0,3);
if(m<10)
{ if(t.ti_min/10!=m/10)
xianshi(55,20,0,10);
xianshi(70,20,m,10);}
else
{if(t.ti_min/10!=m/10)
xianshi(55,20,m/10,10);
xianshi(70,20,m%10,10);}
line(320,240,GETX(m,150),GETY(m,150));
m=t.ti_min;}
if(t.ti_sec!=s)
{ setcolor(0);
setlinestyle(0,0,3);
if(s<10)
{ if(t.ti_sec/10!=s/10)
xianshi(90,20,0,10);
xianshi(105,20,s,10);}
else
{ if(t.ti_sec/10!=s/10)
xianshi(90,20,s/10,10);
xianshi(105,20,s%10,10);}
/*sound(500);
delay(500);
nosound();*/
setlinestyle(0,0,1);
line(320,240,GETX(s,192),GETY(s,192));
s=t.ti_sec;}
}while(!kbhit());/*当有按键被按下就退出*/
closegraph();
}