Define a time class Time, which has private data members: hour, minute, second, and other member functions: the constructor is used to initialize the data member, the output function, and the operator overload + (plus sign). Write the main function: create a time object, then enter the number of seconds n, and overload + (plus sign) through the operator to calculate the time value after n seconds. The time representation is: hour: minute: second, more than 24 hours Restart timing from 0. The test input contains several test cases, and each test case occupies one line. The input ends when 0 0 0 0 is read, and the corresponding result is not output. If the input time, minute, and second data are invalid, the output: Time Error! N is a non-negative integer, otherwise the output: Input n Error!
Input format:
multi-line input, each line is input in the format of hours, minutes, and seconds n, and the end of the input when reading 0 0 0 0 0 For example: 11 59 40 30 (the time is 11:59:40, the number of seconds n = 30)
Output format: the
time is correct, the time is output in the format of "Time: hour: minute: second" otherwise the error message is output: Time Error! Or Input n Error!
Sample input:
A set of inputs is given here. E.g:
11 59 40 30
0 0 1 59
23 59 40 3011
24 23 40 34
20 69 45 45
10 23 100 34
10 23 34 -23
0 0 0 0
Sample output:
The corresponding output is given here. E.g:
Time:12:0:10
Time:0:1:0
Time:0:49:51
Time Error!
Time Error!
Time Error!
Input n Error!
#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 display() {
cout << "Time:" << hour << ":" << minute << ":" << second << endl;
}
friend Time operator+(Time, int);
};
Time operator+(Time t, int n) {
t.second += n % 60;
// cout<<t.second<<endl;
if (t.second >= 60) {
t.second -= 60;
t.minute += 1;
}
// cout<<t.second<<endl;
t.minute += (n / 60) % 60;
if (t.minute >= 60) {
t.hour += 1;
t.minute -= 60;
}
t.hour += n / 60 / 60;
if (t.hour >= 24) {
t.hour -= 24;
}
return t;
}
int main() {
int a, b, c, d;
Time t;
cin >> a >> b >> c >> d;
do {
if ((a >= 0 && a < 24) && (b >= 0 && b < 60) && (c >= 0 && c < 60)) {
if (d >= 0) {
t.set(a, b, c);
} else {
cout << "Input n Error!" << endl;
cin >> a >> b >> c >> d;
continue;
}
} else {
cout << "Time Error!" << endl;
cin >> a >> b >> c >> d;
continue;
}
t = t + d;
t.display();
cin >> a >> b >> c >> d;
} while (!(a == 0 && b == 0 && c == 0 && d == 0));
}