C++时钟
#include<graphics.h>#include<conio.h>
#include<math.h>
#include<stdio.h>
#include<windows.h>
#include<iostream.h>
#define PI 3.1415926
void Draw(int hour,int minute,int second)
{
double a_hour,a_min,a_sec;
int x_hour,y_hour,x_min,y_min,x_sec,y_sec;
int x_hour1,y_hour1,x_min1,y_min1,x_sec1,y_sec1;
a_sec = second*2*PI/60;
a_min = minute*2*PI/60+a_sec/60;
a_hour= hour*2*PI/12+a_min/12;
x_sec = 320 +(int)(115 * sin(a_sec));
y_sec = 240 -(int)(115 * cos(a_sec));
x_min = 320 + (int)(100 * sin(a_min));
y_min = 240 - (int)(100 * cos(a_min));
x_hour= 320 + (int)(70 * sin(a_hour));
y_hour= 240 - (int)(70 * cos(a_hour));
/*------------------------------------------------------*/
x_sec1 = 320 +(int)(30 * sin(a_sec));
y_sec1 = 240 -(int)(30 * cos(a_sec));
x_min1 = 320 + (int)(20 * sin(a_min));
y_min1 = 240 - (int)(20 * cos(a_min));
x_hour1= 320 + (int)(20 * sin(a_hour));
y_hour1= 240 - (int)(20 * cos(a_hour));
setlinestyle(PS_SOLID,NULL,6);
setcolor(WHITE);
line(320,240,x_hour,y_hour);
line(320, 240, 640-x_hour1,480-y_hour1 );
setlinestyle(PS_SOLID, NULL, 4);
setcolor(LIGHTGRAY);
line(320, 240, x_min, y_min);
line(320, 240, 640-x_min1,480-y_min1 );
setlinestyle(PS_SOLID, NULL, 2);
setcolor(RED);
line(320, 240, x_sec, y_sec);
//line(320, 240, x_sec1, y_sec1);
line(320, 240, 640-x_sec1,480-y_sec1 );
}
void main(void)
{
initgraph(640,480);
/*----------------------------------------------------------------*/
circle(320,240,4);
circle(320,240,115);
circle(320,240,120);
rectangle(300,360,340,400);
rectangle(250,400,390,440);
circle(200,120,50);
circle(200,120,45);
circle(440,120,50);
circle(440,120,45);
outtextxy(210,235,'9');
outtextxy(425,235,'3');
outtextxy(315,130,"12");
outtextxy(315,339,'6');
outtextxy(262,322,'7');
outtextxy(260,145,"11");
outtextxy(370,322,'5');
outtextxy(370,145,'1');
outtextxy(225,287,'8');
outtextxy(408,287,'4');
outtextxy(222,185,"10");
outtextxy(410,181,'2');
outtextxy(180,110,"月");
outtextxy(220,110,"日");
outtextxy(410,110,"星期");
/*----------------------------------------------------------------*/
setwritemode(R2_XORPEN);
SYSTEMTIME ti; // SYSTEMTIME是一个结构体 //
char Second_R[1],Second_L[1];
char Minute_R[1],Minute_L[1];
char Hour_R[1],Hour_L[1];
while(!kbhit())
{
GetLocalTime(&ti);
// 把当前的时间给结构体变量ti,让它保存数据,用&ti作为实参是因为形参是指针,指针进行操作,直接改变原来的数据 //
/*----------------------------------------------------------------*/
switch(ti.wDayOfWeek)
{
case 0: setcolor(LIGHTRED);
outtextxy(450,110,"日");
break;
case 1: setcolor(YELLOW);
outtextxy(450,110,"一");
break;
case 2: setcolor(LIGHTCYAN);
outtextxy(450,110,"二");
break;
case 3: setcolor(LIGHTBLUE);
outtextxy(450,110,"三");
break;
case 4: setcolor(LIGHTGREEN);
outtextxy(450,110,"四");
break;
case 5: setcolor(LIGHTMAGENTA);
outtextxy(450,110,"五");
break;
case 6: setcolor(RGB(255,128,0));
outtextxy(450,110,"六");
break;
}
/*----------------------------------------------------------------*/
char Day[2],Month[2],Year[4];
setcolor(CYAN);
sprintf(Month,"%d",ti.wMonth);
outtextxy(160,110,Month);
sprintf(Day,"%d",ti.wDay);
outtextxy(205,110,Day);
setcolor(LIGHTMAGENTA);
sprintf(Year,"%d",ti.wYear);
outtextxy(305,370,Year);
/*----------------------------------------------------------------*/
Draw(ti.wHour,ti.wMinute,ti.wSecond);
/*----------------------------------------------------------------*/ sprintf(Hour_R, "%d", ti.wHour %10);
sprintf(Hour_L,"%d", (int)ti.wHour /10);
outtextxy(272,420,Hour_L);
outtextxy(280,420,Hour_R);
outtextxy(300,420,':');
sprintf(Minute_R, "%d", ti.wMinute %10);
sprintf(Minute_L,"%d", (int)ti.wMinute /10);
outtextxy(312,420,Minute_L);
outtextxy(320,420,Minute_R);
outtextxy(340,420,':');
sprintf(Second_R, "%d", ti.wSecond %10);
sprintf(Second_L,"%d", (int)ti.wSecond /10);
outtextxy(352,420,Second_L);
outtextxy(360,420,Second_R);
Sleep(1000);
Draw(ti.wHour,ti.wMinute,ti.wSecond);
}
/*----------------------------------------------------------------*/
getch();
closegraph();
}