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 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
| #include <iostream> using namespace std; bool isLeap(int x) //判断是否是闰年 { if (x % 400 == 0) return true; if (x % 100 == 0) return false; if (x % 4 == 0) return true; return false; } long days(int y, int m, int d) { int num[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; //平年每月的天数 y--; long ret = 365 * y + y / 4 - y / 100 + y / 400 + d; for (int i = 1; i < m; i++) { ret += num[i]; if (i == 2 && isLeap(y + 1)) ret++; //符合闰年且月份大于2月 } return ret; } int Leap_Year(int year) { if (year % 400 == 0 || (year % 100 != 0 && year % 4 == 0)) return 366; else return 365; } class DateType { private: int y0; int m0; int d0; public: friend bool isLeap(int x); friend long days(int y, int m, int d); void Set_Day(int day) { d0 += day; } int Get_Day() { return d0; } void Set_Month(int month) { m0 += month; } int Get_Month() { return m0; } void Set_Year(int year) { y0 += year; } int Get_Year() { return y0; } DateType() { y0 = 1; m0 = 1; d0 = 1; } DateType(int y = 1, int m = 1, int d = 1) { y0 = y; m0 = m; d0 = d; } void PrintDate() { cout << y0 << "-" << m0 << "-" << d0; } }; class TimeType { private: int hr0; int mi0; int se0; public: friend bool isLeap(int x); friend long days(int y, int m, int d); void Set_Second(int second) { se0 += second; } int Get_Second() { return se0; } void Set_Minute(int minute) { mi0 += minute; } int Get_Minute() { return mi0; } void Set_Hour(int hour) { hr0 += hour; } int Get_Hour() { return hr0; } TimeType(int hr = 0, int mi = 0, int se = 0) { hr0 = hr; mi0 = mi; se0 = se; } TimeType() { hr0 = 0; mi0 = 0; se0 = 0; } void Get_Time() { cout << hr0 << ":" << mi0 << ":" << se0; } }; class DateTimeType :public DateType, public TimeType { private: TimeType time; DateType date; public: friend bool isLeap(int x); friend long days(int y, int m, int d); DateTimeType(int y=1, int m=1, int d=1, int hr=0, int mi=0, int se=0) :DateType(y, m, d),TimeType(hr, mi, se), date(y, m, d), time(hr, mi, se) { } void PrintDateTime() { date.PrintDate(); cout << " "; time.Get_Time(); cout << endl; } DateType& GetDate() { return date; } TimeType& GetTime() { return time; } void IncrementSecond(int s) { int MONTH[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; time.Set_Second(s); if (time.Get_Second() < 60) ; else { for (; time.Get_Second() >= 60;) { time.Set_Second(-60); time.Set_Minute(1); } for (; time.Get_Minute() >= 60;) { time.Set_Minute(-60); time.Set_Hour(1); } for (; time.Get_Hour() >= 24;) { time.Set_Hour(-24); date.Set_Day(1); } long Total_Day = days(date.Get_Year(), date.Get_Month(), date.Get_Day()); int temp, Cur_Year, Cur_Month, Cur_Day; for (Cur_Year = 0; Total_Day >= Leap_Year(Cur_Year); Cur_Year++) { Total_Day-=Leap_Year(Cur_Year); } if (Leap_Year(Cur_Year) == 366) MONTH[2] = 29; for (temp = 1,Cur_Month=1; Total_Day >= MONTH[temp]; temp++) { Total_Day -= MONTH[temp]; Cur_Month++; } Cur_Day = Total_Day; date.Set_Day(- date.Get_Day() + Cur_Day); date.Set_Month(- date.Get_Month() + Cur_Month); date.Set_Year(- date.Get_Year() + Cur_Year); } } };
int main() { DateTimeType dttm1(1999, 12, 31, 23, 59, 59), dttm2; (dttm1.GetDate()).PrintDate(); //调用对象成员所属类的公有成员函数 cout << endl; dttm1.PrintDateTime(); //调用本派生类的成员函数 PrintDateTime dttm2.PrintDateTime(); dttm1.IncrementSecond(30); //调用本派生类成员函数 dttm1.PrintDateTime(); return 0; }
|