服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C/C++ - C语言实现基于控制台的电子时钟

C语言实现基于控制台的电子时钟

2022-11-21 15:16码来的小朋友 C/C++

这篇文章主要为大家详细介绍了C语言实现基于控制台的电子时钟,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

使用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;}

最后运行截图(完美运行)

C语言实现基于控制台的电子时钟

是不是满满的成就感! 好了今天就分享到这了。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/qq_25755645/article/details/97375983

延伸 · 阅读

精彩推荐
  • C/C++cmake 学习笔记

    cmake 学习笔记

    这篇文章主要介绍了作者学习cmake 的相关资料与心得,有需要的小伙伴可以参考下...

    thinkpp4532021-05-24
  • C/C++深入理解C++内链接与外链接的意义

    深入理解C++内链接与外链接的意义

    链接描述了名称在整个程序或一个翻译单元中如何引用或不引用同一实体,下面这篇文章主要给大家介绍了关于C++内链接与外链接意义的理解,需要的朋友可...

    Jerish_C11162022-02-24
  • C/C++C语言绘制余弦、正弦曲线

    C语言绘制余弦、正弦曲线

    这篇文章主要为大家详细介绍了C语言绘制余弦、正弦曲线的相关代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    修炼千年的大饼4492021-07-30
  • C/C++C++实现中缀表达式转化为后缀表达式详解

    C++实现中缀表达式转化为后缀表达式详解

    这篇文章主要为大家详细介绍了如何利用C++解决实现中缀表达式转换为后缀表达式的问题,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的...

    玄澈_7432022-10-26
  • C/C++C语言实现双人五子棋游戏

    C语言实现双人五子棋游戏

    这篇文章主要为大家详细介绍了C语言实现双人五子棋游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    两片空白7532021-11-12
  • C/C++C++ 中malloc()和free()函数的理解

    C++ 中malloc()和free()函数的理解

    这篇文章主要介绍了C++ 中malloc()和free()函数的理解的相关资料,这里提供用法示例帮助大家理解这部分知识,需要的朋友可以参考下...

    小小白杨1237802021-05-27
  • C/C++C++ decltype用法举例说明

    C++ decltype用法举例说明

    decltype是C++11添加的一个新的关键字,目的是选择并返回操作数的数据类型,重要的是,在此过程中编译器分析表达式并得到它的类型,却不实际计算表达式...

    qq_381969824032021-11-23
  • C/C++Qt实现闹钟小程序

    Qt实现闹钟小程序

    这篇文章主要为大家详细介绍了Qt实现闹钟小程序,利用Qt的designer设计需要的闹钟界面,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    飘云之下9962021-08-01