Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Time.h
- #ifndef TIME_H
- #define TIME_H
- #define LL long long
- class Time
- {
- public:
- virtual ~Time() = default;
- Time(LL, LL, LL);
- void set_hours(LL);
- void set_min(LL);
- void set_sec(LL);
- LL get_hours() const;
- LL get_min() const;
- LL get_sec() const;
- void change_sec(LL);
- protected:
- virtual void change_day(LL);
- private:
- LL hour_;
- LL min_;
- LL sec_;
- };
- #endif // TIME_H
- //Time.cpp
- #include "Time.h"
- Time::Time(const LL hour, const LL min, const LL sec)
- {
- hour_ = hour;
- min_ = min;
- sec_ = sec;
- }
- void Time::set_hours(const LL hour)
- {
- if (hour < 24 && hour > -1)
- hour_ = hour;
- }
- void Time::set_min(const LL min)
- {
- if (min < 60 && min > -1)
- min_ = min;
- }
- void Time::set_sec(const LL sec)
- {
- if (sec < 60 && sec > -1)
- sec_ = sec;
- }
- LL Time::get_hours() const
- {
- const LL hours = hour_;
- return hours;
- }
- LL Time::get_min() const
- {
- const LL min = min_;
- return min;
- }
- LL Time::get_sec() const
- {
- const LL sec = sec_;
- return sec;
- }
- void Time::change_day(LL)
- {
- }
- void Time::change_sec(const LL change)
- {
- LL t = hour_ * 3600 + min_ * 60 + sec_;
- t += change;
- sec_ = (t % 60 + 60) % 60;
- min_ = ((t / 60) % 60 + 60) % 60;
- hour_ = ((t / 3600) % 24 + 24) % 24;
- const LL days = (t + change) / 60 / 60 / 24;
- change_day(days);
- }
- //Date.h
- #ifndef DATE_H
- #define DATE_H
- #include "Time.h"
- class Date : public Time
- {
- public:
- Date(LL, LL, LL, LL, LL, LL);
- void set_date(LL, LL, LL);
- LL get_year() const;
- LL get_month() const;
- LL get_day() const;
- static bool leap(LL);
- void shift_day(LL);
- protected:
- void change_day(const LL d) override { shift_day(d); }
- private:
- LL day_;
- LL month_;
- LL year_;
- LL collect_day() const;
- };
- #endif // DATE_H
- //Date.cpp
- #include "Date.h"
- #include <stdexcept>
- #define OST(X, Y) (((X) % (Y) + (Y)) % (Y))
- #define ABS(X) ((X) > 0) ? (X) : (-(X))
- #define SWAP(X, Y, T) T SWAP = X; (X) = Y; (Y) = SWAP;
- Date::Date(const LL year, const LL month_th, const LL day, const LL hour, const LL min, const LL sec) : Time(hour, min, sec)
- {
- year_ = year;
- month_ = month_th;
- day_ = day;
- }
- bool Date::leap(const LL year)
- {
- return year % 4 == 0 && year % 100 != 0 || year % 400 == 0;
- }
- void Date::set_date(const LL year, const LL month_th, const LL day)
- {
- if (year != 0) year_ = year;
- else
- {
- throw std::invalid_argument("Incorrect year");
- return;
- }
- if (month_th > 0 && month_th < 13) month_ = month_th;
- else
- {
- throw std::invalid_argument("Incorrect month");
- return;
- }
- if (day > 0)
- {
- switch (month_th)
- {
- case 1: // январь
- if (day < 32) day_ = day;
- else throw std::invalid_argument("Incorrect day");
- return;
- case 2: // февраль
- if (day < 30)
- {
- if (leap(year)) day_ = day;
- else if (day < 29) day_ = day;
- else throw std::invalid_argument("Incorrect day. Selected year is not leap");
- }
- else throw std::invalid_argument("Incorrect day");
- return;
- case 3: // март
- if (day < 32) day_ = day;
- else throw std::invalid_argument("Incorrect day");
- return;
- case 4: // апрель
- if (day < 31) day_ = day;
- else throw std::invalid_argument("Incorrect day");
- return;
- case 5: // май
- if (day < 32) day_ = day;
- else throw std::invalid_argument("Incorrect day");
- return;
- case 6: // июнь
- if (day < 31) day_ = day;
- else throw std::invalid_argument("Incorrect day");
- return;
- case 7: // июль
- if (day < 32) day_ = day;
- else throw std::invalid_argument("Incorrect day");
- return;
- case 8: // август
- if (day < 32) day_ = day;
- else throw std::invalid_argument("Incorrect day");
- return;
- case 9: // сентябрь
- if (day < 31) day_ = day;
- else throw std::invalid_argument("Incorrect day");
- return;
- case 10: // октябрь
- if (day < 32) day_ = day;
- else throw std::invalid_argument("Incorrect day");
- return;
- case 11: // ноябрь
- if (day < 31) day_ = day;
- else throw std::invalid_argument("Incorrect day");
- return;
- default: // декабрь
- if (day < 32) day_ = day;
- else throw std::invalid_argument("Incorrect day");
- return;
- }
- }
- else
- {
- throw std::invalid_argument("Incorrect day");
- return;
- }
- }
- LL Date::get_year() const
- {
- const LL year = year_;
- return year;
- }
- LL Date::get_month() const
- {
- const LL month_th = month_;
- return month_th;
- }
- LL Date::get_day() const
- {
- const LL day = day_;
- return day;
- }
- LL Date::collect_day() const
- {
- LL collect = 0; // текущее кол-во дней относительно 0
- if (year_ < 0)
- {
- collect--; // анти 0 день
- LL y = -1; // считаем год
- while (y > year_)
- {
- collect -= leap(y) ? 366 : 365;
- y--;
- }
- switch (month_)
- { // месяца идут наоборот
- case 12: collect -= (31 - day_); break;
- case 11: collect -= 31 + (30 - day_); break;
- case 10: collect -= 31 + 30 + (31 - day_); break;
- case 9: collect -= 31 + 30 + 31 + (30 - day_); break;
- case 8: collect -= 31 + 30 + 31 + 30 + (31 - day_); break;
- case 7: collect -= 31 + 30 + 31 + 30 + 31 + (31 - day_); break;
- case 6: collect -= 31 + 30 + 31 + 30 + 31 + 30 + (30 - day_); break;
- case 5: collect -= 31 + 30 + 31 + 30 + 31 + 30 + 31 + (31 - day_); break;
- case 4: collect -= 31 + 30 + 31 + 30 + 31 + 30 + 31 + 31 + (30 - day_); break;
- case 3: collect -= 31 + 30 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + (31 - day_); break;
- case 2: collect -= 31 + 30 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + (29 - day_); break;
- default: collect -= 31 + 30 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 29 + (31 - day_); break;
- }
- if (!leap(y) && month_ < 4) collect++;
- }
- else // year_ > 0
- {
- collect += day_; // прибавляя день, мы автоматом определяем знак (т.е. нет нужды collect++)
- LL y = 1; // считаем год
- while (y < year_)
- {
- collect += leap(y) ? 366 : 365;
- y++;
- }
- switch (month_)
- { // а здесь норм
- case 1: break;
- case 2: collect += 31; break;
- case 3: collect += 31 + 29; break;
- case 4: collect += 31 + 29 + 31; break;
- case 5: collect += 31 + 29 + 31 + 30; break;
- case 6: collect += 31 + 29 + 31 + 30 + 31; break;
- case 7: collect += 31 + 29 + 31 + 30 + 31 + 30; break;
- case 8: collect += 31 + 29 + 31 + 30 + 31 + 30 + 31; break;
- case 9: collect += 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31; break;
- case 10: collect += 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30; break;
- case 11: collect += 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31; break;
- default: collect += 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30; break;
- }
- if (!leap(y) && month_ > 2) collect--;
- }
- return collect;
- }
- void Date::shift_day(const LL change)
- {
- if (change == 0) return;
- LL collect = collect_day();
- const LL save = collect;
- collect += change; // изменили кол-во дней
- if (save > 0 && collect <= 0) collect--;
- else if (save < 0 && collect >= 0) collect++; // вычёркивание 0 дня
- if (collect < 0)
- {
- year_ = -1;
- while (ABS(collect) >= 366)
- {
- if (ABS(collect) == 366) break;
- collect += leap(year_) ? 366 : 365;
- year_--;
- }
- if (ABS(collect) == 366 && !leap(year_))
- {
- collect += 365;
- year_--;
- }
- if (ABS(collect) <= 31)
- {
- month_ = 12;
- day_ = 31 - (ABS(collect));
- }
- else if (ABS(collect) <= 31 + 30)
- {
- month_ = 11;
- collect += 31;
- day_ = 30 - (ABS(collect));
- }
- else if (ABS(collect) <= 31 + 30 + 31)
- {
- month_ = 10;
- collect += 31 + 30;
- day_ = 31 - (ABS(collect));
- }
- else if (ABS(collect) <= 31 + 30 + 31 + 30)
- {
- month_ = 9;
- collect += 31 + 30 + 31;
- day_ = 30 - (ABS(collect));
- }
- else if (ABS(collect) <= 31 + 30 + 31 + 30 + 31)
- {
- month_ = 8;
- collect += 31 + 30 + 31 + 30;
- day_ = 31 - (ABS(collect));
- }
- else if (ABS(collect) <= 31 + 30 + 31 + 30 + 31 + 31)
- {
- month_ = 7;
- collect += 31 + 30 + 31 + 30 + 31;
- day_ = 31 - (ABS(collect));
- }
- else if (ABS(collect) <= 31 + 30 + 31 + 30 + 31 + 31 + 30)
- {
- month_ = 6;
- collect += 31 + 30 + 31 + 30 + 31 + 31;
- day_ = 30 - (ABS(collect));
- }
- else if (ABS(collect) <= 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31)
- {
- month_ = 5;
- collect += 31 + 30 + 31 + 30 + 31 + 31 + 30;
- day_ = 31 - (ABS(collect));
- }
- else if (ABS(collect) <= 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30)
- {
- month_ = 4;
- collect += 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31;
- day_ = 30 - (ABS(collect));
- }
- else if (ABS(collect) <= 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31)
- {
- month_ = 3;
- collect += 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30;
- day_ = 31 - (ABS(collect));
- }
- else
- {
- if (leap(year_))
- {
- if (ABS(collect) <= 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31 + 29)
- {
- month_ = 2;
- collect += 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31;
- day_ = 29 - (ABS(collect));
- }
- else
- {
- month_ = 1;
- collect += 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31 + 29;
- day_ = 31 - (ABS(collect));
- }
- }
- else
- {
- if (ABS(collect) <= 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31 + 28)
- {
- month_ = 2;
- collect += 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31;
- day_ = 28 - (ABS(collect));
- }
- else
- {
- month_ = 1;
- collect += 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31 + 28;
- day_ = 31 - (ABS(collect));
- }
- }
- }
- day_++; // считаем от 0 дня
- return;
- }
- else //collect > 0
- {
- year_ = 1;
- while (collect >= 366)
- {
- if (collect == 366) break;
- collect -= leap(year_) ? 366 : 365;
- year_++;
- }
- if(collect == 366 && !leap(year_))
- {
- collect -= 365;
- year_++;
- }
- if (collect <= 31) { month_ = 1; }
- else
- {
- if (leap(year_))
- {
- if (collect <= 31 + 29)
- {
- month_ = 2;
- collect -= 31;
- }
- else if (collect <= 31 + 29 + 31)
- {
- month_ = 3;
- collect -= 31 + 29;
- }
- else if (collect <= 31 + 29 + 31 + 30)
- {
- month_ = 4;
- collect -= 31 + 29 + 31;
- }
- else if (collect <= 31 + 29 + 31 + 30 + 31)
- {
- month_ = 5;
- collect -= 31 + 29 + 31 + 30;
- }
- else if (collect <= 31 + 29 + 31 + 30 + 31 + 30)
- {
- month_ = 6;
- collect -= 31 + 29 + 31 + 30 + 31;
- }
- else if (collect <= 31 + 29 + 31 + 30 + 31 + 30 + 31)
- {
- month_ = 7;
- collect -= 31 + 29 + 31 + 30 + 31 + 30;
- }
- else if (collect <= 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31)
- {
- month_ = 8;
- collect -= 31 + 29 + 31 + 30 + 31 + 30 + 31;
- }
- else if (collect <= 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30)
- {
- month_ = 9;
- collect -= 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31;
- }
- else if (collect <= 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31)
- {
- month_ = 10;
- collect -= 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30;
- }
- else if (collect <= 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30)
- {
- month_ = 11;
- collect -= 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31;
- }
- else
- {
- month_ = 12;
- collect -= 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30;
- }
- }
- else
- {
- if (collect <= 31 + 28)
- {
- month_ = 2;
- collect -= 31;
- }
- else if (collect <= 31 + 28 + 31)
- {
- month_ = 3;
- collect -= 31 + 28;
- }
- else if (collect <= 31 + 28 + 31 + 30)
- {
- month_ = 4;
- collect -= 31 + 28 + 31;
- }
- else if (collect <= 31 + 28 + 31 + 30 + 31)
- {
- month_ = 5;
- collect -= 31 + 28 + 31 + 30;
- }
- else if (collect <= 31 + 28 + 31 + 30 + 31 + 30)
- {
- month_ = 6;
- collect -= 31 + 28 + 31 + 30 + 31;
- }
- else if (collect <= 31 + 28 + 31 + 30 + 31 + 30 + 31)
- {
- month_ = 7;
- collect -= 31 + 28 + 31 + 30 + 31 + 30;
- }
- else if (collect <= 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31)
- {
- month_ = 8;
- collect -= 31 + 28 + 31 + 30 + 31 + 30 + 31;
- }
- else if (collect <= 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30)
- {
- month_ = 9;
- collect -= 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31;
- }
- else if (collect <= 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31)
- {
- month_ = 10;
- collect -= 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30;
- }
- else if (collect <= 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30)
- {
- month_ = 11;
- collect -= 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31;
- }
- else
- {
- month_ = 12;
- collect -= 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30;
- }
- }
- }
- day_ = collect;
- return;
- }
- }
- //main.cpp
- #include <cstdio>
- #include <clocale>
- #include "Date.h"
- int main()
- {
- setlocale(LC_ALL, "Russian");
- Date one(0, 0, 0, 0, 0, 0);
- LL y = 0; // год
- LL m = 0; // месяц
- LL d = 0; // день
- LL sec = 0; // изменение сек
- LL days = 0; // изменение дней
- LL s = 0; // сек
- LL min = 0; // мин
- LL h = 0; // часы
- printf("Введи часы: ");
- scanf("%lld", &h);
- printf("Введи минуты: ");
- scanf("%lld", &min);
- printf("Введи секунды: ");
- scanf("%lld", &s);
- printf("Введи год: ");
- scanf("%lld", &y);
- printf("Введи месяц: ");
- scanf("%lld", &m);
- printf("Введи день: ");
- scanf("%lld", &d);
- one.set_date(y, m, d);
- one.set_hours(h);
- one.set_min(min);
- one.set_sec(s);
- printf("Введите кол-во секунд, на которое хотите изменить время: ");
- scanf("%lld", &sec);
- printf("Введите кол-во дней, на которое хотите изменить время: ");
- scanf("%lld", &days);
- one.change_sec(sec);
- one.shift_day(days);
- printf("Новая дата: год %lld : месяц %lld : день %lld\n часы %lld : минуты %lld : секунды %lld\n", one.get_year(), one.get_month(), one.get_day(), one.get_hours(), one.get_min(), one.get_sec());
- return 0;
- }
Add Comment
Please, Sign In to add comment