5-2 時間模擬
阿新 • • 發佈:2017-06-11
pan get ont 時間 operator right 測試 謝謝 第一個
給出下面的基類Time的框架如下:
class Time
{
protected:
int second;
int minute;
int hour;
public:
void operator++();
void operator--();
}
建立一個派生類Time_12hours,用於表示十二進制時間,增加以下成員數據:
string type;//標識為12進制時間,type=”12-hours-time”
string interval;//標識為AM或者PM,interval=”AM”或interval=”PM”
增加以下成員函數: void operator++();
void operator--();
建立一個派生類Time_24hours,用於表示二十四進制時間,增加以下成員數據:
string type;//標識為24進制時間,type=”24-hours-time”
增加以下成員函數:
void operator++();
void operator--();
生成上述類並編寫主函數,根據輸入的初始時間信息、自增或者自減類型、自增或者自減次數,輸出其最後時間信息。
輸入格式:測試輸入包含多個測試用例,一個測試用例為一行,每行共五個數字,第一個數字為進制,121表示輸入為12進制AM時間,122表示輸入為12進制PM時間,輸入為24表示輸入為24進制時間,第二個數字為hour,第三個數字為minute,第四個數字為second,第五個字符為運算類型,+表示自增,-表示自減,第六個數字為運算次數,0表示測試用例結束。
輸入樣例:
121 11 59 59 + 3
24 11 59 59 + 3
122 11 59 59 + 3
122 00 00 00 - 3
121 00 00 00 - 5
24 00 00 00 - 1
0
輸出樣例:
PM 00:00:02
12:00:02
AM 00:00:02
AM 11:59:57
PM 11:59:55
23:59:59
參考代碼:
#include<iostream> #include<iomanip> using namespace std; //建立Time基類 class Time{ protected: int second; int minute; inthour; int total; public: void operator ++ (); void operator -- (); friend istream& operator >>(istream& , Time& ); }; istream& operator >> (istream& set, Time& x){ set>> x.hour>> x.minute>> x.second; x.total = (x.hour*60+ x.minute)*60 + x.second; return set; } void Time::operator ++(){ total++; second = total%60;//對60秒取余數 minute = total/60%60;//換為分對60分取余數 hour = total/3600%24;//換為小時對24小時取余數 } void Time::operator --(){ if(total==0) total=86400;//到前一天,保證sum大於零 total--; second = total%60; minute = total/60%60; hour = total/3600%24; } //建立Time_12hours class Time_12hours:public Time{ private: string type;//12-hours-time string interval;//AM ro PM1 public: void set(int x){if(x == 122) total += 43200;}; void operator --(){Time::operator --();}; void operator ++(){Time::operator ++();}; friend ostream& operator <<(ostream& ,Time_12hours &); }; ostream& operator <<(ostream& put,Time_12hours &x) { x.type="12-hours-time "; if(x.hour>=12) { x.interval="PM "; x.hour-=12; } else { x.interval="AM "; } put <<x.interval <<setiosflags(ios::right)<<setfill(‘0‘) <<setw(2)<<x.hour<<":" <<setw(2)<<x.minute<<":" <<setw(2)<<x.second<<endl; } //建立Time_24hours class Time_24hours:public Time{ string type; public: void operator ++(){Time::operator ++();}; void operator --(){Time::operator --();}; friend ostream& operator <<(ostream& , Time_24hours &); }; ostream& operator <<(ostream& put, Time_24hours& x){ put <<setiosflags(ios::right)<<setfill(‘0‘) <<setw(2)<<x.hour<<":" <<setw(2)<<x.minute<<":" <<setw(2)<<x.second<<endl; } int main(){ Time_12hours t12; Time_24hours t24; int type; char flag; int n; while( cin>>type ) { if( type == 0 )break; else if( type >100 ) { cin>>t12; t12.set(type); getchar(); cin>>flag>>n; if(flag==‘+‘) while(n--) ++t12; else while(n--) --t12; cout<<t12; } else { cin>>t24; getchar(); cin>>flag>>n; if(flag == ‘+‘) while(n--) ++t24; else while(n--) --t24; cout<<t24; } } return 0; }
歡迎指教,一起學習!
未經本人允許,請勿轉載!
謝謝!
5-2 時間模擬