#include #include typedef struct MyDate { int year; int month; int day; } MyDate; class Date { public: Date(int year, int month, int day) { MyDate dt; dt.year = year; dt.month = month; dt.day = day; this->date = dt; this->isLeap = this->YearIsLeap(year); char ch[12] = { '\0' }; snprintf(ch, sizeof(ch), "%02d.%02d.%04d", dt.day, dt.month, dt.year); this->strFormat = {ch}; } bool IsLeap() const { return this->isLeap; } std::string ToString() const { return this->strFormat; } Date DaysLater(int days) { int year = this->date.year, month = this->date.month, day = this->date.day + days; int amount = this->DaysAmount(month, year); while (day > amount) { day -= amount; if (++month > 12) { month = 1; year += 1; } amount = this->DaysAmount(month, year); } return Date(year, month, day); } int DaysLeft(const Date& date) { MyDate dt = this->date; int a = (14 - dt.month) / 12; int y = dt.year + 4800 - a; int m = dt.month + 12*a - 3; int fr = dt.day + (153*m + 2) / 5 + 365*y + y/4 - y/100 + y/400 - 32045; dt = date.GetDate(); a = (14 - dt.month) / 12; y = dt.year + 4800 - a; m = dt.month + 12*a - 3; int sc = dt.day + (153*m + 2) / 5 + 365*y + y/4 - y/100 + y/400 - 32045; return abs(fr - sc); } MyDate GetDate() const { return this->date; } private: MyDate date; bool isLeap = true; std::string strFormat; int DaysAmount(int month, int year) { if (month < 8) { if (month == 2) { int res = 28; if (this->YearIsLeap(year)) res++; return res; } return 30 + (month % 2); } return 31 - (month % 2); } bool YearIsLeap(int year) { if (year % 4 != 0) return false; else if (year >= 100 && year % 100 == 0 && year % 400 != 0) return false; return true; } };