Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <iomanip>
- using namespace std;
- class Time
- {
- private:
- void normalize();
- int hours;
- int minutes;
- int seconds;
- public:
- Time()
- {
- seconds = 0;
- minutes = 0;
- hours = 0;
- }
- Time(int h, int m, int s)
- {
- hours = h;
- minutes = m;
- seconds = s;
- normalize();
- }
- friend Time operator + (const Time &t1, const Time &t2);
- friend Time operator - (const Time &t3, const Time &t4);
- friend bool operator < (const Time &t1, const Time &t3);
- friend ostream &operator << (ostream &output, Time &t);
- friend istream &operator >> (istream &input, Time &t);
- };
- istream &operator >> (istream &input, Time &t)
- {
- input >> t.hours >> t.minutes >> t.seconds;
- t.normalize();
- return input;
- }
- ostream &operator << (ostream &output, Time &t)
- {
- output << setfill('0') << setw(2) << t.hours << ":"
- << setfill('0') << setw(2) << t.minutes << ":"
- << setfill('0') << setw(2) << t.seconds;
- return output;
- }
- Time operator + (const Time &t1, const Time &t2)
- {
- Time result;
- result.hours = t1.hours + t2.hours;
- result.minutes = t1.minutes + t2.minutes;
- result.seconds = t1.seconds + t2.seconds;
- result.normalize();
- return result;
- }
- Time operator - (const Time &t3, const Time &t4)
- {
- Time result;
- result.hours = t3.hours - t4.hours;
- result.minutes = t3.minutes - t4.minutes;
- result.seconds = t3.seconds - t4.seconds;
- result.normalize();
- return result;
- }
- bool operator<(const Time &t1, const Time &t3)
- {
- return true;
- }
- int main()
- {
- Time t1, t2, t3, t4;
- cin >> t1;
- cin >> t2;
- cin >> t3;
- cout << "Time1: " << t1 << endl;
- cout << "Time2: " << t2 << endl;
- cout << "Time3: " << t3 << endl;
- t4 = t1 + t2;
- cout << "Time4: " << t4 << endl;
- t1 = t3 - t4;
- cout << "Time1: " << t1 << endl;
- if (t1 < t3)
- {
- cout << "Time1 < Time3" << endl;
- }
- else
- {
- cout << "Time3 >= Time1" << endl;
- }
- Time t5 = t2 + Time(0,0,1);
- if (t5 < t2)
- {
- cout << "Time5 < Time2" << endl;
- }
- else
- {
- cout << "Time5 >= Time2" << endl;
- }
- // cout << "Almost midnight: " << Time(0,0,0) - Time(0,0,1) << endl;
- return 0;
- }
- void Time :: normalize()
- {
- int s = seconds;
- int m = minutes;
- int h = hours;
- while(s < 0)
- {
- s += 60;
- m--;
- }
- while(m < 0)
- {
- m += 60;
- h--;
- }
- while(h < 0)
- {
- h = h + 24;
- }
- seconds = s % 60;
- minutes = (m + s/60) % 60;
- hours = (h + m/60 + s/3600) % 24;
- }
- /*
- Example:
- 0 0 14655
- 0 104 323
- 1 1 1
- output:
- Time1: 04:04:15
- Time2: 01:49:23
- Time3: 01:01:01
- Time4: 05:53:38
- Time1: 19:07:23
- Time3 >= Time1
- Time5 >= Time2
- Almost midnight: 23:59:59
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement