fork download
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. class CThoiGian
  6. {
  7. private:
  8. int hours, minutes, seconds;
  9. public:
  10. CThoiGian(int h = 0, int m = 0, int s = 0) : hours(h), minutes(m), seconds(s) {}
  11. bool operator>=(const CThoiGian& time)
  12. {
  13. if (hours > time.hours) return true;
  14. if (hours == time.hours && minutes > time.minutes) return true;
  15. if (hours == time.hours && minutes == time.minutes && seconds >= time.seconds) return true;
  16. return false;
  17. }
  18. bool operator!=(const CThoiGian& time)
  19. {
  20. return hours != time.hours || minutes != time.minutes || seconds != time.seconds;
  21. }
  22. CThoiGian operator+(const CThoiGian& time)
  23. {
  24. CThoiGian res;
  25. res.seconds = seconds + time.seconds;
  26. res.minutes = minutes + time.minutes + res.seconds / 60;
  27. res.seconds %= 60;
  28. res.hours = hours + time.hours + res.minutes / 60;
  29. res.minutes %= 60;
  30. return res;
  31. }
  32. CThoiGian operator-(CThoiGian time)
  33. {
  34. CThoiGian res;
  35. res.seconds = seconds - time.seconds;
  36. if (res.seconds < 0)
  37. {
  38. res.seconds += 60;
  39. time.minutes++;
  40. }
  41. res.minutes = minutes - time.minutes;
  42. if (res.minutes < 0)
  43. {
  44. res.minutes += 60;
  45. time.hours++;
  46. }
  47. res.hours = hours - time.hours;
  48. return res;
  49. }
  50. CThoiGian& operator++()
  51. {
  52. seconds++;
  53. if (seconds == 60)
  54. {
  55. seconds = 0;
  56. minutes++;
  57. if (minutes == 60)
  58. {
  59. minutes = 0;
  60. hours++;
  61. if (hours == 24)
  62. {
  63. hours = 0;
  64. }
  65. }
  66. }
  67. return *this;
  68. }
  69. CThoiGian& operator--()
  70. {
  71. seconds--;
  72. if (seconds == -1)
  73. {
  74. seconds = 59;
  75. minutes--;
  76. if (minutes == -1)
  77. {
  78. minutes = 59;
  79. hours--;
  80. if (hours == -1)
  81. {
  82. hours = 23;
  83. }
  84. }
  85. }
  86. return *this;
  87. }
  88. friend istream& operator>>(istream& is, CThoiGian& time)
  89. {
  90. is >> time.hours >> time.minutes >> time.seconds;
  91. return is;
  92. }
  93. friend ostream& operator<<(ostream& os, const CThoiGian& time)
  94. {
  95. os << time.hours << " " << time.minutes << " " << time.seconds;
  96. return os;
  97. }
  98. };
  99.  
  100. int main() {
  101. CThoiGian t1(1, 30, 45);
  102. CThoiGian t2(0, 45, 30);
  103.  
  104. CThoiGian t3 = t1 + t2;
  105. cout << "t1 + t2 = " << t3 << endl; // 2:16:15
  106.  
  107. CThoiGian t4 = t1 - t2;
  108. cout << "t1 - t2 = " << t4 << endl; // 0:45:15
  109.  
  110. ++t1;
  111. cout << "++t1 = " << t1 << endl; // 1:30:46
  112.  
  113. --t2;
  114. cout << "--t2 = " << t2 << endl; // 0:45:29
  115.  
  116. cout << "t1 >= t2: " << (t1 >= t2) << endl; // 1
  117. cout << "t1 != t2: " << (t1 != t2) << endl; // 1
  118.  
  119. return 0;
  120. }
Success #stdin #stdout 0.01s 5292KB
stdin
Standard input is empty
stdout
t1 + t2 = 2 16 15
t1 - t2 = 0 45 15
++t1 = 1 30 46
--t2 = 0 45 29
t1 >= t2: 1
t1 != t2: 1