Добавил:
vvrstcnho
Рад, если кому-то помог
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз:
Предмет:
Файл:Лабораторные работы С++ (для ИВТ) / Готовые лабы С++ / Лаба3 / Laba 3 (3)
.cpp#include <iostream>
#include <windows.h>
using namespace std;
class BritishDate {
private:
int day, month, year;
bool isLeapYear(int y) const {
return (y % 4 == 0 && y % 100 != 0) || (y % 400 == 0);
}
int daysInMonth(int m, int y) const {
switch (m) {
case 2: return isLeapYear(y) ? 29 : 28;
case 4: case 6: case 9: case 11: return 30;
default: return 31;
}
}
void normalize() {
while (day > daysInMonth(month, year)) {
day -= daysInMonth(month, year);
month++;
if (month > 12) {
month = 1;
year++;
}
}
while (day < 1) {
month--;
if (month < 1) {
month = 12;
year--;
}
day += daysInMonth(month, year);
}
}
public:
BritishDate(int d = 1, int m = 1, int y = 2000) : day(d), month(m), year(y) {
normalize();
}
// Сложение даты и количества дней
BritishDate operator+(int days) const {
BritishDate result = *this;
result.day += days;
result.normalize();
return result;
}
// Вычитание количества дней из даты
BritishDate operator-(int days) const {
BritishDate result = *this;
result.day -= days;
result.normalize();
return result;
}
// Разность между двумя датами в днях (%)
int operator%(const BritishDate& other) const {
BritishDate start = *this;
BritishDate end = other;
int days = 0;
// Приводим к одной дате
if (start.year > end.year || (start.year == end.year && start.month > end.month) ||
(start.year == end.year && start.month == end.month && start.day > end.day)) {
swap(start, end);
}
// Подсчет дней
while (start.year != end.year || start.month != end.month || start.day != end.day) {
start.day++;
days++;
start.normalize();
}
return days;
}
void print() const {
cout << day << "/" << month << "/" << year << endl;
}
friend ostream& operator<<(ostream& os, const BritishDate& date);
};
ostream& operator<<(ostream& os, const BritishDate& date) {
os << date.day << "/" << date.month << "/" << date.year;
return os;
}
int main() {
SetConsoleOutputCP(65001);
cout << "=== Задача 3.3 - Британская дата ===" << endl;
BritishDate d1(15, 5, 2024);
BritishDate d2(1, 1, 2024);
cout << "Дата d1: " << d1 << endl;
cout << "Дата d2: " << d2 << endl;
BritishDate d3 = d1 + 30;
cout << "d1 + 30 дней: " << d3 << endl;
BritishDate d4 = d1 - 45;
cout << "d1 - 45 дней: " << d4 << endl;
int daysBetween = d1 % d2;
cout << "Разница между d1 и d2: " << daysBetween << " дней" << endl;
// Проверка високосного года
BritishDate leap(28, 2, 2024);
cout << "28/2/2024 + 1 день: " << (leap + 1) << endl;
return 0;
}
Соседние файлы в папке Лаба3
