當前位置:編程學習大全網 - 編程軟體 - c++編程定義壹時鐘類Clock

c++編程定義壹時鐘類Clock

#include <iostream> using namespace std; //時間類 class Time{ private: int hour; int minute; int second; public: //設置時間 void set(int h,int m,int s){ hour = h; minute = m; second = s; } //時間走壹秒,時分秒的變化情況 void next(){ if(second<59) second++; else if(minute<59){ second=0; minute++;} else if(hour<23){ minute=0; hour++;} else hour=0; } //得到時間 int get(){ return hour*10000+minute*100+second; } }; //時鐘類 class Clock{ private: Time now; Time ring_time; public: //對表,設定初始時間 void adjust_now(int h,int m,int s){ now.set(h,m,s); cout<<"現在的時間是:"<<h<<"時"<<m<<"分"<<s<<"秒"<<endl; } //設定鬧鈴時間 void adjust_ring(int h,int m,int s){ ring_time.set(h,m,s); cout<<"鬧鈴時間是:"<<h<<"時"<<m<<"分"<<s<<"秒"<<endl; } //時間過壹秒 void tick(){ long int old=time(0); while(time(0)==old) ; now.next(); } //顯示當前時間 void showtime(){ cout<<now.get()<<endl; } //時鐘開始走時,等到了鬧鈴時間,開始響 void run(){ do{ tick(); showtime(); if(now.get()>=ring_time.get()) cout<<'\a'; }while(1); } }; int main(){ Clock c; c.adjust_now(18,35,40); //起始時間 c.adjust_ring(18,35,45); //鬧鈴時間 c.run(); }

  • 上一篇:電信戰略轉型3.0中三大方向是什麽?
  • 下一篇:我想問壹下怎麽制作遊戲,編寫代碼什麽的,詳細點開看看吧~希望大神們能認真回答
  • copyright 2024編程學習大全網