Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <math.h>
- using namespace std;
- class Date
- {
- int y; //год
- int m; //месяц
- int d; //день
- friend int friend_dec(Date&, Date&);
- public:
- Date()//конструктор по умолчанию
- {
- y = 0;
- m = 0;
- d = 0;
- }
- Date(int y_y, int m_m, int d_d)//конструктор по параметрам
- {
- y = y_y;
- m = m_m;
- d = d_d;
- }
- // Конструктор копирования
- Date(const Date& drob) :
- y(drob.y), m(drob.m), d(drob.d)
- {
- cout << "Copy constructor worked here!\n"; // просто, чтобы показать, что это работает
- }
- void set(Date& new_date);
- void get(Date& time);
- void add(Date& time);
- };
- void Date::get(Date& time)
- {
- cout << "Here is your data: ";
- cout << time.d << "." << time.m << "." << time.y << endl;
- }
- void Date::set(Date& new_date)
- {
- int d_in_m[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 };
- int day, month, year, res_d, res_m;
- cout << "Input year: ";
- cin >> year;
- do {
- cout << "Input month: ";
- cin >> month;
- if (month < 1 || month > 12) {
- cout << "Bad months input.Try again.n";
- res_m = 0;
- }
- else res_m = 1;
- } while (res_m == 0);
- do {
- cout << "Input day: ";
- cin >> day;
- if (d_in_m[month - 1] < 1 || day > d_in_m[month - 1]) {
- cout << "Bad days input.Try again.n";
- res_d = 0;
- }
- else res_d = 1;
- } while (res_d == 0);
- new_date.d = day;
- new_date.m = month;
- new_date.y = year;
- }
- void Date::add(Date& time)
- {
- int day, param, month, year, res_d, res_m;
- cout << "Choose: 1 - add year, 2 - add month, 3 - add day\n";
- cin >> param;
- switch (param)
- {
- case 1:
- cout << "Input year to add: ";
- cin >> year;
- time.y += year;
- break;
- case 2:
- do {
- cout << "Input month to add: ";
- cin >> month;
- if (month < 0) {
- cout << "Bad months input.Try again.\n";
- res_m = 0;
- }
- else res_m = 1;
- } while (res_m == 0);
- if (time.m + month > 12) {
- int dop = 12 - time.m;
- time.y++;
- time.m = 0;
- int ostatok = month % 12 - dop;
- time.m += ostatok;
- int dop_years = month / 12;
- time.y += dop_years;
- }
- else {
- time.m += month;
- }
- break;
- case 3:
- do {
- cout << "Input day: ";
- cin >> day;
- if (day < 0) {
- cout << "Bad days input.Try again.\n";
- res_d = 0;
- }
- else res_d = 1;
- } while (res_d == 0);
- if (day / 365 > 0) time.y += day / 365;
- if (day / 30 > 0)
- {
- time.m += day / 30;
- if (time.m > 12)
- {
- time.y += time.m / 12;
- time.m %= 12;
- }
- }
- else
- {
- time.d += day;
- if (time.d / 30 > 0) time.m++;
- }
- break;
- default: cout << "Bad input.n"; break;
- }
- }
- int friend_dec(Date& time, Date& new_time) /*разность дат*/
- {
- int d_in_m[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 };
- int days_in_current_months = 0;
- for (int i = 0; i < time.m; i++) days_in_current_months += d_in_m[i];
- days_in_current_months += time.d;
- int gl_time = 365 * time.y + days_in_current_months;
- int days_in_current_new_months = 0;
- for (int i = 0; i < new_time.m; i++) days_in_current_new_months += d_in_m[i];
- days_in_current_new_months += new_time.d;
- int new_gl_time = 365 * new_time.y + days_in_current_new_months;
- return fabs(gl_time - new_gl_time);
- }
- int main()
- {
- Date time;
- time.set(time);
- time.get(time);
- Date new_time;
- new_time.set(new_time);
- new_time.get(new_time);
- cout << "Diferrense in days: " << friend_dec(time, new_time) << endl;
- int i = 0;
- int param;
- cout << "Chose how much ops you want to do: ";
- cin >> param;
- do {
- time.add(time);
- i++;
- } while (i < param);
- time.get(time);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement