|
/* FileName: CLOCK.C Author : Crystal.Chen E-Mail : crystal.chen.cc@gmail.com Descrip : 一个时钟程序 Version : 0.1 */
#include <STDIO.H> #include <GRAPHICS.H> #include <CONIO.H> #include <MATH.H> #include <DOS.H> #include <TIME.H>
#define PI 3.1415926
/* BGI模式初始化 */ void initgui(void) { int gdriver = DETECT, gmode, errorcode;
/* Clear screen at the beginning of program. */ clrscr();
/* 注册BGI驱动后可以不需要.BGI文件的支持运行 */ registerbgidriver(EGAVGA_driver);
/* Initialize graphics and local variables */ initgraph(&gdriver, &gmode, ""); /* Read result of initialization */ errorcode = graphresult(); if(errorcode != grOk) { printf("Grapics error: %s\n\n", grapherrormsg(errorcode)); printf("Press any key to halt:"); getch(); exit(1); } }
main() { double x, y; int i, j, k, l; /* i,j,k是圆的度数,在0--360度之间变化 */ double a, b, c, d; /* 定义时钟的刻度 */ struct time t;
/* 初始化秒针、分针和时针 */ gettime(&t); /* 初始化秒针 */ i = t.ti_sec * 6; /* 初始化分针 */ j = t.ti_min * 6; /* 初始化时针 */ if(t.ti_hour < 12) { k = (t.ti_hour * 30) + (t.ti_min / 2); } else { k = ((t.ti_hour - 12) * 30) + (t.ti_min / 2); }
/* BGI模式初始化 */ initgui();
/* 设置背景颜色 0 : 黑色 5 : 紫色 10 : 草绿色 15 : 白色 1 : 蓝色 6 : 橙色 11 : 蓝绿色 16 : 无 2 : 绿色 7 : 灰色 12 : 粉红色 3 : 浅蓝色 8 : 深灰色 13 : 粉紫色 4 : 红色 9 : 天蓝色 14 : 黄色 */ setbkcolor(8); /* 开始走秒针 */ while(!kbhit()) { /* 设定圆为蓝绿色 */ setcolor(11); /* 给时钟划一个圆 */ circle(getmaxx() / 2, getmaxy() / 2, 150);
/* 在圆上画上时钟的刻度 */ for(l = 0; l < 360; l = l + 6) { if(l % 30 == 0) { a = (getmaxx() / 2) + (140 * cos(l * PI / 180)); b = (getmaxy() / 2) + (140 * sin(l * PI / 180)); } else { a = (getmaxx() / 2) + (145 * cos(l * PI / 180)); b = (getmaxy() / 2) + (145 * sin(l * PI / 180)); } c = (getmaxx() / 2) + (150 * cos(l * PI / 180)); d = (getmaxy() / 2) + (150 * sin(l * PI / 180)); line(a, b, c, d); } /* 中午12点之前,显示AM,之后,显示PM */ if((t.ti_hour < 12)) { moveto(getmaxx() / 2 - 7, getmaxy() / 2 - 130); outtext("AM"); } else { moveto(getmaxx() / 2 - 7, getmaxy() / 2 - 130); outtext("PM"); }
/* 显示是BIOS CLOCK */ moveto(getmaxx() / 2 - 49, getmaxy() / 2 - 110); outtext("SYSTEM CLOCK");
/* 设定秒钟指针为白色 */ setcolor(15); /* 判断i的值 */ if(i >= 360) { i = 0; } /* 获取秒钟X、Y轴的终点 */ x = (getmaxx() / 2) + (135 * cos((i - 90) * PI / 180)); y = (getmaxy() / 2) + (135 * sin((i - 90) * PI / 180)); line(getmaxx() / 2, getmaxy() / 2, x, y);
/* 设定分钟指针为蓝色 */ setcolor(12); /* 判断j的值 */ if(j >= 360) { j = 0; } /* 获取分钟X、Y轴的终点 */ x = (getmaxx() / 2) + (100 * cos((j - 90) * PI / 180)); y = (getmaxy() / 2) + (100 * sin((j - 90) * PI / 180)); line(getmaxx() / 2, getmaxy() / 2, x, y);
/* 设定时钟指针为草绿色 */ setcolor(10); /* 判断j的值 */ if(k >= 360) { k = 0; } /* 获取分钟X、Y轴的终点 */ x = (getmaxx() / 2) + (60 * cos((k - 90) * PI / 180)); y = (getmaxy() / 2) + (60 * sin((k - 90) * PI / 180)); line(getmaxx() / 2, getmaxy() / 2, x, y);
i = i + 6; if(i % 360 == 0) { j = j + 6; } if(j % 72 == 0 && j != 0 && i % 360 == 0) { k = k + 6; }
/* 延时一秒钟 */ sleep(1);
/* 清除屏幕,开始下一秒钟的走动 */ cleardevice(); }
getch(); /* 关闭BGI模式 */ closegraph(); }
|