3 回答

TA貢獻(xiàn)1811條經(jīng)驗 獲得超6個贊
#include <iostream>
using namespace std;
//時間類
class Time{
private:
int hour;
int minute;
int second;
public:
//設(shè)置時間
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:
//對表,設(shè)定初始時間
void adjust_now(int h,int m,int s){
now.set(h,m,s);
cout<<"現(xiàn)在的時間是:"<<h<<"時"<<m<<"分"<<s<<"秒"<<endl;
}
//設(shè)定鬧鈴時間

TA貢獻(xiàn)1847條經(jīng)驗 獲得超7個贊
#include <iostream> using namespace std; //時間類 class Time{ private: int hour; int minute; int second; public: //設(shè)置時間 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: //對表,設(shè)定初始時間 void adjust_now(int h,int m,int s){ now.set(h,m,s); cout<<"現(xiàn)在的時間是:"<<h<<"時"<<m<<"分"<<s<<"秒"<<endl; } //設(shè)定鬧鈴時間 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(); } //顯示當(dāng)前時間 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 關(guān)注
- 1427 瀏覽
添加回答
舉報