Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <math.h>
- using namespace std;
- class Date
- {
- int m_day;
- int m_month;
- int m_year;
- public:
- Date() //Конструктор по умолчанию
- {
- m_day = 19;
- m_month = 10;
- m_year = 2001;
- }
- Date(int day, int month, int year) //Конструктор с параметрами
- {
- m_day = day;
- m_month = month;
- m_year = year;
- }
- Date(const Date& other) //Конструктор копирования
- {
- this->m_day = other.m_day;
- this->m_month = other.m_month;
- this->m_year = other.m_year;
- cout << '\n' << "Сработал конструктор копирования" << '\n'<<this << '\n';
- }
- void get(Date* t) //Вывод
- {
- cout << "Дата: ";
- if (t->m_day < 10)
- cout << "0" << t->m_day << ".";
- else
- cout << t->m_day << ".";
- if (t->m_month < 10)
- cout << "0" << t->m_month << ".";
- else
- cout << t->m_month << ".";
- cout << t->m_year << '\n';
- }
- void set(Date& today) //Ввод
- {
- int d, m, y;
- bool f1, f2;
- f1 = f2 = false;
- cout << "Введите год: ";
- cin >> y;
- int arr[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 };
- if (y % 4 == 0) arr[1] = 29;
- while (f1 == 0)
- {
- cout << "Введите месяц: ";
- cin >> m;
- if (m < 1 || m>12)
- {
- cout << "Месяц введен не верно. Попробуйте еще раз!\n";
- f1 = 0;
- }
- else f1 = 1;
- }
- while (f2 == 0)
- {
- cout << "Введите день: ";
- cin >> d;
- if (arr[m - 1] < d || d < 1)
- {
- cout << "День введен не верно. Попробуйте еще раз!\n";
- f2 = 0;
- }
- else f2 = 1;
- }
- today.m_day = d;
- today.m_month = m;
- today.m_year = y;
- }
- int dobavl(Date* t) //Добавление к текущей дате дня/ месяца/ года
- {
- while (true)
- {
- int a;
- cout << '\n' << "Что Вы хотите сделать?" << '\n'
- << "1.Добавить некоторое количесво дней." << '\n'
- << "2.Добавить некоторое количество месяцев." << '\n'
- << "3.Добавить некоторое количество лет." << '\n'
- << "4.Выйти из меню." << '\n';
- cin >> a;
- int arr[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 };
- if (m_year % 4 == 0)arr[1] = 29;
- switch (a)
- {
- case 1:
- int day1;
- cout << "Сколько дней Вы хотите добавить(количество дней не должно превышать 31 дня)?\n";
- cin >> day1;
- if (t->m_day + day1 > arr[t->m_day - 1])
- {
- t->m_day = day1 - arr[t->m_month - 1] + t->m_day;
- t->m_month++;
- }
- else
- t->m_day += day1;
- if (t->m_month > 12)
- {
- t->m_month = 1;
- t->m_year++;
- }
- break;
- case 2:
- int month1;
- cout << "Сколько месяцев Вы хотите добавить(количество месяцев не должно превышать 12 месяцев)?\n";
- cin >> month1;
- if (t->m_month + month1 > 12)
- {
- t->m_month = t->m_month+month1-12;
- t->m_year++;
- }
- else t->m_month += month1;
- break;
- case 3:
- int year1;
- cout << "Сколько лет вы хотите добавить?\n";
- cin >> year1;
- t->m_year += year1;
- break;
- case 4: get(t);
- return 1;
- default: cout << "Выберите правильную функцию из предложенного меню."; break;
- }
- }
- return 0;
- }
- friend int kolvo_days(Date*); //Дружественная функция для нахождения количества лней между датами
- Date& operator=(const Date& data) // копирование даты
- {
- this->m_day = data.m_day;
- this->m_month = data.m_month;
- this->m_year = data.m_year;
- return *this;
- }
- Date& operator+=(const Date& atata) //добавление даты
- {
- int arr[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 };
- if (this->m_year % 4 == 0)arr[1] = 29;
- this->m_year += atata.m_year;
- if (this->m_month + atata.m_month > 12)
- {
- this->m_month= this->m_month+ atata.m_month - 12;
- this->m_year++;
- }
- else this->m_month += atata.m_month;
- if (this->m_day + atata.m_day > arr[this->m_month - 1])
- {
- this->m_day = this->m_day + atata.m_day - arr[this->m_month - 1];
- this->m_month++;
- }
- else this->m_day += atata.m_day;
- if (this->m_month > 12)
- {
- this->m_month -= 12;
- this->m_year++;
- }
- return *this;
- }
- Date& operator-=(const Date& lal) // вычитание даты
- {
- int arr[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 };
- if (this->m_year % 4 == 0)arr[1] = 29;
- this->m_year =abs( this->m_year - lal.m_year);
- if (this->m_month - lal.m_month < 0)
- {
- this->m_month = this->m_month - lal.m_month + 12;
- this->m_year--;
- }
- else this->m_month = this->m_month - lal.m_month;
- if (this->m_day - lal.m_day < 1)
- {
- this->m_day = this->m_day - lal.m_day + arr[this->m_month - 2];
- this->m_month--;
- }
- else this->m_day = this->m_day - lal.m_day;
- if (this->m_month < 1)
- {
- this->m_month += 12;
- this->m_year--;
- }
- return *this;
- }
- Date& operator-=(int k) // вычитание дней
- {
- int arr[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 };
- if (this->m_year % 4 == 0)arr[1] = 29;
- while (k > 0)
- {
- this->m_day--;
- k--;
- if (this->m_day < 1)
- {
- this->m_month--;
- if (this->m_month < 1)
- {
- this->m_month += 12;
- this->m_year--;
- }
- this->m_day =arr[this->m_month-1];
- }
- }
- return *this;
- }
- Date& operator+=(int k)// добавление дней
- {
- int arr[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 };
- if (this->m_year % 4 == 0)arr[1] = 29;
- while (k > 0)
- {
- this->m_day++;
- k--;
- if (this->m_day > arr[this->m_month - 1])
- {
- this->m_month++;
- this->m_day = 1;
- if (this->m_month > 12)
- {
- this->m_month = 1;
- this->m_year++;
- }
- }
- }
- return *this;
- }
- friend std::ostream& operator<<( std::ostream& os, const Date&);
- friend std::istream& operator>>(std::istream& is, Date&);
- };
- istream& operator>>(std::istream& is, Date& kik)
- {
- int d, m, y;
- bool f1, f2;
- f1 = f2 = false;
- cout << "Введите год: ";
- cin >> y;
- int arr[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 };
- if (y % 4 == 0) arr[1] = 29;
- while (f1 == 0)
- {
- cout << "Введите месяц: ";
- cin >> m;
- if (m < 1 || m>12)
- {
- cout << "Месяц введен не верно. Попробуйте еще раз!\n";
- f1 = 0;
- }
- else f1 = 1;
- }
- while (f2 == 0)
- {
- cout << "Введите день: ";
- cin >> d;
- if (arr[m - 1] < d || d < 1)
- {
- cout << "День введен не верно. Попробуйте еще раз!\n";
- f2 = 0;
- }
- else f2 = 1;
- }
- kik.m_day = d;
- kik.m_month = m;
- kik.m_year = y;
- return is;
- }
- ostream& operator<<(std::ostream& os, const Date& pip)
- {
- if (pip.m_day < 10 && pip.m_month < 10)os << "0" << pip.m_day
- << ".0" << pip.m_month << "." << pip.m_year;
- if(pip.m_day < 10 && pip.m_month>9)os << "0" << pip.m_day
- << "." << pip.m_month << "." << pip.m_year;
- if (pip.m_day > 9 && pip.m_month<10)os << pip.m_day
- << ".0" << pip.m_month << "." << pip.m_year;
- if (pip.m_day >9 && pip.m_month >9)os << pip.m_day
- << "." << pip.m_month << "." << pip.m_year;
- return os;
- }
- int kolvo_days(Date* t)
- {
- int d, y, m, kdays = 0;
- int arr[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 };
- if (t->m_year % 4 == 0)arr[1] = 29;
- cout << "Введите день: ";
- cin >> d;
- cout << "Введите месяц: ";
- cin >> m;
- cout << "Введите год: ";
- cin >> y;
- if (d > t->m_day)
- {
- kdays = d - t->m_day;
- m--;
- if (t->m_month < m)kdays += arr[m - 1];
- }
- if (d < t->m_day)
- {
- kdays = arr[t->m_month - 1] - t->m_day + d;
- m--;
- }
- if (m < 1)
- {
- m = 12; y--;
- }
- if (d > t->m_day)
- if (m == t->m_month)kdays += arr[m - 1];
- if (m > t->m_month)
- {
- while (m > t->m_month)
- {
- kdays += arr[m - 1];
- m--;
- }
- }
- if (m < t->m_month)
- if (y > t->m_year)
- {
- while (m > 0)
- {
- kdays += arr[m - 1];
- m--;
- }
- y--;
- m = 12;
- while (m > t->m_month)
- {
- kdays += arr[m - 1];
- m--;
- }
- }
- if (y > t->m_year)
- {
- while (y > t->m_year)
- {
- if (y % 4 == 0)kdays += 366;
- else kdays += 365;
- y--;
- }
- }
- return kdays;
- }
- int main()
- {
- setlocale(LC_ALL, "Russian");
- cout<< "Лабараторная работа №4" << '\n' << '\n';
- cout << "Копирование даты:" << '\n';
- Date op1;
- op1.get(&op1);
- Date op1a(12,3,2021);
- op1a.get(&op1a);
- op1a = op1;
- cout << "copy:"; op1a.get(&op1a) ;
- cout << "Добавление даты:" << '\n';
- Date op2;
- op2.get(&op2);
- Date op2a(17, 10, 2020);
- op2a.get(&op2a);
- op2a += op2;
- cout << "date is: ";
- op2a.get(&op2a);
- cout << "Вычитание даты:" << '\n';
- Date op3;
- op3.get(&op3);
- Date op3a(7, 1, 2019);
- op3a.get(&op3a);
- op3a -= op3;
- cout << "date is: ";
- op3a.get(&op3a);
- cout << "Вычитание дней:" << '\n';
- Date op4a(21, 11, 2020);
- op4a.get(&op4a);
- op4a -=29;
- cout << "date is: ";
- op4a.get(&op4a);
- cout << "Добавление дней:" << '\n';
- Date op5a(21, 11, 2020);
- op5a.get(&op5a);
- op5a += 80;
- cout << "date is: ";
- op5a.get(&op5a);
- cout << "Вывод:" << '\n';
- Date op6(18, 10, 2020);
- cout << op6;
- cout << '\n' << "Ввод:" << '\n';
- Date op7;
- cin >> op7;
- cout << op7;
- cout << '\n' << '\n' << "Лабараторная работа №3" << '\n' << '\n';
- cout << '\n' << "Работа конструктора по умолчанию:" << '\n';
- Date today;
- today.get(&today);
- cout << '\n' << "Работа конструктора с параметрами:" << '\n';
- Date today1(15, 3, 2020);
- today1.get(&today1);
- Date today2(op2);
- cout << today2;
- cout << '\n' << "Добавление к текущей дате дня/месяца/года " << '\n';
- Date nachdata;
- nachdata.set(nachdata);
- cout << '\n';
- nachdata.get(&nachdata);
- nachdata.dobavl(&nachdata);
- cout << '\n';
- Date razn;
- razn.set(razn);
- cout << '\n';
- razn.get(&razn);
- cout << "Количество дней между введенными датами составляет: " << kolvo_days(&razn)<<'\n';
- return 0;
- }
Add Comment
Please, Sign In to add comment