(C/C++学习)12.获取系统时间制作时钟(system()略解)

说明:通过调用函数来获取系统当前时间,并制作一个数字式的时钟,时钟的显示包括年、月、日、小时、分以及秒,通过系统屏幕的刷新来对不断更新的时间进行屏幕的显示。

一.对相关函数的学习

1.time_t time(time_t *second);

a.头文件:<time.h>

b.该函数返回自纪元1970-01-01 00:00:00 起经过的秒数,这是一个很大的数,若 second 不为NULL,则返回值将自动存储在该指针中。

注意:此处的 time_t 以及 __time32_t 都是 long 的别名,其实也就是long int 变量类型。

2.struct tm * localtime (const time_t * timer);

a.头文件:<stdio.h>

b. 该函数的参数为从1990.1.1 00:00:00 到现在累积的秒数,即上个函数的返回值取地址,其返回值为一个结构体类型,其中包含了日期、小时等数据成员;具体见下表:

捕获

3.int kbhit(void);

a.头文件:<conio.h>

b.该函数的功能在于检测当前是否有键盘按键按下,如果有则返回一个非 0 值,否则返回0.

3.void sleep(unsigned second);

a.头文件:<unistd.h>

b.该函数能把进程挂起一段时间,单位为秒,无返回值。

4.void usleep(int micro_second);

a.头文件:<unistd.h>

b.把进程挂起一段时间,单位为微秒,无返回值。这个函数实测在 windows Qt编译器下不准确。(网络上解释:该函数不能用在在windows系统下,只能用于 linux 的测试环境下面。)

注意:usleep() 与sleep() 类似,都用于将进程挂起一段时间。当需延迟时间数量级为秒的时候,尽量使用 sleep(),当为几十毫秒或者更小时,使用 usleep() 更精确。

5.int system(char *command);

a.头文件:<stdlib.h>

b.执行一个 windows 的 DOS 命令。这里主要讲一个命令:清屏。用来定时的刷新显示屏上的系统时间。在windows环境下,该函数为 system(“cls”);而在linux环境下,该函数为system(“clear”);并且,在linux环境下,也可以用如下语句来代替该语句:puts(“33c”),printf(“33c”)。两种方式也存在一定的差别,system()函数在调用命令时会“占用 ”一定的进程时间,如果需要精确的定时会导致定时不准确。而puts(“33c”)和printf(“33c”)则相对来说占用系统进程时间较少,用于定时比较精确。

c.system 的一些其他常用命令

(1.)system(“Date /T”) 为获取当前系统时间并显示,显示方式为 2018/10/17 周三,system(“Time /T”) 为获取当前系统时间并显示,显示方式为 09:56;运行程序后时间不会自动刷新。

(2.)system(“title XX”) 设置命令台窗口的标题为XX。

(3.)system(“mode con cols = n lines = m”) 设置窗口的宽度和高度。

(4.)system(“color 0B”) 设置窗口的颜色,其中color后面的0是背景色代号,A是前景色代号。各颜色代码如下:0=黑色 1=蓝色 2=绿色 3=湖蓝色 4=红色 5=紫色 6=黄色 7=白色 8=灰色 9=淡蓝色 A=淡绿色 B=淡浅绿色 C=淡红色 D=淡紫色 E=淡黄色 F=亮白色。

(5.)system(“shutdown –p”) 为立即关闭计算机;system(“shutdown –l”) 为注销计算机(不关闭);system(“shutdown -s“)关闭计算机(默认一分钟关);system(“shutdown -s –t num“)定时num 秒后关闭计算机,system(“shutdown -r”) 为重启计算机;system(“shutdown -r –t num“) 定时重启计算机(默认一分钟后重启);system (“shutdown -a”) 取消定时任务。

(6.)system("pause") 可以实现冻结屏幕,便于观察程序的执行结果。

(7.)system("del c::xxx.txt") 删除文件。

(8.)system("dir C:\Users\YJ\Desktop\CPlusExample") 查看文件目录的详细信息。

(9.)system(“start C:\xxx\YYY.exe”);执行 YYY.exe,strat 可以省略。

以下程序用到了以上的大部分函数,实现功能:显示当前程序运行的时间,实现关机管理。可以选择定时关机、立即关机、注销(不关机)以及退出此程序等选项,代码如下:

  1 #include<stdio.h>
  2 #include<string.h>
  3 #include<stdlib.h>
  4 int print()
  5 {
  6     printf(" ======================= 
");
  7     printf(" ===C Turn Off Programa===
");
  8     printf("1.Turn off after some minute
");
  9     printf("2.Turn off at once
");
 10     printf("3.Logout
");
 11     printf("4.Exit
");
 12     printf("========================
");
 13     return 0;
 14 }
 15 int main()
 16 {
 17     system("title C Turn Off Program");
 18     system("mode con cols=48 lines=15");
 19     system("color 0B");
 20     system("DAte /T");
 21     system("TiME /T");
 22     char cmd[20]="shutdown -s -t ";
 23     char t[5]="0";
 24     print();
 25     int c;
 26     scanf("%d",&c);
 27     getchar();
 28     switch(c)
 29     {
 30         case 1:printf("The Waiting Time for Turn Off  (0~600)
");
 31         scanf("%s",t);
 32         system(strcat(cmd,t));
 33         int d = 0;
 34         printf("

You can press 1 to end this off system!!
");
 35         scanf("%d",&d);
 36         getchar();
 37         if(d == 1)
 38             system("shutdown -a");
 39         break;
 40         case 2:system("shutdown -p");break;
 41         case 3:system("shutdown -l");break;
 42         case 4:break;
 43         default:printf("Error!
");
 44     }
 45     system("pause");
 46     exit(0);
 47 }
 48 
 49 
查看代码

二.数字式时钟的实现

代码功能:获取系统实时的时间(包括年、月、日、小时、分钟以及秒)等,显示在屏幕上,并刷新屏幕。

  1 #include<time.h>
  2 #include<iostream>
  3 #include<unistd.h>
  4 #include<conio.h>
  5 using namespace std;
  6 int main()
  7 {
  8     while(!kbhit())
  9     {
 10         time_t t = time(NULL);
 11         struct tm *localt = localtime(&t);
 12         system("cls");
 13         cout<<'
'<<"		";
 14         cout<<localt->tm_year+1900<<'\';
 15         cout<<localt->tm_mon+1<<'\';
 16         cout<<localt->tm_mday<<'	';
 17         cout<<localt->tm_hour<<':',
 18         cout<<localt->tm_min<<':',
 19         cout<<localt->tm_sec;
 20         sleep(1);
 21     }
 22     return 0;
 23 }

程序运行结果:

捕获

原文地址:https://www.cnblogs.com/tuihou/p/9789427.html