oop思想实现数字时钟
#include "stdafx.h"
#include <iostream>
using namespace std;
#include <time.h>
#include <iomanip>
#include <windows.h>
//初始化的数据来自系统 以后的逻辑运算及显示自实现
class Clock
{
public:
Clock()
{
//取得现在的时间
time_t t = time(NULL);
struct tm ti = *localtime(&t);
//struct tm * __CRTDECL localtime(const time_t * _Time)
hour = ti.tm_hour;
min = ti.tm_min;
sec = ti.tm_sec;
}
void run()
{
while (1)
{
show(); //完成显示
tick(); //完成数据更新
}
}
private:
void show()
{
system("cls"); //刷新屏幕
cout << setw(2) << setfill('0') << hour << ":";
cout << setw(2) << setfill('0') << min << ":";
cout << setw(2) << setfill('0') << sec;
}
/*void tick()
{
Sleep(1000);
if (++sec == 60)
{
sec = 0;
min++;
if (++min == 60)
{
min = 0;
hour++;
if (++hour == 24)
{
hour = 0;
}
}
}
}*/
void tick()
{
Sleep(1000); //Sleep的S要大写
sec++;
if (sec == 60)
{
sec = 0;
min++;
}
if (min == 60)
{
min = 0;
hour++;
}
if (hour == 24)
{
hour = 0;
}
}
int hour;
int min;
int sec;
};
int _tmain(int argc, _TCHAR* argv[])
{
Clock c;
c.run();
return 0;
}