时间类Time的编写.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #include <iostream>
  2. #include <iomanip>
  3. class Time {
  4. public:
  5. int hour, minute, second;
  6. // ¹¹Ô캯Êý
  7. Time(int h = 0, int m = 0, int s = 0) : hour(h), minute(m), second(s) {}
  8. // ÊäÈëÔËËã·ûÖØÔØ
  9. friend std::istream& operator>>(std::istream& is, Time& t) {
  10. is >> t.hour >> t.minute >> t.second;
  11. return is;
  12. }
  13. // Êä³öÔËËã·ûÖØÔØ
  14. friend std::ostream& operator<<(std::ostream& os, const Time& t) {
  15. os << std::setw(2) << std::setfill('0') << t.hour << ":"
  16. << std::setw(2) << std::setfill('0') << t.minute << ":"
  17. << std::setw(2) << std::setfill('0') << t.second;
  18. return os;
  19. }
  20. // += ÔËËã·ûÖØÔØ
  21. Time& operator+=(const Time& t) {
  22. this->second += t.second;
  23. this->minute += t.minute + this->second / 60;
  24. this->hour += t.hour + this->minute / 60;
  25. this->second %= 60;
  26. this->minute %= 60;
  27. this->hour %= 24;
  28. return *this;
  29. }
  30. // -= ÔËËã·ûÖØÔØ
  31. Time& operator-=(const Time& t) {
  32. int totalSeconds1 = this->hour * 3600 + this->minute * 60 + this->second;
  33. int totalSeconds2 = t.hour * 3600 + t.minute * 60 + t.second;
  34. int totalSeconds = totalSeconds1 - totalSeconds2;
  35. if (totalSeconds < 0) {
  36. totalSeconds += 24 * 3600;
  37. }
  38. this->hour = (totalSeconds / 3600) % 24;
  39. this->minute = (totalSeconds % 3600) / 60;
  40. this->second = totalSeconds % 60;
  41. return *this;
  42. }
  43. // Ç°ÖÃ++ÔËËã·ûÖØÔØ
  44. Time& operator++() {
  45. this->second++;
  46. if (this->second >= 60) {
  47. this->second = 0;
  48. this->minute++;
  49. if (this->minute >= 60) {
  50. this->minute = 0;
  51. this->hour++;
  52. if (this->hour >= 24) {
  53. this->hour = 0;
  54. }
  55. }
  56. }
  57. return *this;
  58. }
  59. // ºóÖÃ++ÔËËã·ûÖØÔØ
  60. Time operator++(int) {
  61. Time temp = *this;
  62. ++(*this);
  63. return temp;
  64. }
  65. // Ç°ÖÃ--ÔËËã·ûÖØÔØ
  66. Time& operator--() {
  67. if (this->second == 0) {
  68. this->second = 59;
  69. if (this->minute == 0) {
  70. this->minute = 59;
  71. if (this->hour == 0) {
  72. this->hour = 23;
  73. } else {
  74. this->hour--;
  75. }
  76. } else {
  77. this->minute--;
  78. }
  79. } else {
  80. this->second--;
  81. }
  82. return *this;
  83. }
  84. // ºóÖÃ--ÔËËã·ûÖØÔØ
  85. Time operator--(int) {
  86. Time temp = *this;
  87. --(*this);
  88. return temp;
  89. }
  90. };
  91. int main() {
  92. Time time1, time2;
  93. std::cin >> time1 >> time2;
  94. std::cout << (time1 += (time2++)) << std::endl;
  95. std::cout << (time1 -= time2) << std::endl;
  96. std::cout << (++time2) << std::endl;
  97. std::cout << (time2 += (time1--)) << std::endl;
  98. std::cout << (--time1) << std::endl;
  99. std::cout << (time2 -= time1) << std::endl;
  100. return 0;
  101. }