就是这个程序了 是个时钟程序,画出一个钟表,调用系统当前时间,使钟表与系统时间同步,并在钟表下面显示当前时间 #include <graphics.h> #include <time.h> #include <stdio.h> #include <math.h> #include <stdlib.h> #include <conio.h> #include <dos.h>
main() { void minutepoint(int,int,int); void pointer(int x,int y,int r); void watchface(int,int,int); void drawcircle(int,int,int,int);
int gdriver=DETECT,gmode,errorcode; int x,y,r=60;
initgraph(&gdriver,&gmode,"");/*initialize the graph*/ errorcode=graphresult(); if(errorcode!=grOk) /*the graph initialization failed*/ { printf("Graphics error:%s\n",grapherrormsg(errorcode)); printf("Press any key to terminate the program......"); getch(); exit(1); } /*end if*/
x=getmaxx()/2; /*set the x coordinate of the circle*/ y=getmaxy()/2; /*set the y coordinate of the circle*/
setbkcolor(LIGHTGRAY); /* set the backgroud color*/
setcolor(YELLOW); outtextxy(x-r-30,y+r+50,"press any key to quit......"); watchface(x,y,r); pointer(x,y,r); } /*end main*/
void drawcircle(int x,int y,int r,int color) { setcolor(color); /*set the circle's color*/ circle(x,y,r); setfillstyle(1,color); floodfill(x,y,color); } /*end drawcircle*/
/*the following function is to draw the hour graduations*/ void hourpoint(int x,int y,int r) { int r0=r-10,i; /*r-r0 is the length of the hour graduations*/ double PI=3.1415927; double temp1=0,temp2=0;
for(i=0;i<12;i++) { temp1=cos(i*PI/6); temp2=sin(i*PI/6);
/*draw the hour graduations*/ line(x+floor(r*temp1),y-floor(r*temp2),x+floor(r0*temp1),y-floor(r0*temp2)); } /*end for*/ } /*end hourpoint*/
/*the following functiong is to draw the minute graduations*/ void minutepoint(int x,int y,int r) { int r0=r-5,i; /*r-r0 is the length of the minute graduations*/ double PI=3.1415927,temp1,temp2;
for(i=0;i<60;i++) if(i%5!=0) { temp1=cos(i*PI/30); temp2=sin(i*PI/30);
/*draw the minute graduations*/ line(x+floor(r*temp1),y-floor(r*temp2),x+floor(r0*temp1),y-floor(r0*temp2)); } /*end if*/ } /*end minutepoint*/
/*the following function is to draw the face of the clock*/ void watchface(int x,int y,int r) { drawcircle(x,y,r+10,YELLOW); drawcircle(x,y,r,LIGHTBLUE);
setcolor(YELLOW); /*set color of the hour graduations and the minute graduations*/
hourpoint(x,y,r); minutepoint(x,y,r); } /*end watchface*/
/*the following function is to draw the hourpointer,minutepointer,secondpointer*/ void pointer(int x,int y,int r) { /*len is the secondpointer's length*/ /*mlen is the minutepointer's length*/ /*hlen is the hourpointer's length*/ /*counter is to count the current minute, the hourpointer will move forward a graduation every 12 minutes*/
int len=r-15,mlen=r-30,hlen=r-45,i,j,k,counter=0; double PI=3.1415927; struct time t,t1,t2; time_t t0; int *buffer1,size1;
/*apply space to store the image of current time*/ time(&t0); size1=imagesize(x-r-30,y+r+30,x-r+strlen(ctime(&t0)),y+r+40); buffer1=(int *) malloc (size1); if(!buffer1) { printf("Can't apply spaces to store the image!\n"); printf("Press any key to terminate the program......"); getch(); } /*end if*/
settextstyle(0,0,1);/*set the text property*/
gettime(&t); counter=t.ti_min/12; /*calculate current minute is how many times of 12*/ k=(t.ti_hour%12)*5+counter;/*set the hourpointer's initial position*/
for(;;) /*the circulation of the hourpointer*/ for(j=t.ti_min;j<t.ti_min+60;) /*the circulation of the minutepointer*/ { if(t.ti_min%12==0) /*redraw the hour pointer*/ { setcolor(LIGHTBLUE); line(x,y,x+floor(hlen*cos(k*PI/30)),y-floor(hlen*sin(k*PI/30))); setcolor(YELLOW); line(x,y,x+floor(hlen*cos(k*PI/30)),y-floor(hlen*sin(++k*PI/30))); } /*end if*/ for(i=t.ti_sec;i<t.ti_sec+60;)/*the circulation of the secondpointer*/ { gettime(&t1); while(1) { if(kbhit()) exit(0);/*press any key to exit*/ gettime(&t2); if(t2.ti_sec-t1.ti_sec==1||t2.ti_sec-t1.ti_sec==-59) break; } /*end while*/
/*draw the minutepointer*/ line(x,y,x+floor(mlen*cos((15-j)*PI/30)),y-floor(mlen*sin((15-j)*PI/30)));
if(t2.ti_sec-t1.ti_sec==-59) /*redraw the minute pointer every 60 seconds*/ { setcolor(LIGHTBLUE); line(x,y,x+floor(mlen*cos((15-j)*PI/30)),y-floor(mlen*sin((15-j)*PI/30))); setcolor(YELLOW); line(x,y,x+floor(mlen*cos((15-j)*PI/30)),y-floor(mlen*sin((15-++j)*PI/30))); } /*end if*/
/*redraw the hourpointer*/ line(x,y,x+floor(hlen*cos((15-k)*PI/30)),y-floor(hlen*sin((15-k)*PI/30)));
/*redraw the secondpointer*/ setcolor(LIGHTBLUE); line(x,y,x+floor(len*cos((15-i)*PI/30)),y-floor(len*sin((15-i)*PI/30))); setcolor(YELLOW); line(x,y,x+floor(len*cos((15-i)*PI/30)),y-floor(len*sin((15-++i)*PI/30)));
/*show the current time in text form*/ putimage(x-r-30,y+r+30,buffer1,1);/*put the current time*/ time(&t0);/*get the current time*/ outtextxy(x-r-30,y+r+30,ctime(&t0)); /*get image of the current time*/ getimage(x-r-30,y+r+30,x-r-30+size1,y+r+40,buffer1); } /*end for*/ } /*end for*/ } /*end pointer*/