您好,登錄后才能下訂單哦!
用面向對象思想實現時鐘C++描述的實例代碼:
# include <iostream> # include <time.h> # include <iomanip> # include <windows.h> //# include <unistd.h> using namespace std; //初始化的數據來自系統,以后的邏輯運算及顯示自實現 class Clock { public: Clock() { time_t t = time(NULL); struct tm ti = *localtime(&t); hour = ti.tm_hour; min = ti.tm_min; sec = ti.tm_sec; } void run() { while (1) { system("cls"); 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(1); if (++sec == 60) { sec = 0; min += 1; if (++min == 60) { min = 0; hour += 1; if (++hour == 24) { hour = 0; } } } } int hour = 0; int min = 0; int sec = 0; }; int main(void) { Clock c; c.run(); cout << " Hello World " << endl; return 0; }
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。