Advertisement
Guest User

Untitled

a guest
Oct 27th, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.03 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3.  
  4. using namespace std;
  5.  
  6. class Time
  7. {
  8. private:
  9.     void normalize();
  10.     int hours;
  11.     int minutes;
  12.     int seconds;
  13. public:
  14.     Time()
  15.     {
  16.         seconds = 0;
  17.         minutes = 0;
  18.         hours   = 0;
  19.     }
  20.     Time(int h, int m, int s)
  21.     {
  22.         hours   = h;
  23.         minutes = m;
  24.         seconds = s;
  25.         normalize();
  26.     }
  27.  
  28.     friend Time operator + (const Time &t1, const Time &t2);
  29.     friend Time operator - (const Time &t3, const Time &t4);
  30.     friend bool operator < (const Time &t1, const Time &t3);
  31.     friend ostream &operator << (ostream &output, Time &t);
  32.     friend istream &operator >> (istream &input, Time &t);
  33. };
  34.  
  35. istream &operator >> (istream &input, Time &t)
  36. {
  37.     input >> t.hours >> t.minutes >> t.seconds;
  38.     t.normalize();
  39.     return input;
  40. }
  41.  
  42. ostream &operator << (ostream &output, Time &t)
  43. {
  44.     output << setfill('0') << setw(2) << t.hours << ":"
  45.     << setfill('0') << setw(2) << t.minutes << ":"
  46.     << setfill('0') << setw(2) << t.seconds;
  47.     return output;
  48. }
  49.  
  50.  
  51. Time operator + (const Time &t1, const Time &t2)
  52. {
  53.     Time result;
  54.     result.hours = t1.hours + t2.hours;
  55.     result.minutes = t1.minutes + t2.minutes;
  56.     result.seconds = t1.seconds + t2.seconds;
  57.     result.normalize();
  58.     return result;
  59. }
  60.  
  61. Time operator - (const Time &t3, const Time &t4)
  62. {
  63.     Time result;
  64.     result.hours = t3.hours - t4.hours;
  65.     result.minutes = t3.minutes - t4.minutes;
  66.     result.seconds = t3.seconds - t4.seconds;
  67.     result.normalize();
  68.     return result;
  69. }
  70.  
  71. bool operator<(const Time &t1, const Time &t3)
  72. {
  73.     return true;
  74. }
  75.  
  76. int main()
  77. {
  78.     Time t1, t2, t3, t4;
  79.     cin >> t1;
  80.     cin >> t2;
  81.     cin >> t3;
  82.  
  83.     cout << "Time1: " << t1 << endl;
  84.     cout << "Time2: " << t2 << endl;
  85.     cout << "Time3: " << t3 << endl;
  86.  
  87.     t4 = t1 + t2;
  88.     cout << "Time4: " << t4 << endl;
  89.  
  90.     t1 = t3 - t4;
  91.     cout << "Time1: " << t1 << endl;
  92.  
  93.  
  94.  
  95.     if (t1 < t3)
  96.     {
  97.         cout << "Time1 < Time3" << endl;
  98.     }
  99.     else
  100.     {
  101.         cout << "Time3 >= Time1" << endl;
  102.     }
  103.  
  104.     Time t5 = t2 + Time(0,0,1);
  105.     if (t5 < t2)
  106.     {
  107.         cout << "Time5 < Time2" << endl;
  108.     }
  109.     else
  110.     {
  111.         cout << "Time5 >= Time2" << endl;
  112.     }
  113.  
  114. //    cout << "Almost midnight: " << Time(0,0,0) - Time(0,0,1) << endl;
  115.  
  116.     return 0;
  117. }
  118. void Time :: normalize()
  119.     {
  120.         int s = seconds;
  121.         int m = minutes;
  122.         int h = hours;
  123.  
  124.         while(s < 0)
  125.         {
  126.             s += 60;
  127.             m--;
  128.         }
  129.  
  130.         while(m < 0)
  131.         {
  132.             m += 60;
  133.             h--;
  134.         }
  135.  
  136.         while(h < 0)
  137.         {
  138.             h = h + 24;
  139.         }
  140.  
  141.         seconds = s % 60;
  142.         minutes = (m + s/60) % 60;
  143.         hours   = (h + m/60 + s/3600) % 24;
  144.  
  145.  
  146.     }
  147.  
  148.  
  149. /*
  150. Example:
  151. 0 0 14655
  152. 0 104 323
  153. 1 1 1
  154. output:
  155. Time1: 04:04:15
  156. Time2: 01:49:23
  157. Time3: 01:01:01
  158. Time4: 05:53:38
  159. Time1: 19:07:23
  160. Time3 >= Time1
  161. Time5 >= Time2
  162. Almost midnight: 23:59:59
  163. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement