使用c语言制作一个控制台的电子时钟,供大家参考,具体内容如下
学习了c语言基本语法后,在学习了time.h的库文件,让我产生了想制作一款电子时钟的念头,那好就开始动手操作吧。
使用到下面这些技术:
首先必须先导入库
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
/***************** 实时数字时钟(和计算机系统时间关联) ************** #include <time.h> -- 必须的时间函数头文件 time_t -- 时间类型( time .h 定义) struct tm -- 时间结构, time .h 定义如下:(依需求选用) int tm_sec; int tm_min; int tm_hour; int tm_mday; int tm_mon; int tm_year; int tm_wday; int tm_yday; int tm_isdst; time (&rawtime); -- 获取时间,以秒计,从1970年1月一日起算,存于rawtime -- 获取到当前的秒数,参数为0则函数返回值即为结果 localtime (&rawtime); -- 转为当地时间, tm 时间结构 system ( "cls" );--命令行清屏 |
获取坐标的代码如下
1
2
3
4
5
6
7
8
|
#include <windows.h> void gotoxy( int x, int y) //光标定位函数,需要包含windos.h头文件 { COORD coord; coord.X=x; coord.Y=y; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),coord); } |
源代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
|
#include <stdio.h> #include <time.h> #include <stdlib.h> #include <conio.h> #include <windows.h> void gotoxy( int x, int y) //光标定位函数,需要包含windos.h头文件 { COORD coord; coord.X=x; coord.Y=y; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),coord); } void dians(){ int x=8; gotoxy(x*3,8); printf ( " **" ); gotoxy(x*3,9); printf ( " **" ); gotoxy(x*3,11); printf ( " **" ); gotoxy(x*3,12); printf ( " **" ); gotoxy(x*6,8); printf ( " **" ); gotoxy(x*6,9); printf ( " **" ); gotoxy(x*6,11); printf ( " **" ); gotoxy(x*6,12); printf ( " **" ); } void draw_numb( int x, int shu){ //判断0-9的数据,通过gotoxy显示出来 if (shu==0){ gotoxy(x,6); printf ( "*****" ); gotoxy(x,7); printf ( "* *" ); gotoxy(x,8); printf ( "* *" ); gotoxy(x,9); printf ( "* *" ); gotoxy(x,10); printf ( "* *" ); gotoxy(x,11); printf ( "* *" ); gotoxy(x,12); printf ( "* *" ); gotoxy(x,13); printf ( "* *" ); gotoxy(x,14); printf ( "*****" ); } if (shu==1){ gotoxy(x,6); printf ( " * " ); gotoxy(x,7); printf ( " * " ); gotoxy(x,8); printf ( " * " ); gotoxy(x,9); printf ( " * " ); gotoxy(x,10); printf ( " * " ); gotoxy(x,11); printf ( " * " ); gotoxy(x,12); printf ( " * " ); gotoxy(x,13); printf ( " * " ); gotoxy(x,14); printf ( " * " ); } if (shu==2){ gotoxy(x,6); printf ( "*****" ); gotoxy(x,7); printf ( " *" ); gotoxy(x,8); printf ( " *" ); gotoxy(x,9); printf ( " *" ); gotoxy(x,10); printf ( "*****" ); gotoxy(x,11); printf ( "* " ); gotoxy(x,12); printf ( "* " ); gotoxy(x,13); printf ( "* " ); gotoxy(x,14); printf ( "*****" ); } if (shu==3){ gotoxy(x,6); printf ( "*****" ); gotoxy(x,7); printf ( " *" ); gotoxy(x,8); printf ( " *" ); gotoxy(x,9); printf ( " *" ); gotoxy(x,10); printf ( "*****" ); gotoxy(x,11); printf ( " *" ); gotoxy(x,12); printf ( " *" ); gotoxy(x,13); printf ( " *" ); gotoxy(x,14); printf ( "*****" ); } if (shu==4){ gotoxy(x,6); printf ( "* *" ); gotoxy(x,7); printf ( "* *" ); gotoxy(x,8); printf ( "* *" ); gotoxy(x,9); printf ( "* *" ); gotoxy(x,10); printf ( "*****" ); gotoxy(x,11); printf ( " *" ); gotoxy(x,12); printf ( " *" ); gotoxy(x,13); printf ( " *" ); gotoxy(x,14); printf ( " *" ); } if (shu==5){ gotoxy(x,6); printf ( "*****" ); gotoxy(x,7); printf ( "* " ); gotoxy(x,8); printf ( "* " ); gotoxy(x,9); printf ( "* " ); gotoxy(x,10); printf ( "*****" ); gotoxy(x,11); printf ( " *" ); gotoxy(x,12); printf ( " *" ); gotoxy(x,13); printf ( " *" ); gotoxy(x,14); printf ( "*****" ); } if (shu==6){ gotoxy(x,6); printf ( "*****" ); gotoxy(x,7); printf ( "* " ); gotoxy(x,8); printf ( "* " ); gotoxy(x,9); printf ( "* " ); gotoxy(x,10); printf ( "*****" ); gotoxy(x,11); printf ( "* *" ); gotoxy(x,12); printf ( "* *" ); gotoxy(x,13); printf ( "* *" ); gotoxy(x,14); printf ( "*****" ); } if (shu==7){ gotoxy(x,6); printf ( "*****" ); gotoxy(x,7); printf ( " *" ); gotoxy(x,8); printf ( " *" ); gotoxy(x,9); printf ( " *" ); gotoxy(x,10); printf ( " *" ); gotoxy(x,11); printf ( " *" ); gotoxy(x,12); printf ( " *" ); gotoxy(x,13); printf ( " *" ); gotoxy(x,14); printf ( " *" ); } if (shu==8){ gotoxy(x,6); printf ( "*****" ); gotoxy(x,7); printf ( "* *" ); gotoxy(x,8); printf ( "* *" ); gotoxy(x,9); printf ( "* *" ); gotoxy(x,10); printf ( "*****" ); gotoxy(x,11); printf ( "* *" ); gotoxy(x,12); printf ( "* *" ); gotoxy(x,13); printf ( "* *" ); gotoxy(x,14); printf ( "*****" ); } if (shu==9){ gotoxy(x,6); printf ( "*****" ); gotoxy(x,7); printf ( "* *" ); gotoxy(x,8); printf ( "* *" ); gotoxy(x,9); printf ( "* *" ); gotoxy(x,10); printf ( "*****" ); gotoxy(x,11); printf ( " *" ); gotoxy(x,12); printf ( " *" ); gotoxy(x,13); printf ( " *" ); gotoxy(x,14); printf ( "*****" ); } } void draws( char wei, int shu){ //这里定义了6个位置 分别是小时的个位十位,分钟的个位十位和秒钟的个位十位 int x=8; if (wei== '1' ){ draw_numb(x*1,shu); //这里调用了 draw_numb函数吧x*1是横坐标(也表示第几个位置数),shu是要显示的数据调过去 } //x*1表示第一个位置 if (wei== '2' ){ draw_numb(x*2,shu); } if (wei== '3' ){ draw_numb(x*4,shu); } if (wei== '4' ){ draw_numb(x*5,shu); } if (wei== '5' ){ draw_numb(x*7,shu); } if (wei== '6' ){ draw_numb(x*8,shu); } } int main() { system ( "color 1b" ); struct tm *curtime; //结构tm,结构指针curtime,time.h中定义 time_t t; //时间类型变量t,time.h中定义 clock_t start; //结构clock_t,结构变量start,time.h中定义 double th_hour,th_min,th_sec; do { dians(); t= time (0); //获取到当前的秒数,参数为0则函数返回值即为结果 curtime= localtime (&t); //得到当前系统时间/ if (( double )curtime->tm_hour<=12) //午前的处理/ { gotoxy(5,3); printf ( "AM " ); //if((double)curtime->tm_hour<10) draws('1',0); //十点之前在小时数前加零 draws( '1' ,(( int )curtime->tm_hour)/10); draws( '2' ,(( int )(( double )curtime->tm_hour))%10); } else //午后的处理 { gotoxy(5,3); printf ( "PM " ); //if((double)curtime->tm_hour-12<10) draws('1',0);//输入0 draws( '1' ,( int )curtime->tm_hour/10); draws( '2' ,(( int )(( double )curtime->tm_hour))%10); } if (( double )curtime->tm_min<10) draws( '3' ,0); draws( '3' ,( int )curtime->tm_min/10); draws( '4' ,( int )curtime->tm_min%10); if (( double )curtime->tm_sec<10) draws( '5' ,0); draws( '5' ,( int )curtime->tm_sec/10); draws( '6' ,( int )curtime->tm_sec%10); start= clock (); while ( clock ()-start<500); //等待延时1000ms system ( "cls" ); } while (!kbhit()); //按任一键退出 return 0;} |
最后运行截图(完美运行)
是不是满满的成就感! 好了今天就分享到这了。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/qq_25755645/article/details/97375983