一个c++时间显示器

#include <iostream>
#include <iomanip>
#include <unistd.h>
#include <time.h>
#include <cstdio>
using namespace std;

class Clock{
public:
Clock(time_t t){
    tm* local = localtime(&t);
    m_hour = local->tm_hour;
    m_min = local->tm_min;
    m_sec = local->tm_sec;
}
void run(void) {
    while(1) {
        cout << setw(2) << setfill('0') << m_hour << ":";
        cout << setw(2) << setfill('0') << m_min << ":";
        cout << setw(2) << setfill('0') << m_sec << '\r';
        fflush(stdout);
        if (++m_sec == 60) {
            m_sec = 0;
            if (++m_min == 60) {
                m_min = 0;
                if (++m_hour == 24) {
                    m_hour = 0;
                }
            }
        }
        sleep(1);
    }
}
private:
int m_hour;
int m_min;
int m_sec;
};

int main(void) {
time_t t;
time(&t);
Clock c(t);
c.run();
return 0;
}

猜你喜欢

转载自blog.csdn.net/u011955875/article/details/81347084