1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
| #include <iostream> #include <cstdio> using namespace std;
bool isLeap(int y){ return (y%4==0 &&y%100!=0) || (y%400==0); }
int month[13][2] = { {0,0},{31,31},{28,29},{31,31},{30,30},{31,31},{30,30}, {31,31},{31,31},{30,30},{31,31},{30,30},{31,31} };
class Date{ public: Date(){ y = 2000,m = 1,d = 1; } Date(int _y,int _m,int _d): y(_y),m(_m),d(_d){} Date(Date& t){ y = t.y,m = t.m,d = t.d; } void print(){ printf("Y:%d,M:%2d,D:%2d\n",y,m,d); } friend Date operator+ (Date& D,int n); friend Date operator++ (Date& D,int); friend Date operator++ (Date& D); private: int y; int m; int d; }; Date operator+ (Date& D,int n){ Date temp = D; int ans = 0; while (ans < n){ if (n- ans > 366){ if (isLeap(temp.y)) ans += 366; else ans += 365; temp.y ++; continue; } temp.d ++; if (temp.d == month[temp.m][isLeap(temp.y)]+1){ temp.d = 1;temp.m ++; } if (temp.m == 13){ temp.m = 1;temp.y ++; } ans ++; } return temp; }
Date operator++ (Date& D,int){ Date temp = D; D.d ++; if (D.d == month[D.m][isLeap(D.y)]+1){ D.d = 1;D.m ++; } if (D.m == 13){ D.m = 1;D.y ++; } return temp; }
Date operator++ (Date& D){ D.d ++; if (D.d == month[D.m][isLeap(D.y)]+1){ D.d = 1;D.m ++; } if (D.m == 13){ D.m = 1;D.y ++; } return D; } int main(){ Date d1(2019,12,31),d2,d3; d2 = d1++; d1.print(); d2.print(); d3 = ++d1; d1.print(); d3.print(); d1 = d1 + 365; d1.print(); return 0; }
|