123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- #include <iostream>
- #include <iomanip>
- class Time {
- public:
- int hour, minute, second;
- // ¹¹Ô캯Êý
- Time(int h = 0, int m = 0, int s = 0) : hour(h), minute(m), second(s) {}
- // ÊäÈëÔËËã·ûÖØÔØ
- friend std::istream& operator>>(std::istream& is, Time& t) {
- is >> t.hour >> t.minute >> t.second;
- return is;
- }
- // Êä³öÔËËã·ûÖØÔØ
- friend std::ostream& operator<<(std::ostream& os, const Time& t) {
- os << std::setw(2) << std::setfill('0') << t.hour << ":"
- << std::setw(2) << std::setfill('0') << t.minute << ":"
- << std::setw(2) << std::setfill('0') << t.second;
- return os;
- }
- // += ÔËËã·ûÖØÔØ
- Time& operator+=(const Time& t) {
- this->second += t.second;
- this->minute += t.minute + this->second / 60;
- this->hour += t.hour + this->minute / 60;
- this->second %= 60;
- this->minute %= 60;
- this->hour %= 24;
- return *this;
- }
- // -= ÔËËã·ûÖØÔØ
- Time& operator-=(const Time& t) {
- int totalSeconds1 = this->hour * 3600 + this->minute * 60 + this->second;
- int totalSeconds2 = t.hour * 3600 + t.minute * 60 + t.second;
- int totalSeconds = totalSeconds1 - totalSeconds2;
- if (totalSeconds < 0) {
- totalSeconds += 24 * 3600;
- }
- this->hour = (totalSeconds / 3600) % 24;
- this->minute = (totalSeconds % 3600) / 60;
- this->second = totalSeconds % 60;
- return *this;
- }
- // Ç°ÖÃ++ÔËËã·ûÖØÔØ
- Time& operator++() {
- this->second++;
- if (this->second >= 60) {
- this->second = 0;
- this->minute++;
- if (this->minute >= 60) {
- this->minute = 0;
- this->hour++;
- if (this->hour >= 24) {
- this->hour = 0;
- }
- }
- }
- return *this;
- }
- // ºóÖÃ++ÔËËã·ûÖØÔØ
- Time operator++(int) {
- Time temp = *this;
- ++(*this);
- return temp;
- }
- // Ç°ÖÃ--ÔËËã·ûÖØÔØ
- Time& operator--() {
- if (this->second == 0) {
- this->second = 59;
- if (this->minute == 0) {
- this->minute = 59;
- if (this->hour == 0) {
- this->hour = 23;
- } else {
- this->hour--;
- }
- } else {
- this->minute--;
- }
- } else {
- this->second--;
- }
- return *this;
- }
- // ºóÖÃ--ÔËËã·ûÖØÔØ
- Time operator--(int) {
- Time temp = *this;
- --(*this);
- return temp;
- }
- };
- int main() {
- Time time1, time2;
- std::cin >> time1 >> time2;
- std::cout << (time1 += (time2++)) << std::endl;
- std::cout << (time1 -= time2) << std::endl;
- std::cout << (++time2) << std::endl;
- std::cout << (time2 += (time1--)) << std::endl;
- std::cout << (--time1) << std::endl;
- std::cout << (time2 -= time1) << std::endl;
- return 0;
- }
|