本文实例讲述了C++实现当前时间动态显示的方法。分享给大家供大家参考。具体如下:
//struct tm {
// 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;
// };
#include
#include
#include "dos.h"
#include
using namespace std;
int main()
{
char *myweek[]={"日","一","二","三","四","五","六"};
time_t nowtime; //typedef long time_t;在编译器定义的头文件中
nowtime = time(NULL); //获取当前时间 此时它是用一个长整形表示的
struct tm *local;
local = localtime(&nowtime); //获取当前系统时钟
while (1)
{
cout<<"当前时间:";
cout<tm_year+1900<<"年"<tm_mon+1<<"月"<tm_mday<<"日"<<" ";
cout<tm_hour<<"时"<tm_min<<"分"<tm_sec<<"秒"<<" ";
cout<<"星期"<tm_wday]<tm_sec==59 && local->tm_min!=59)
//当秒到59,分未到59时 分钟加1,秒清0
{
local->tm_min++;
local->tm_sec=0;
}
//当秒和分都为59 时不为23时 ,秒和分钟都清0,时钟加1
else if(local->tm_sec==59 && local->tm_min==59 && local->tm_hour!=23)
{
local->tm_min=0;
local->tm_sec=0;
local->tm_hour++;
}
//当秒和分都为59 时为23时 ,秒,分钟和时钟都清0
else if(local->tm_sec==59&&local->tm_min==59&&local->tm_hour==23)
{
local->tm_sec=0;
local->tm_min=0;
local->tm_hour=0;
}
else //其它情况秒钟进行不断加1
{
local->tm_sec++;
}
Sleep(1000);
}
system("pause");
return 0;
}
希望本文所述对大家的C++程序设计有所帮助。



