Добавил:
vvrstcnho
Рад, если кому-то помог
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз:
Предмет:
Файл:Лабораторные работы С++ (для ИВТ) / Готовые лабы С++ / Лаба3 / Laba 3 (7)
.cpp#include <iostream>
#include <windows.h>
using namespace std;
class Time {
private:
int hours, minutes, seconds;
void normalize() {
if (seconds >= 60) {
minutes += seconds / 60;
seconds %= 60;
} else if (seconds < 0) {
minutes -= (-seconds) / 60 + 1;
seconds = 60 - ((-seconds) % 60);
}
if (minutes >= 60) {
hours += minutes / 60;
minutes %= 60;
} else if (minutes < 0) {
hours -= (-minutes) / 60 + 1;
minutes = 60 - ((-minutes) % 60);
}
if (hours >= 24) {
hours %= 24;
} else if (hours < 0) {
hours = 24 - ((-hours) % 24);
}
}
public:
Time(int h = 0, int m = 0, int s = 0) : hours(h), minutes(m), seconds(s) {
normalize();
}
// Сложение времени и секунд
Time operator+(int sec) const {
Time result = *this;
result.seconds += sec;
result.normalize();
return result;
}
// Вычитание секунд из времени
Time operator-(int sec) const {
Time result = *this;
result.seconds -= sec;
result.normalize();
return result;
}
// Сложение двух моментов времени
Time operator+(const Time& other) const {
Time result;
result.hours = hours + other.hours;
result.minutes = minutes + other.minutes;
result.seconds = seconds + other.seconds;
result.normalize();
return result;
}
// Вычитание двух моментов времени
Time operator-(const Time& other) const {
Time result;
result.hours = hours - other.hours;
result.minutes = minutes - other.minutes;
result.seconds = seconds - other.seconds;
result.normalize();
return result;
}
// Разность в секундах между двумя моментами времени (%)
int operator%(const Time& other) const {
int totalSeconds1 = hours * 3600 + minutes * 60 + seconds;
int totalSeconds2 = other.hours * 3600 + other.minutes * 60 + other.seconds;
return abs(totalSeconds1 - totalSeconds2);
}
void print() const {
cout << hours << ":" << minutes << ":" << seconds << endl;
}
friend ostream& operator<<(ostream& os, const Time& t);
};
ostream& operator<<(ostream& os, const Time& t) {
os << t.hours << ":" << t.minutes << ":" << t.seconds;
return os;
}
int main() {
SetConsoleOutputCP(65001);
cout << "=== Задача 3.7 - Время ===" << endl;
Time t1(10, 30, 45);
Time t2(2, 45, 15);
cout << "Время t1: " << t1 << endl;
cout << "Время t2: " << t2 << endl;
Time t3 = t1 + 3600; // +1 час
cout << "t1 + 3600 секунд: " << t3 << endl;
Time t4 = t1 - 7200; // -2 часа
cout << "t1 - 7200 секунд: " << t4 << endl;
Time t5 = t1 + t2;
cout << "t1 + t2: " << t5 << endl;
Time t6 = t1 - t2;
cout << "t1 - t2: " << t6 << endl;
int secondsBetween = t1 % t2;
cout << "Разница между t1 и t2: " << secondsBetween << " секунд" << endl;
// Проверка перехода через 24 часа
Time t7(23, 59, 59);
Time t8 = t7 + 2;
cout << "23:59:59 + 2 секунды: " << t8 << endl;
return 0;
}
Соседние файлы в папке Лаба3
