效果图:(其实还可以再美观一点 o(∩_∩)o 。。。)
1 /******************************************** 2 * 程序名称:MR.DUAN 的模拟时钟 3 * 作 者:WindAutumn <duanxu@outlook.com> 4 * 最后修改:2012-8-9-PM 5 * 版 本 号:1.0 6 * 7 * 以后有空再修改 8 * *****************************************/ 9 10 #include<stdio.h> 11 #include<Windows.h> 12 #include<time.h> 13 #include<math.h> 14 15 #define MAXWIDE 50 16 #define MAXHIGH 24 17 #define CENTER_X 25 18 #define CENTER_Y 12 19 #define CALENDA_Y_OFFSET 1 20 #define TIME_OFFSET_X MAXWIDE + 1 21 #define TIME_OFFSET_Y CALENDA_Y_OFFSET + 13 22 #define PI 3.1415926 23 24 void InitScreen(HANDLE hClock); 25 void HideCursor(HANDLE hClock); 26 void GotoXY(HANDLE hClock, int x, int y); 27 void PrintCalendar(HANDLE hClock, int x, int y, time_t * p_rawtime); 28 void PrintClock(HANDLE hClock, time_t * p_rawtime); 29 void PrintTime(HANDLE hClock, time_t * p_rawtime); 30 31 void main() 32 { 33 time_t rawtime = time(0); 34 35 HANDLE hClock = GetStdHandle(STD_OUTPUT_HANDLE); 36 system("color 7b"); 37 SetConsoleTitle("MR.DUAN 的时钟"); 38 HideCursor(hClock); 39 InitScreen(hClock); 40 PrintCalendar(hClock, MAXWIDE+1, CALENDA_Y_OFFSET, &rawtime); 41 42 PrintClock(hClock, &rawtime); 43 } 44 45 void InitScreen(HANDLE hClock) // 打印周围红框框 46 { 47 int i; 48 49 SetConsoleTextAttribute(hClock, FOREGROUND_INTENSITY | FOREGROUND_RED | 50 BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_RED); // 红色字体,白色背景 51 GotoXY(hClock, 0, 0); 52 printf("╔"); 53 for(i=1; i<(MAXWIDE-1)/2; i++) 54 printf("═"); 55 printf("╗"); 56 57 for(i=1; i< MAXHIGH; i++) 58 { 59 GotoXY(hClock, 0, i); 60 printf("║"); 61 GotoXY(hClock, MAXWIDE-2, i); 62 printf("║"); 63 } 64 65 GotoXY(hClock, 0, MAXHIGH); 66 printf("╚"); 67 for(i=1; i<(MAXWIDE-1)/2; i++) 68 printf("═"); 69 printf("╝"); 70 71 GotoXY(hClock, 0, 0); 72 } 73 74 void HideCursor(HANDLE hClock) 75 { 76 CONSOLE_CURSOR_INFO cursor_info = {1, 0}; 77 SetConsoleCursorInfo(hClock, &cursor_info); 78 } 79 80 void GotoXY(HANDLE hClock, int x, int y) 81 { 82 COORD coord; 83 coord.X = x; 84 coord.Y = y; 85 SetConsoleCursorPosition(hClock, coord); 86 } 87 88 void PrintCalendar(HANDLE hClock, int x, int y, time_t * p_rawtime) 89 { 90 int i; 91 struct tm * timenow = localtime(p_rawtime); 92 int year = timenow->tm_year+1900 , years; 93 int month = timenow->tm_mon + 1; 94 int day = timenow->tm_mday; 95 int week,centry; 96 97 int mon[12]= {31,28,31,30,31,30,31,31,30,31,30,31}; 98 if(year%400==0||year%4==0&&year%100!=0) 99 mon[1]++; 100 if ( month < 3 ) 101 { 102 year -= 1; 103 month += 12; 104 } 105 centry = year/100; 106 years = year-centry*100; 107 week = (centry/4)-2*centry+(years+years/4)+(26*(month+1)/10); 108 week = (week%7+7)%7; 109 110 GotoXY(hClock, x,y++); 111 printf("Sun Mon Tue Wed Thu Fri Sat"); 112 GotoXY(hClock, x,y); 113 for(i=0; i<week; i++) 114 printf(" "); 115 for(i=1; i<=mon[month-1]; i++) 116 { 117 if(day==i) 118 { 119 SetConsoleTextAttribute(hClock,FOREGROUND_INTENSITY | FOREGROUND_BLUE | 120 BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_RED); 121 printf("%3d ",i++); 122 SetConsoleTextAttribute(hClock,FOREGROUND_INTENSITY | FOREGROUND_RED | 123 BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_RED); 124 } 125 printf("%3d ",i); 126 if((i+week)%7==0) 127 GotoXY(hClock, x,y+=2); 128 } 129 } 130 131 void PrintClock(HANDLE hClock, time_t * p_rawtime) 132 { 133 struct tm * timenow = localtime(p_rawtime); 134 int board_degree=0, hour_degree=0, min_degree=0, sec_degree=0,i,R=11; 135 136 SetConsoleTextAttribute(hClock,FOREGROUND_INTENSITY | FOREGROUND_BLUE | 137 BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_RED); 138 139 for(i=1; i<=12; i++) // 打印周围的数字1-12 140 { 141 GotoXY(hClock,CENTER_X + R*2*sin(PI*i/6), CENTER_Y - R*cos(PI*i/6)); 142 printf("%d",i); 143 } 144 145 while(1) 146 { 147 while(*p_rawtime != time(0)) 148 { 149 *p_rawtime = time(0); 150 timenow = localtime(p_rawtime); 151 hour_degree = timenow->tm_hour % 12 * 30; 152 min_degree = timenow->tm_min * 6; 153 sec_degree = timenow->tm_sec * 6; 154 155 PrintTime(hClock, p_rawtime); 156 157 for(i=1; i<11; i++) 158 { 159 SetConsoleTextAttribute(hClock,FOREGROUND_INTENSITY | FOREGROUND_BLUE | 160 BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_RED); 161 GotoXY(hClock,CENTER_X + i*2*sin(PI*(sec_degree-6)/180), CENTER_Y - i*cos(PI*(sec_degree-6)/180)); 162 printf(" "); 163 GotoXY(hClock,CENTER_X + i*2*sin(PI*sec_degree/180), CENTER_Y - i*cos(PI*sec_degree/180)); 164 printf("+"); 165 } 166 for(i=1; i<8; i++) 167 { 168 SetConsoleTextAttribute(hClock,FOREGROUND_INTENSITY | FOREGROUND_GREEN | 169 BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_RED); 170 if(0==timenow->tm_sec) 171 { 172 GotoXY(hClock,CENTER_X + i*2*sin(PI*(min_degree-6)/180), CENTER_Y - i*cos(PI*(min_degree-6)/180)); 173 printf(" "); 174 } 175 GotoXY(hClock,CENTER_X + i*2*sin(PI*min_degree/180), CENTER_Y - i*cos(PI*min_degree/180)); 176 printf("*"); 177 } 178 for(i=1; i<5; i++) 179 { 180 SetConsoleTextAttribute(hClock,FOREGROUND_INTENSITY | FOREGROUND_RED | 181 BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_RED); 182 if(0==timenow->tm_min) 183 { 184 GotoXY(hClock,CENTER_X + i*2*sin(PI*(hour_degree-30)/180), CENTER_Y - i*cos(PI*(hour_degree-30)/180)); 185 printf(" "); 186 } 187 GotoXY(hClock,CENTER_X + i*2*sin(PI*hour_degree/180), CENTER_Y - i*cos(PI*hour_degree/180)); 188 printf("#"); 189 190 } 191 } 192 } 193 } 194 195 void PrintTime(HANDLE hClock, time_t * p_rawtime) 196 { 197 struct tm * timenow = localtime(p_rawtime); 198 SetConsoleTextAttribute(hClock,FOREGROUND_INTENSITY | FOREGROUND_BLUE | 199 BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_RED); 200 GotoXY(hClock,TIME_OFFSET_X,TIME_OFFSET_Y); 201 printf("当前系统时间:"); 202 GotoXY(hClock,TIME_OFFSET_X,TIME_OFFSET_Y + 1); 203 printf("%4d 年 %2d 月 %2d 日",1900+timenow->tm_year,timenow->tm_mon,timenow->tm_mday); 204 GotoXY(hClock,TIME_OFFSET_X,TIME_OFFSET_Y + 2); 205 printf("%4d 时 %2d 分 %2d 秒",timenow->tm_hour,timenow->tm_min,timenow->tm_sec); 206 }