palenda21

4laboop

Dec 8th, 2020 (edited)
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 12.91 KB | None | 0 0
  1. #include <iostream>
  2. #include <math.h>
  3.  
  4. using namespace std;
  5.  
  6. class Date
  7. {
  8.     int m_day;
  9.     int m_month;
  10.     int m_year;
  11. public:
  12.     Date() //Конструктор по умолчанию
  13.     {
  14.         m_day = 19;
  15.         m_month = 10;
  16.         m_year = 2001;
  17.     }
  18.     Date(int day, int month, int year) //Конструктор с параметрами
  19.     {
  20.         m_day = day;
  21.         m_month = month;
  22.         m_year = year;
  23.     }
  24.     Date(const Date& other) //Конструктор копирования
  25.     {
  26.         this->m_day = other.m_day;
  27.         this->m_month = other.m_month;
  28.         this->m_year = other.m_year;
  29.         cout << '\n' << "Сработал конструктор копирования" << '\n'<<this << '\n';
  30.     }
  31.     void get(Date* t)  //Вывод
  32.     {
  33.         cout << "Дата: ";
  34.         if (t->m_day < 10)
  35.             cout << "0" << t->m_day << ".";
  36.         else
  37.             cout << t->m_day << ".";
  38.         if (t->m_month < 10)
  39.             cout << "0" << t->m_month << ".";
  40.         else
  41.             cout << t->m_month << ".";
  42.         cout << t->m_year << '\n';
  43.     }
  44.     void set(Date& today)    //Ввод
  45.     {
  46.         int  d, m, y;
  47.         bool f1, f2;
  48.         f1 = f2 = false;
  49.         cout << "Введите год: ";
  50.         cin >> y;
  51.         int arr[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 };
  52.         if (y % 4 == 0) arr[1] = 29;
  53.         while (f1 == 0)
  54.         {
  55.             cout << "Введите месяц: ";
  56.             cin >> m;
  57.             if (m < 1 || m>12)
  58.             {
  59.                 cout << "Месяц введен не верно. Попробуйте еще раз!\n";
  60.                 f1 = 0;
  61.             }
  62.             else f1 = 1;
  63.         }
  64.         while (f2 == 0)
  65.         {
  66.             cout << "Введите день: ";
  67.             cin >> d;
  68.             if (arr[m - 1] < d || d < 1)
  69.             {
  70.                 cout << "День введен не верно. Попробуйте еще раз!\n";
  71.                 f2 = 0;
  72.             }
  73.             else f2 = 1;
  74.         }
  75.         today.m_day = d;
  76.         today.m_month = m;
  77.         today.m_year = y;
  78.     }
  79.     int dobavl(Date* t)  //Добавление к текущей дате дня/ месяца/ года
  80.     {
  81.         while (true)
  82.         {
  83.             int a;
  84.                cout << '\n' << "Что Вы хотите сделать?" << '\n'
  85.                 << "1.Добавить некоторое количесво дней." << '\n'
  86.                 << "2.Добавить некоторое количество месяцев." << '\n'
  87.                 << "3.Добавить некоторое количество лет." << '\n'
  88.                 << "4.Выйти из меню." << '\n';
  89.             cin >> a;
  90.             int arr[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 };
  91.             if (m_year % 4 == 0)arr[1] = 29;
  92.             switch (a)
  93.             {
  94.             case 1:
  95.                 int day1;
  96.                 cout << "Сколько дней Вы хотите добавить(количество дней не должно превышать 31 дня)?\n";
  97.                 cin >> day1;
  98.  
  99.                 if (t->m_day + day1 > arr[t->m_day - 1])
  100.                 {
  101.                     t->m_day = day1 - arr[t->m_month - 1] + t->m_day;
  102.                     t->m_month++;
  103.                 }
  104.                 else
  105.                     t->m_day += day1;
  106.                 if (t->m_month > 12)
  107.                 {
  108.                     t->m_month = 1;
  109.                     t->m_year++;
  110.                 }
  111.                 break;
  112.             case 2:
  113.                 int month1;
  114.                 cout << "Сколько месяцев Вы хотите добавить(количество месяцев не должно превышать 12 месяцев)?\n";
  115.                 cin >> month1;
  116.                 if (t->m_month + month1 > 12)
  117.                 {
  118.                     t->m_month = t->m_month+month1-12;
  119.                     t->m_year++;
  120.                 }
  121.                 else t->m_month += month1;
  122.                 break;
  123.             case 3:
  124.                 int year1;
  125.                 cout << "Сколько лет вы хотите добавить?\n";
  126.                 cin >> year1;
  127.                 t->m_year += year1;
  128.                 break;
  129.             case 4: get(t);
  130.                 return 1;
  131.             default: cout << "Выберите правильную функцию из предложенного меню."; break;
  132.             }
  133.         }
  134.         return 0;
  135.     }
  136.     friend int kolvo_days(Date*); //Дружественная функция для нахождения количества лней между датами
  137.     Date& operator=(const Date& data)  // копирование даты
  138.     {
  139.         this->m_day = data.m_day;
  140.         this->m_month = data.m_month;
  141.         this->m_year = data.m_year;
  142.         return *this;
  143.     }
  144.     Date& operator+=(const Date& atata) //добавление даты
  145.     {
  146.         int arr[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 };
  147.         if (this->m_year % 4 == 0)arr[1] = 29;
  148.         this->m_year += atata.m_year;
  149.         if (this->m_month + atata.m_month > 12)
  150.         {
  151.             this->m_month= this->m_month+ atata.m_month - 12;
  152.             this->m_year++;
  153.         }
  154.         else  this->m_month += atata.m_month;
  155.         if (this->m_day + atata.m_day > arr[this->m_month - 1])
  156.         {
  157.             this->m_day = this->m_day + atata.m_day - arr[this->m_month - 1];
  158.             this->m_month++;
  159.         }
  160.         else this->m_day += atata.m_day;
  161.         if (this->m_month > 12)
  162.         {
  163.             this->m_month -= 12;
  164.             this->m_year++;
  165.         }
  166.         return *this;
  167.     }
  168.     Date& operator-=(const Date& lal)       // вычитание даты
  169.     {
  170.         int arr[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 };
  171.         if (this->m_year % 4 == 0)arr[1] = 29;
  172.        
  173.             this->m_year =abs( this->m_year - lal.m_year);
  174.             if (this->m_month - lal.m_month < 0)
  175.             {
  176.                 this->m_month = this->m_month - lal.m_month + 12;
  177.                 this->m_year--;
  178.             }
  179.             else this->m_month = this->m_month - lal.m_month;
  180.            
  181.             if (this->m_day - lal.m_day < 1)
  182.             {
  183.                 this->m_day = this->m_day - lal.m_day + arr[this->m_month - 2];
  184.                 this->m_month--;
  185.             }
  186.             else this->m_day = this->m_day - lal.m_day;
  187.             if (this->m_month < 1)
  188.             {
  189.                 this->m_month += 12;
  190.                 this->m_year--;
  191.             }
  192.         return *this;
  193.     }
  194.     Date& operator-=(int k) // вычитание дней
  195.     {
  196.         int arr[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 };
  197.         if (this->m_year % 4 == 0)arr[1] = 29;
  198.         while (k > 0)
  199.         {
  200.             this->m_day--;
  201.             k--;
  202.             if (this->m_day < 1)
  203.             {
  204.                 this->m_month--;
  205.                 if (this->m_month < 1)
  206.             {
  207.                 this->m_month += 12;
  208.                 this->m_year--;
  209.             }
  210.                 this->m_day =arr[this->m_month-1];
  211.             }
  212.            
  213.         }
  214.         return *this;
  215.     }
  216.     Date& operator+=(int k)// добавление дней
  217.     {
  218.         int arr[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 };
  219.         if (this->m_year % 4 == 0)arr[1] = 29;
  220.         while (k > 0)
  221.         {
  222.             this->m_day++;
  223.             k--;
  224.             if (this->m_day > arr[this->m_month - 1])
  225.             {
  226.                 this->m_month++;
  227.                 this->m_day = 1;
  228.                 if (this->m_month > 12)
  229.                 {
  230.                     this->m_month = 1;
  231.                     this->m_year++;
  232.                 }
  233.             }
  234.         }
  235.         return *this;
  236.     }
  237.     friend std::ostream& operator<<( std::ostream& os, const Date&);
  238.     friend std::istream& operator>>(std::istream& is, Date&);
  239. };
  240. istream& operator>>(std::istream& is, Date& kik)
  241. {
  242.     int  d, m, y;
  243.     bool f1, f2;
  244.     f1 = f2 = false;
  245.     cout << "Введите год: ";
  246.     cin >> y;
  247.     int arr[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 };
  248.     if (y % 4 == 0) arr[1] = 29;
  249.     while (f1 == 0)
  250.     {
  251.         cout << "Введите месяц: ";
  252.         cin >> m;
  253.         if (m < 1 || m>12)
  254.         {
  255.             cout << "Месяц введен не верно. Попробуйте еще раз!\n";
  256.             f1 = 0;
  257.         }
  258.         else f1 = 1;
  259.     }
  260.     while (f2 == 0)
  261.     {
  262.         cout << "Введите день: ";
  263.         cin >> d;
  264.         if (arr[m - 1] < d || d < 1)
  265.         {
  266.             cout << "День введен не верно. Попробуйте еще раз!\n";
  267.             f2 = 0;
  268.         }
  269.         else f2 = 1;
  270.     }
  271.     kik.m_day = d;
  272.     kik.m_month = m;
  273.     kik.m_year = y;
  274.     return is;
  275.  
  276. }
  277. ostream& operator<<(std::ostream& os, const Date& pip)
  278. {
  279.     if (pip.m_day < 10 && pip.m_month < 10)os << "0" << pip.m_day
  280.         << ".0" << pip.m_month << "." << pip.m_year;
  281.     if(pip.m_day < 10 && pip.m_month>9)os << "0" << pip.m_day
  282.         << "." << pip.m_month << "." << pip.m_year;
  283.     if (pip.m_day > 9 && pip.m_month<10)os << pip.m_day
  284.         << ".0" << pip.m_month << "." << pip.m_year;
  285.     if (pip.m_day >9 && pip.m_month >9)os << pip.m_day
  286.         << "." << pip.m_month << "." << pip.m_year;
  287.     return os;
  288.  
  289. }
  290.  
  291. int kolvo_days(Date* t)
  292. {
  293.     int d, y, m, kdays = 0;
  294.     int arr[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 };
  295.     if (t->m_year % 4 == 0)arr[1] = 29;
  296.     cout << "Введите день: ";
  297.     cin >> d;
  298.     cout << "Введите месяц: ";
  299.     cin >> m;
  300.     cout << "Введите год: ";
  301.     cin >> y;
  302.     if (d > t->m_day)
  303.     {
  304.         kdays = d - t->m_day;
  305.         m--;
  306.         if (t->m_month < m)kdays += arr[m - 1];
  307.     }
  308.     if (d < t->m_day)
  309.     {
  310.         kdays = arr[t->m_month - 1] - t->m_day + d;
  311.         m--;
  312.     }
  313.     if (m < 1)
  314.     {
  315.         m = 12; y--;
  316.     }
  317.     if (d > t->m_day)
  318.         if (m == t->m_month)kdays += arr[m - 1];
  319.     if (m > t->m_month)
  320.     {
  321.         while (m > t->m_month)
  322.         {
  323.             kdays += arr[m - 1];
  324.             m--;
  325.         }
  326.     }
  327.     if (m < t->m_month)
  328.         if (y > t->m_year)
  329.         {
  330.             while (m > 0)
  331.             {
  332.                 kdays += arr[m - 1];
  333.                 m--;
  334.             }
  335.             y--;
  336.             m = 12;
  337.             while (m > t->m_month)
  338.             {
  339.                 kdays += arr[m - 1];
  340.                 m--;
  341.             }
  342.         }
  343.     if (y > t->m_year)
  344.     {
  345.         while (y > t->m_year)
  346.         {
  347.             if (y % 4 == 0)kdays += 366;
  348.             else kdays += 365;
  349.             y--;
  350.         }
  351.     }
  352.     return kdays;
  353. }
  354.  
  355. int main()
  356. {
  357.     setlocale(LC_ALL, "Russian");
  358.     cout<< "Лабараторная работа №4" << '\n' << '\n';
  359.     cout << "Копирование даты:" << '\n';
  360.     Date op1;
  361.     op1.get(&op1);
  362.     Date op1a(12,3,2021);
  363.     op1a.get(&op1a);
  364.     op1a = op1;
  365.     cout << "copy:"; op1a.get(&op1a) ;
  366.     cout << "Добавление даты:" << '\n';
  367.     Date op2;
  368.     op2.get(&op2);
  369.     Date op2a(17, 10, 2020);
  370.     op2a.get(&op2a);
  371.     op2a += op2;
  372.     cout << "date is: ";
  373.     op2a.get(&op2a);
  374.     cout << "Вычитание даты:" << '\n';
  375.     Date op3;
  376.     op3.get(&op3);
  377.     Date op3a(7, 1, 2019);
  378.     op3a.get(&op3a);
  379.     op3a -= op3;
  380.     cout << "date is: ";
  381.     op3a.get(&op3a);
  382.     cout << "Вычитание дней:" << '\n';
  383.     Date op4a(21, 11, 2020);
  384.     op4a.get(&op4a);
  385.     op4a -=29;
  386.     cout << "date is: ";
  387.     op4a.get(&op4a);
  388.     cout << "Добавление дней:" << '\n';
  389.     Date op5a(21, 11, 2020);
  390.     op5a.get(&op5a);
  391.     op5a += 80;
  392.     cout << "date is: ";
  393.     op5a.get(&op5a);
  394.     cout << "Вывод:" << '\n';
  395.     Date op6(18, 10, 2020);
  396.     cout << op6;
  397.     cout << '\n' << "Ввод:" << '\n';
  398.     Date op7;
  399.     cin >> op7;
  400.     cout << op7;
  401.     cout << '\n' << '\n' << "Лабараторная работа №3" << '\n' << '\n';
  402.     cout << '\n' << "Работа конструктора по умолчанию:" << '\n';
  403.     Date today;
  404.     today.get(&today);
  405.     cout << '\n' << "Работа конструктора с параметрами:" << '\n';
  406.     Date today1(15, 3, 2020);
  407.     today1.get(&today1);
  408.     Date today2(op2);
  409.     cout << today2;
  410.     cout << '\n' << "Добавление к текущей дате дня/месяца/года " << '\n';
  411.     Date nachdata;
  412.     nachdata.set(nachdata);
  413.     cout << '\n';
  414.     nachdata.get(&nachdata);
  415.     nachdata.dobavl(&nachdata);
  416.     cout << '\n';
  417.     Date razn;
  418.     razn.set(razn);
  419.     cout << '\n';
  420.     razn.get(&razn);
  421.     cout << "Количество дней между введенными датами составляет: " << kolvo_days(&razn)<<'\n';
  422.     return 0;
  423. }
  424.  
Add Comment
Please, Sign In to add comment