Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use C language to realize timer

2025-01-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article focuses on "how to use C language to achieve timer", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "how to use C language to implement timer".

Realization idea

A brief introduction to my implementation ideas:

This article includes three versions, namely, the very simple version, the ordinary version and the advanced version:

Very simple version: simple single countdown, can only set the number of seconds, timing through the Sleep () function (rough countdown), first enter the number of seconds cnt, and then every second (Sleep (1000), cnt minus 1, until cnt becomes 0, exit the program.

Ordinary version: can set timing hours, minutes, seconds (hour,min,sec), timing through the time () function (used to obtain the number of seconds of the current system time), enter the while cycle to determine whether the number of seconds of the system time has changed, if there is any change, it means that 1 second has passed. At this time, the number of seconds sec minus 1. When sec is reduced to 0, the minute min is subtracted by 1 in the next second, while sec is changed to 59. The relationship between minute min and hour hour is similar to that of seconds and minutes. Keep cycling the countdown until both hour,min and sec change to 0, when the clock is over and give an alarm (printf (\ a')).

Advanced version: add repeated timing and pause timing on the basis of the regular version. First of all, you need to set the timing time, then press the space bar to start the timing, press the space bar again, you can pause the countdown, and the countdown time set at the same time has the function of memory, that is, after the timing is over, press the space again. It is still timed according to the timing set last time.

1. Minimalist version

The minimalist version of the code is very simple, so there is no need to introduce it.

# include # include / / Sleep (ms) int main () {int cnt = 0; puts ("input seconds"); scanf ("% d", & cnt); while (cnt > = 0) {Sleep (1000); printf ("% d\ r", cnt); cnt--;} printf ("timing end\ n"); return 0;} running effect

two。 Ordinary version

The normal version is also very simple, mainly including:

Set timing countdown buzzer alarm (system tone) # include # include int main (void) {int hour = 0, min = 0, sec = 0; time_t time_sec = 0; time_t old_sec = 0; printf ("Please set timing-hours, minutes and seconds\ n"); scanf ("% d%d%d", & hour, & min, & sec); time (& time_sec) / / get the current seconds (1970-1-1 00:00:00 to now) printf ("countdown-d:d:d\ r", hour, min, sec); old_sec = time_sec; / / update the old seconds while (hour > 0 | | min > 0 | | sec > 0) {time (& time_sec) / / get the number of seconds saved to the time_t variable if (time_sec! = old_sec) / / if the number of seconds changes (the timing reaches 1 second) {old_sec = time_sec; / / update the old number of seconds if (sec > 0) sec-- / / timing seconds minus 1 else {sec = 59; / / if the original seconds is 0, it becomes 59 if (min > 0) min--; / / timing minutes minus 1 else {min = 59 / / if the number of minutes is 0, it becomes 59 hour--; / / hours minus 1}} printf ("countdown-d:d:d\ r", hour, min, sec);} for (int I = 0; I)

< 5; i++) { printf("\a"); //响蜂鸣器(或系统提示音) sleep(1); //延时1秒,太短电脑不发出声音 } puts("\n计时结束\n"); return 0;}运行效果 3.高级版本 在普通版本的基础上添加了菜单打印和键盘按键扫描两部分代码。 程序的核心为 pause_flag和menu_flag这两个标志,前者控制倒计时的开始,暂停与继续,后者控制菜单是否需要刷新(只有程序第一次运行、设置计时时间或计时结束时才需要打印菜单)。 #include #include #include //kbhit()/_kbhit(),getch()#include //Sleep(ms)int main(void){ //动态计时参数(打印值) unsigned int hour = 0, min = 0, sec = 0; //默认值(设定的计时参数) unsigned int hour_def = 0, min_def = 0, sec_def = 0; time_t time_sec = 0; time_t old_sec = 0; char key; int pause_flag = 1; //暂停标志 int menu_flag = 1; //菜单打印标志 while(1) { /**************** 菜单打印 ****************/ if(menu_flag == 1) { menu_flag = 0; system("cls"); //Windows系统清屏命令 printf("================================\n"); //菜单 if(hour_def || min_def || sec_def) printf("|\t倒计时时间:d:d:d\t |\n",\ hour_def, min_def, sec_def); else printf("|\t未设置计时时间\t\t|\n"); printf("| S:设置 空格:开始/暂停 Q:退出 |\n"); printf("================================\n"); if(pause_flag == 0) { printf("计时结束\n"); for(int i = 0; i < 5; i++) { printf("\a"); //响蜂鸣器(或系统提示音) sleep(1); //延时1秒,太短电脑不发出声音 } pause_flag = 1; //停止计时 } } /**************** 键盘按键扫描+操作 ****************/ key = 0; if(_kbhit()) //检测到按键按下 key = getch(); //读取按键 switch(key) { case 's': //按s/S设置计时时间 case 'S': printf("请设置计时时间--时 分 秒\n"); scanf("%d %d %d", &hour_def, &min_def, &sec_def); if(hour_def >

24 | | min_def > 59 | | sec_def > 59) {/ / printf ("time setting failed\ n"); hour_def = min_def = sec_def = 0; / / reset time} hour = min = sec = 0; / / time value zero menu_flag = 1 / / print menu break Case'': / / Press Spacebar to start / pause timing if (hour_def | | min_def | | sec_def) / / if the time is valid {if (pause_flag) {pause_flag = 0 / / start timing or continue timing if (! hour & &! min & &! sec) / / count to 0 {hour = hour_def; / / read the last set time min = min_def Sec = sec_def; / / print initial time printf ("countdown-d:d:d\ r",\ hour, min, sec) }} else pause_flag = 1; / & pause timing time (& time_sec); / / get current seconds (1970-1-1 00:00:00 to now) old_sec = time_sec / / Update the old number of seconds} break; case'qseconds: case'Qseconds: printf ("Program exits\ n"); return 0;} / * timing operation * / time (& time_sec) / / get the number of seconds saved to the time_t variable if (pause_flag = = 0 & & old_sec! = time_sec) {old_sec = time_sec; / / update the old seconds if (sec > 0) sec-- / / time seconds minus 1 else {sec = 59; / / if the original seconds is 0, it becomes 59 if (min > 0) min-- / / timed minutes minus 1 else {min = 59; / / if the number of minutes is 0, it becomes 59 hour-- / / timing hours minus 1}} printf ("countdown-d:d:d\ r", hour, min, sec); if (! hour & &! min & &! sec) / / timing ends menu_flag = 1; / / print menu}} return 0;} running effect

At this point, I believe you have a deeper understanding of "how to use C language to achieve timer". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report