【问题描述】
编写一个程序,定义一个时间类Time,包含三个属性: hour, minute 和 second
要求通过运算符重载实现如下功能:
-
时间输入输出(>>、<<);
-
时间增加减少若干(+=、-=),例:Time& operator+=(const Time&);Time& operator-=(const Time&);
-
时间前、后自增加/减少1秒(++、--),前自增例:Time& operator++(); 后自增例:Time operator++(int);
【输入形式】
-
输入固定为两个Time实例(time1,time2),每个实例占一行;
-
Time实例输入格式为:hour minute second。
【输出形式】
-
Time实例输出格式为:hour:minute:second;
-
每个输出实例占一行。
-
依次输出以下表达式的值
-
time1 += (time2++)
-
time1 -= time2
-
++time2
-
time2 += (time1--)
-
--time1
-
time2 -= time1
【样例输入】
21 10 35
10 15 25
【样例输出】
07:26:00
21:10:34
10:15:27
07:26:01
21:10:32
10:15:29
【样例说明】
-
不要显示多余的提示信息,避免输出判定错误。
-
输出结束后不要输出任何内容,包括空格和换行。
-
注意判断输出信息是否符合要求。
#include <iostream>
using namespace std;
class Clock {
public: //外部接口
Clock(int NewH = 0, int NewM = 0, int NewS = 0);
Clock operator++(); //前置单目运算符重载
Clock operator++(int); //后置单目运算符重载
Clock operator--(); //前置单目运算符重载
Clock operator--(int); //后置单目运算符重载
Clock operator+=(const Clock &c2); //+运算符重载
Clock operator-=(const Clock &c2); //-运算符重载
friend istream & operator>> (istream &in, Clock &c);
friend ostream & operator<< (ostream &out, Clock &c);
private: //私有数据成员
int Hour, Minute, Second;
};
Clock::Clock(int NewH, int NewM, int NewS) {
if ((0 <= NewH && NewH < 24) && (0 <= NewM && NewM < 60) && (0 <= NewS && NewS < 60)) {
this->Hour = NewH;
this->Minute = NewM;
this->Second = NewS;
} else {
cout << "Time error!" << endl;
}
}
Clock Clock::operator++() {
Second++;
if (Second >= 60) {
Second = 0;
Minute++;
if (Minute >= 60) {
Minute = 0;
Hour++;
if (Hour >= 24) {
Hour = 0;
}
}
}
return *this;
}
Clock Clock::operator++(int) {
Clock old = *this;
++(*this);
return old;
}
Clock Clock::operator--() {
Second--;
if (Second < 0) {
Second = 59;
Minute--;
if (Minute < 0) {
Minute = 59;
Hour --;
if (Hour < 0) {
Hour = 23;
}
}
}
return *this;
}
Clock Clock::operator--(int) {
Clock old = *this;
--(*this);
return old;
}
Clock Clock::operator+=(const Clock &c2) {
this->Second += c2.Second;
this->Minute += c2.Minute;
this->Hour += c2.Hour;
if (this->Second >= 60) {
this->Minute += this->Second / 60;
this->Second %= 60;
}
if (this->Minute >= 60) {
this->Hour += this->Minute / 60;
this->Minute %= 60;
}
if (this->Hour >= 24) {
this->Hour = (Hour % 24);
}
return *this;
}
Clock Clock::operator-=(const Clock &c2) {
this->Second -= c2.Second;
if (this->Second < 0) {
this->Second += 60;
this->Minute--;
}
this->Minute -= c2.Minute;
if (this->Minute < 0) {
this->Minute += 60;
this->Hour--;
}
this->Hour -= c2.Hour;
if (this->Hour < 0) {
this->Hour += 24;
}
return *this;
}
istream &operator >> (istream &in, Clock &c) {
in >> c.Hour >> c.Minute >> c.Second;
return in;
}
ostream &operator << (ostream &out, Clock &c) {
string hour = to_string(c.Hour);
string minute = to_string(c.Minute);
string second = to_string(c.Second);
if (c.Hour < 10) {
hour = "0" + to_string(c.Hour);
}
if (c.Minute < 10) {
minute = "0" + to_string(c.Minute);
}
if (c.Second < 10) {
second = "0" + to_string(c.Second);
}
out << hour << ":" << minute << ":" << second << endl;
return out;
}
int main() {
Clock time1, time2;
cin >> time1;
cin >> time2;
time1 += (time2++);
cout << time1;
time1 -= time2;
cout << time1;
++time2;
cout << time2;
time2 += (time1--);
cout << time2;
--time1;
cout << time1;
time2 -= time1;
cout << time2;
return 0;
}