Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- class Time {
- private:
- int hours;
- int minutes;
- int seconds;
- public:
- // Конструкторы
- Time() : hours(0), minutes(0), seconds(0) {}
- Time(int h, int m, int s) : hours(h), minutes(m), seconds(s) {}
- // Функция-член для приращения времени на 1 секунду
- void increment() {
- seconds++;
- if (seconds >= 60) {
- seconds = 0;
- minutes++;
- if (minutes >= 60) {
- minutes = 0;
- hours++;
- if (hours >= 24) {
- hours = 0;
- }
- }
- }
- }
- // Перегрузка оператора инкремента ++
- Time operator++() {
- increment();
- return *this;
- }
- // Перегрузка оператора декремента --
- Time operator--() {
- seconds--;
- if (seconds < 0) {
- seconds = 59;
- minutes--;
- if (minutes < 0) {
- minutes = 59;
- hours--;
- if (hours < 0) {
- hours = 23;
- }
- }
- }
- return *this;
- }
- // Перегрузка оператора неравенства !=
- bool operator!=(const Time& other) const {
- return hours != other.hours || minutes != other.minutes || seconds != other.seconds;
- }
- // Перегрузка оператора равенства ==
- bool operator==(const Time& other) const {
- return hours == other.hours && minutes == other.minutes && seconds == other.seconds;
- }
- // Перегрузка оператора больше >
- bool operator>(const Time& other) const {
- if (hours > other.hours) {
- return true;
- } else if (hours == other.hours && minutes > other.minutes) {
- return true;
- } else if (hours == other.hours && minutes == other.minutes && seconds > other.seconds) {
- return true;
- }
- return false;
- }
- // Перегрузка оператора меньше <
- bool operator<(const Time& other) const {
- if (hours < other.hours) {
- return true;
- } else if (hours == other.hours && minutes < other.minutes) {
- return true;
- } else if (hours == other.hours && minutes == other.minutes && seconds < other.seconds) {
- return true;
- }
- return false;
- }
- // Перегрузка оператора ввода >>
- friend std::istream& operator>>(std::istream& is, Time& time) {
- is >> time.hours >> time.minutes >> time.seconds;
- return is;
- }
- // Перегрузка оператора вывода <<
- friend std::ostream& operator<<(std::ostream& os, const Time& time) {
- os << time.hours << ":" << time.minutes << ":" << time.seconds;
- return os;
- }
- // Перегрузка оператора присваивания =
- Time& operator=(const Time& other) {
- if (this != &other) {
- hours = other.hours;
- minutes = other.minutes;
- seconds = other.seconds;
- }
- return *this;
- }
- // Перегрузка оператора сложения +=
- Time& operator+=(int secondsToAdd) {
- seconds += secondsToAdd;
- normalizeTime();
- return *this;
- }
- // Перегрузка оператора вычитания -=
- Time& operator-=(int secondsToSubtract) {
- seconds -= secondsToSubtract;
- normalizeTime();
- return *this;
- }
- private:
- // Вспомогательная функция для нормализации времени
- void normalizeTime() {
- if (seconds >= 60) {
- int extraMinutes = seconds / 60;
- seconds %= 60;
- minutes += extraMinutes;
- if (minutes >= 60) {
- int extraHours = minutes / 60;
- minutes %= 60;
- hours += extraHours;
- if (hours >= 24) {
- hours %= 24;
- }
- }
- } else if (seconds < 0) {
- int extraMinutes = (-seconds - 1) / 60 + 1;
- seconds = 60 - (-seconds % 60);
- minutes -= extraMinutes;
- if (minutes < 0) {
- int extraHours = (-minutes - 1) / 60 + 1;
- minutes = 60 - (-minutes % 60);
- hours -= extraHours;
- if (hours < 0) {
- hours = 24 - (-hours % 24);
- }
- }
- }
- }
- };
- int main() {
- Time t1(12, 30, 45);
- Time t2(23, 45, 10);
- // Ввод времени с помощью перегруженного оператора >>
- std::cout << "Enter a time (format: hours minutes seconds): ";
- std::cin >> t1;
- std::cout << "Enter another time (format: hours minutes seconds): ";
- std::cin >> t2;
- // Вывод времени с помощью перегруженного оператора <<
- std::cout << "Time 1: " << t1 << std::endl;
- std::cout << "Time 2: " << t2 << std::endl;
- // Приращение времени на 1 секунду с помощью оператора ++
- ++t1;
- std::cout << "Time 1 after increment: " << t1 << std::endl;
- // Уменьшение времени на 1 секунду с помощью оператора --
- --t2;
- std::cout << "Time 2 after decrement: " << t2 << std::endl;
- // Проверка на неравенство времен с помощью оператора !=
- if (t1 != t2) {
- std::cout << "The times are not equal." << std::endl;
- } else {
- std::cout << "The times are equal." << std::endl;
- }
- // Проверка на равенство времен с помощью оператора ==
- if (t1 == t2) {
- std::cout << "The times are equal." << std::endl;
- } else {
- std::cout << "The times are not equal." << std::endl;
- }
- // Сравнение времен с помощью оператора >
- if (t1 > t2) {
- std::cout << "Time 1 is greater than Time 2." << std::endl;
- } else {
- std::cout << "Time 1 is not greater than Time 2." << std::endl;
- }
- // Сравнение времен с помощью оператора <
- if (t1 < t2) {
- std::cout << "Time 1 is less than Time 2." << std::endl;
- } else {
- std::cout << "Time 1 is not less than Time 2." << std::endl;
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment