brief introduction
A simple analog time display, the principle is to use the time function to obtain the current time , and then through the time update function and the time display function to simulate the time update, the alternate display of time uses the clear screen function system ("CLS");
head File
#include <iostream>
#include <time.h>
using namespace std;
class Curtime {
int h;
int m;
int s;//时 分 秒
public:
void SetTime();//设置起始时间
void DisplayTime();//显示时间
void UpdateTime();//更新时间
};
Function implementation
#include "disTime.h"
void Curtime::SetTime() {
time_t tt = time(NULL);
tm* t = localtime(&tt);
h = t->tm_hour;
m = t->tm_min;
s = t->tm_sec;
}
void Curtime::DisplayTime() {
system("cls");
cout << "当前时间:" << h << " 时" << " " << m << " 分" << s << " 秒";
}
void Curtime::UpdateTime() {
_sleep(1000);//模拟延时
if (++s == 60) {
s = 0;
if (++m == 60) {
m = 0;
if (++h == 24)
h = 0;
}
}
}
Function usage
#include <iostream>
#include"disTime.h"
using namespace std;
int main() {
Curtime tm;
tm.SetTime();
for (int i = 0; i < 100; i++) {
tm.DisplayTime();
tm.UpdateTime();
}
return 0;
}