plarmi

workcpp_10_2

Jun 23rd, 2023
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.65 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. class Time {
  4. private:
  5.     int hours;
  6.     int minutes;
  7.     int seconds;
  8.  
  9. public:
  10.     // Конструкторы
  11.     Time() : hours(0), minutes(0), seconds(0) {}
  12.     Time(int h, int m, int s) : hours(h), minutes(m), seconds(s) {}
  13.  
  14.     // Функция-член для приращения времени на 1 секунду
  15.     void increment() {
  16.         seconds++;
  17.         if (seconds >= 60) {
  18.             seconds = 0;
  19.             minutes++;
  20.             if (minutes >= 60) {
  21.                 minutes = 0;
  22.                 hours++;
  23.                 if (hours >= 24) {
  24.                     hours = 0;
  25.                 }
  26.             }
  27.         }
  28.     }
  29.  
  30.     // Перегрузка оператора инкремента ++
  31.     Time operator++() {
  32.         increment();
  33.         return *this;
  34.     }
  35.  
  36.     // Перегрузка оператора декремента --
  37.     Time operator--() {
  38.         seconds--;
  39.         if (seconds < 0) {
  40.             seconds = 59;
  41.             minutes--;
  42.             if (minutes < 0) {
  43.                 minutes = 59;
  44.                 hours--;
  45.                 if (hours < 0) {
  46.                     hours = 23;
  47.                 }
  48.             }
  49.         }
  50.         return *this;
  51.     }
  52.  
  53.     // Перегрузка оператора неравенства !=
  54.     bool operator!=(const Time& other) const {
  55.         return hours != other.hours || minutes != other.minutes || seconds != other.seconds;
  56.     }
  57.  
  58.     // Перегрузка оператора равенства ==
  59.     bool operator==(const Time& other) const {
  60.         return hours == other.hours && minutes == other.minutes && seconds == other.seconds;
  61.     }
  62.  
  63.     // Перегрузка оператора больше >
  64.     bool operator>(const Time& other) const {
  65.         if (hours > other.hours) {
  66.             return true;
  67.         } else if (hours == other.hours && minutes > other.minutes) {
  68.             return true;
  69.         } else if (hours == other.hours && minutes == other.minutes && seconds > other.seconds) {
  70.             return true;
  71.         }
  72.         return false;
  73.     }
  74.  
  75.     // Перегрузка оператора меньше <
  76.     bool operator<(const Time& other) const {
  77.         if (hours < other.hours) {
  78.             return true;
  79.         } else if (hours == other.hours && minutes < other.minutes) {
  80.             return true;
  81.         } else if (hours == other.hours && minutes == other.minutes && seconds < other.seconds) {
  82.             return true;
  83.         }
  84.         return false;
  85.     }
  86.  
  87.     // Перегрузка оператора ввода >>
  88.     friend std::istream& operator>>(std::istream& is, Time& time) {
  89.         is >> time.hours >> time.minutes >> time.seconds;
  90.         return is;
  91.     }
  92.  
  93.     // Перегрузка оператора вывода <<
  94.     friend std::ostream& operator<<(std::ostream& os, const Time& time) {
  95.         os << time.hours << ":" << time.minutes << ":" << time.seconds;
  96.         return os;
  97.     }
  98.  
  99.     // Перегрузка оператора присваивания =
  100.     Time& operator=(const Time& other) {
  101.         if (this != &other) {
  102.             hours = other.hours;
  103.             minutes = other.minutes;
  104.             seconds = other.seconds;
  105.         }
  106.         return *this;
  107.     }
  108.  
  109.     // Перегрузка оператора сложения +=
  110.     Time& operator+=(int secondsToAdd) {
  111.         seconds += secondsToAdd;
  112.         normalizeTime();
  113.         return *this;
  114.     }
  115.  
  116.     // Перегрузка оператора вычитания -=
  117.     Time& operator-=(int secondsToSubtract) {
  118.         seconds -= secondsToSubtract;
  119.         normalizeTime();
  120.         return *this;
  121.     }
  122.  
  123. private:
  124.     // Вспомогательная функция для нормализации времени
  125.     void normalizeTime() {
  126.         if (seconds >= 60) {
  127.             int extraMinutes = seconds / 60;
  128.             seconds %= 60;
  129.             minutes += extraMinutes;
  130.             if (minutes >= 60) {
  131.                 int extraHours = minutes / 60;
  132.                 minutes %= 60;
  133.                 hours += extraHours;
  134.                 if (hours >= 24) {
  135.                     hours %= 24;
  136.                 }
  137.             }
  138.         } else if (seconds < 0) {
  139.             int extraMinutes = (-seconds - 1) / 60 + 1;
  140.             seconds = 60 - (-seconds % 60);
  141.             minutes -= extraMinutes;
  142.             if (minutes < 0) {
  143.                 int extraHours = (-minutes - 1) / 60 + 1;
  144.                 minutes = 60 - (-minutes % 60);
  145.                 hours -= extraHours;
  146.                 if (hours < 0) {
  147.                     hours = 24 - (-hours % 24);
  148.                 }
  149.             }
  150.         }
  151.     }
  152. };
  153.  
  154. int main() {
  155.     Time t1(12, 30, 45);
  156.     Time t2(23, 45, 10);
  157.  
  158.     // Ввод времени с помощью перегруженного оператора >>
  159.     std::cout << "Enter a time (format: hours minutes seconds): ";
  160.     std::cin >> t1;
  161.  
  162.     std::cout << "Enter another time (format: hours minutes seconds): ";
  163.     std::cin >> t2;
  164.  
  165.     // Вывод времени с помощью перегруженного оператора <<
  166.     std::cout << "Time 1: " << t1 << std::endl;
  167.     std::cout << "Time 2: " << t2 << std::endl;
  168.  
  169.     // Приращение времени на 1 секунду с помощью оператора ++
  170.     ++t1;
  171.     std::cout << "Time 1 after increment: " << t1 << std::endl;
  172.  
  173.     // Уменьшение времени на 1 секунду с помощью оператора --
  174.     --t2;
  175.     std::cout << "Time 2 after decrement: " << t2 << std::endl;
  176.  
  177.     // Проверка на неравенство времен с помощью оператора !=
  178.     if (t1 != t2) {
  179.         std::cout << "The times are not equal." << std::endl;
  180.     } else {
  181.         std::cout << "The times are equal." << std::endl;
  182.     }
  183.  
  184.     // Проверка на равенство времен с помощью оператора ==
  185.     if (t1 == t2) {
  186.         std::cout << "The times are equal." << std::endl;
  187.     } else {
  188.         std::cout << "The times are not equal." << std::endl;
  189.     }
  190.  
  191.     // Сравнение времен с помощью оператора >
  192.     if (t1 > t2) {
  193.         std::cout << "Time 1 is greater than Time 2." << std::endl;
  194.     } else {
  195.         std::cout << "Time 1 is not greater than Time 2." << std::endl;
  196.     }
  197.  
  198.     // Сравнение времен с помощью оператора <
  199.     if (t1 < t2) {
  200.         std::cout << "Time 1 is less than Time 2." << std::endl;
  201.     } else {
  202.         std::cout << "Time 1 is not less than Time 2." << std::endl;
  203.     }
  204.  
  205.     return 0;
  206. }
  207.  
Add Comment
Please, Sign In to add comment