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

子函数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_H
void shijian()

#endif


#ifdef  _SLEEP_H 
#define _SLEEP_H
extern 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;
}


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

猜你喜欢

转载自blog.csdn.net/endless_fighting/article/details/52229329