Advertisement
palenda21

2-3laboop

Dec 15th, 2020
616
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.43 KB | None | 0 0
  1. #include <iostream>
  2. #include <math.h>
  3. using namespace std;
  4. class Date
  5. {
  6.     int y;     //год
  7.     int m;     //месяц
  8.     int d;     //день
  9.     friend int friend_dec(Date&, Date&);
  10. public:
  11.     Date()//конструктор по умолчанию
  12.     {
  13.         y = 0;
  14.         m = 0;
  15.         d = 0;
  16.     }
  17.     Date(int y_y, int m_m, int d_d)//конструктор по параметрам
  18.     {
  19.         y = y_y;
  20.         m = m_m;
  21.         d = d_d;
  22.     }
  23.     // Конструктор копирования
  24.     Date(const Date& drob) :
  25.         y(drob.y), m(drob.m), d(drob.d)
  26.     {
  27.        
  28.         cout << "Copy constructor worked here!\n"; // просто, чтобы показать, что это работает
  29.     }
  30.     void set(Date& new_date);
  31.     void get(Date& time);
  32.     void add(Date& time);
  33. };
  34.  
  35. void Date::get(Date& time)
  36. {
  37.     cout << "Here is your data: ";
  38.     cout << time.d << "." << time.m << "." << time.y << endl;
  39. }
  40. void Date::set(Date& new_date)
  41. {
  42.     int d_in_m[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 };
  43.     int day, month, year, res_d, res_m;
  44.     cout << "Input year: ";
  45.     cin >> year;
  46.     do {
  47.         cout << "Input month: ";
  48.         cin >> month;
  49.         if (month < 1 || month > 12) {
  50.             cout << "Bad months input.Try again.n";
  51.             res_m = 0;
  52.         }
  53.         else res_m = 1;
  54.     } while (res_m == 0);
  55.     do {
  56.         cout << "Input day: ";
  57.         cin >> day;
  58.         if (d_in_m[month - 1] < 1 || day > d_in_m[month - 1]) {
  59.             cout << "Bad days input.Try again.n";
  60.             res_d = 0;
  61.         }
  62.         else res_d = 1;
  63.     } while (res_d == 0);
  64.  
  65.     new_date.d = day;
  66.     new_date.m = month;
  67.     new_date.y = year;
  68.  
  69. }
  70. void  Date::add(Date& time)
  71. {
  72.     int day, param, month, year, res_d, res_m;
  73.  
  74.  
  75.     cout << "Choose: 1 - add year, 2 - add month, 3 - add day\n";
  76.     cin >> param;
  77.     switch (param)
  78.     {
  79.  
  80.     case 1:
  81.         cout << "Input year to add: ";
  82.         cin >> year;
  83.         time.y += year;
  84.         break;
  85.     case 2:
  86.         do {
  87.             cout << "Input month to add: ";
  88.             cin >> month;
  89.             if (month < 0) {
  90.                 cout << "Bad months input.Try again.\n";
  91.                 res_m = 0;
  92.             }
  93.             else res_m = 1;
  94.         } while (res_m == 0);
  95.         if (time.m + month > 12) {
  96.             int dop = 12 - time.m;
  97.             time.y++;
  98.             time.m = 0;
  99.             int ostatok = month % 12 - dop;
  100.             time.m += ostatok;
  101.             int dop_years = month / 12;
  102.             time.y += dop_years;
  103.         }
  104.         else {
  105.             time.m += month;
  106.         }
  107.         break;
  108.  
  109.     case 3:
  110.         do {
  111.             cout << "Input day: ";
  112.             cin >> day;
  113.             if (day < 0) {
  114.                 cout << "Bad days input.Try again.\n";
  115.                 res_d = 0;
  116.             }
  117.             else res_d = 1;
  118.         } while (res_d == 0);
  119.  
  120.         if (day / 365 > 0) time.y += day / 365;
  121.         if (day / 30 > 0)
  122.         {
  123.             time.m += day / 30;
  124.             if (time.m > 12)
  125.             {
  126.                 time.y += time.m / 12;
  127.                 time.m %= 12;
  128.             }
  129.         }
  130.         else
  131.         {
  132.             time.d += day;
  133.             if (time.d / 30 > 0) time.m++;
  134.         }
  135.         break;
  136.  
  137.     default: cout << "Bad input.n"; break;
  138.     }
  139.  
  140. }
  141. int friend_dec(Date& time, Date& new_time)    /*разность дат*/
  142. {
  143.     int d_in_m[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 };
  144.     int days_in_current_months = 0;
  145.     for (int i = 0; i < time.m; i++) days_in_current_months += d_in_m[i];
  146.     days_in_current_months += time.d;
  147.     int gl_time = 365 * time.y + days_in_current_months;
  148.     int days_in_current_new_months = 0;
  149.     for (int i = 0; i < new_time.m; i++) days_in_current_new_months += d_in_m[i];
  150.     days_in_current_new_months += new_time.d;
  151.     int new_gl_time = 365 * new_time.y + days_in_current_new_months;
  152.     return fabs(gl_time - new_gl_time);
  153. }
  154. int main()
  155. {
  156.     Date time;
  157.     time.set(time);
  158.     time.get(time);
  159.     Date new_time;
  160.     new_time.set(new_time);
  161.     new_time.get(new_time);
  162.     cout << "Diferrense in days: " << friend_dec(time, new_time) << endl;
  163.     int i = 0;
  164.     int param;
  165.     cout << "Chose how much ops you want to do: ";
  166.     cin >> param;
  167.     do {
  168.         time.add(time);
  169.         i++;
  170.     } while (i < param);
  171.     time.get(time);
  172.     return 0;
  173. }
  174.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement