自己编写的C语言实时时钟代码

来源:互联网 发布:阿里云大数据开发平台 编辑:程序博客网 时间:2024/05/18 03:29

子函数shijian.c

#include <time.h>void shijian(){struct tm *t;char week[3]="/0";time_t timer;time(&timer);t=localtime(&timer);/**********************************/ /*   本身localtime输出的周为数字 *//*   是0~6分别表示周日到周六,   *//*   阅读不便,在此做个小优化    */ /*********************************/ switch(t->tm_wday){case 1:strcpy(week,"Mon");break;case 2:strcpy(week,"Tue");break;case 3:strcpy(week,"Wed");break;case 4:strcpy(week,"Thu");break;case 5:strcpy(week,"Fri");break;case 6:strcpy(week,"Sat");break;default:strcpy(week,"Sun");}printf("YEAR \t MONTH \t DAY \t    TIME \t WEEK\n");printf("%d \t   %02d    %02d  \t  %02d:%02d:%02d \t  %s\n",1900+t->tm_year,1+t->tm_mon,t->tm_mday,t->tm_hour,t->tm_min,t->tm_sec,week);}


子函数sleep.c

#include <windows.h>/************************/ /*   Sleep单位是毫秒    */ /************************/ void sleep(unsigned long sec){Sleep(sec);}

.h文件

#ifdef  _SHIJIAN_H#define _SHIJIAN_Hvoid shijian()#endif#ifdef  _SLEEP_H #define _SLEEP_Hextern void sleep(unsigned long sec)#endif


主函数main.c

#include <stdio.h>#include <conio.h>#include "sleep.h"int main(){while(!kbhit()){shijian();sleep(1000);system("cls");}return 0;}


键盘输入中断程序,运行结果如下

0 0
原创粉丝点击