C编写时钟程序
// 需要使用EasyX 库来描绘图形界面,show the code;
#include <graphics.h>
#include <conio.h>
#include <stdio.h>
#include <math.h>
int flag = 0;
void Draw_T_1(int x, int y)
{
// setcolor(WHITE);
line(x-2, y, x-2, y-10);
line(x-2, y-10, x-20, y-10);
line(x-20, y-10, x-20, y-14);
line(x+2, y, x+2, y-10);
line(x+2, y-10, x+20, y-10);
line(x+20, y-10, x+20, y-14);
line(x-2, y, x+2, y);
line(x-20, y-14, x+20, y-14);
//setfillstyle(LIGHTMAGENTA, SOLID_FILL, NULL);
// floodfill(x, y-1, WHITE);
}
void Draw_T_2(int x, int y)
{
// setcolor(WHITE);
line(x-2, y, x-2, y-20);
line(x-2, y-20, x-20, y-20);
line(x-20, y-20, x-20, y-24);
line(x+2, y, x+2, y-20);
line(x+2, y-20, x+20, y-20);
line(x+20, y-20, x+20, y-24);
line(x-2, y, x+2, y);
line(x-20, y-24, x+20, y-24);
/// setfillstyle(LIGHTMAGENTA, SOLID_FILL, NULL);
// floodfill(x, y-1, WHITE);
}
void Draw(int h, int m, int s)
{
int h_x, h_y, m_x, m_y, s_x, s_y;
double s_a, m_a, h_a;
s_a = s*2*3.14/60;
m_a = m*2*3.14/60 + s_a/60;
h_a = h*2*3.14/12 + m_a/12;
s_x = 300 + (int)160*sin(s_a);
s_y = 300 - (int)160*cos(s_a);
m_x = 300 + (int)140*sin(m_a);
m_y = 300 - (int)140*cos(m_a);
// m_a = m*2*3.14/60; + s_a/60;
h_x = 300 + (int)120*sin(h_a);
h_y = 300 - (int)120*cos(h_a);
line(300, 300, h_x, h_y);
line(300, 300, m_x, m_y);
line(300, 300, s_x, s_y);
}
/*
void setlinestyle(
int linestyle,
WORD upattern = NULL,
int thickness = 1
);
*/
void Draw_Num()
{
int i, j, x, y;
char str[4];
double a;
for(i = 1; i <= 12; i++)
{
a = i*2*3.14/12;
x = 300 + (int)200*sin(a);
y = 300 - (int)200*cos(a);
setcolor(RED);
circle(x, y, 20);
setfillstyle(YELLOW, SOLID_FILL, NULL);
floodfill(x, y, RED);
setcolor(WHITE);
sprintf(str, "%d", i);
moveto(x-3, y-3);
outtext(str);
}
}
void Draw_Down()
{
int L_x1, L_y1, L_x2, L_y2, L_x3, L_y3, R_x1, R_y1, R_x2, R_y2, R_x3, R_y3;
double a;
a = 7*2*3.14/12;
L_x1 = 300 + (int)220*sin(a);
L_y1 = 300 - (int)220*cos(a);
L_x2 = 300 + (int)340*sin(a);
L_y2 = 300 - (int)340*cos(a);
L_x3 = 300 + (int)400*sin(a);
L_y3 = 300 - (int)400*cos(a);
a = 5*2*3.14/12;
R_x1 = 300 + (int)220*sin(a);
R_y1 = 300 - (int)220*cos(a);
R_x2 = 300 + (int)340*sin(a);
R_y2 = 300 - (int)340*cos(a);
R_x3 = 300 + (int)400*sin(a);
R_y3 = 300 - (int)400*cos(a);
line(L_x1, L_y1, L_x3, L_y3);
line(L_x3, L_y3, L_x3+50, L_y3);
line(L_x3+50, L_y3, L_x2+50, L_y2);
line(R_x1, R_y1, R_x3, R_y3);
line(R_x3, R_y3, R_x3-50, R_y3);
line(R_x3-50, R_y3, R_x2-50, R_y2);
line(L_x2+50, L_y2, R_x2-50, R_y2);
/*setfillstyle(YELLOW, SOLID_FILL, NULL);
floodfill(300, 521, WHITE);*/
// draw rectangle
rectangle(245, 535, 355, 575);
rectangle(250, 540, 350, 570);
}
void Show_Time(int h, int m, int s)
{
char str[15];
moveto(265, 550);
sprintf(str, "%d: %d: %d", h, m, s);
outtext(str);
}
void Draw_Font(int x, int y)
{
line(x, y, x-15, y);
line(x, y, x+15, y);
int t = y - 10;
line(x, t, x-6, t);
line(x, t, x+6, t);
line(x-15, y, x-6, t);
line(x+15, y, x+6, t);
setfillstyle(LIGHTGRAY, SOLID_FILL, NULL);
floodfill(x, y-1, WHITE);
}
void main()
{
SYSTEMTIME t;
int flag = 0;
initgraph(600, 900);
//loadimage(NULL, "D:\\1.bmp");
/*int r = 180;
setcolor(LIGHTCYAN);
for(r = 180; r <= 220; r++)
{
circle(300, 300, r);
}*/
setcolor(RED);
circle(300, 300, 220);
circle(300, 300, 180);
setfillstyle(GREEN, SOLID_FILL, NULL);
floodfill(85, 300, RED);
setcolor(WHITE);
Draw_Num();
Draw_Down();
Draw_Font(300, 80);
//Draw_T_1(300, 70);
circle(300, 300, 4);
while(1)
{
//Draw_T_1(300, 70);
circle(300, 300, 4);
GetLocalTime(&t);
setcolor(WHITE);
circle(300, 300, 4);
if(flag == 0)
{
Draw_T_1(300, 70);
setfillstyle(LIGHTBLUE, SOLID_FILL, NULL);
floodfill(300, 69, WHITE);
flag = 1;
Draw(t.wHour, t.wMinute, t.wSecond);
Show_Time(t.wHour, t.wMinute, t.wSecond);
Sleep(500);
setcolor(0);
setfillstyle(BLACK, SOLID_FILL, NULL);
floodfill(300, 69, WHITE);
Draw_T_1(300, 70);
Draw(t.wHour, t.wMinute, t.wSecond);
}
else
{
Draw_T_2(300, 70);
setfillstyle(LIGHTBLUE, SOLID_FILL, NULL);
floodfill(300, 69, WHITE);
flag = 0;
Draw(t.wHour, t.wMinute, t.wSecond);
Show_Time(t.wHour, t.wMinute, t.wSecond);
Sleep(500);
setcolor(0);
setfillstyle(BLACK, SOLID_FILL, NULL);
floodfill(300, 69, WHITE);
Draw_T_2(300, 70);
Draw(t.wHour, t.wMinute, t.wSecond);
}
setcolor(0);
//cleardevice();
// Draw_T_1(300, 70);
}
}

浙公网安备 33010602011771号