功能:实现日期的简单操作
基本的成员函数:构造函数,拷贝构造函数,析构函数,赋值运算符重载,操作符重载(两个日期间比较大小)
日期类功能函数: 1:计算一个日期加上多少天数后的日期 2:把该日期改为加上指定数目的天数后的日期
3:一个日期减上多少天数后的日期 4:把该日期改为减去指定数目的天数后的日期 5:该日期加1(前置++)(后置++)
6:该日期减1(前置--)(后置--) 7:计算某日期到未来某日期间隔的天数 By :Lynn-Zhang
1 //--------------------------------------------------------------------------
2 /*
3 **功能:实现日期的简单操作
4 **
5 **
6 **基本的成员函数:
7 ** 构造函数,拷贝构造函数,析构函数,赋值运算符重载,操作符重载(两个日期间比较大小)
8 **
9 **日期类功能函数:
10 ** 1:计算一个日期加上多少天数后的日期
11 ** 2:把该日期改为加上指定数目的天数后的日期
12 ** 3:一个日期减上多少天数后的日期
13 ** 4:把该日期改为减去指定数目的天数后的日期
14 ** 5:该日期加1(前置++)(后置++)
15 ** 6:该日期减1(前置--)(后置--)
16 ** 7:计算某日期到未来某日期间隔的天数
17 **
18 **
19 ** By :Lynn-Zhang
20 **
21 */
22 //---------------------------------------------------------------------------
23
24 #define _CRT_SECURE_NO_WARNINGS 1
25
26 #include<assert.h>
27 #include <iostream>
28 using namespace std;
29
30 //在实现日期之间的运算之前,要先进行日期是否非法的检查
31 //实现日期间大小的比较
32 //计算两个日期间相差的天数
33 //计算一个日期加或减上day天后的日期
34
35 class Date
36 {
37 public:
38 Date(int year,int month,int day) //构造函数
39 {
40 if (year >= 2000 && month > 0 && month<13 && day>0 < GetMonthDay(year, month)) //判断日期是否非法
41 {
42 _year = year;
43 _month = month;
44 _day = day;
45 }
46 else
47 {
48 cout << "日期非法" << endl;
49 assert(false);
50 }
51 }
52
53 Date(const Date& d) //拷贝构造
54 {
55 _year = d._year;
56 _month = d._month;
57 _day = d._day;
58 }
59
60 void Display() // 打印日期
61 {
62 cout << _year << "-" << _month << "-" << _day << endl;
63 }
64
65 ~Date() //析构函数
66 {
67 //cout << "~Date()" << endl;
68 }
69
70 //运算符重载(两个日期间比较大小)
71 bool operator==(const Date &d) //判断两个日期是否相等
72 {
73 return (_year == d._year) && (_month == d._month) && (_day == d._day);
74 }
75
76 bool operator>(const Date &d) //判断本日期是否大于日期d
77 {
78 if (_year > d._year)
79 {
80 return true;
81 }
82 else if (_year == d._year&&_month > d._month)
83 {
84 return true;
85 }
86 else if (_year == d._year&&_month == d._month&&_day > d._day)
87 {
88 return true;
89 }
90 else
91 return false;
92 }
93
94 bool operator>=(const Date &d) //判断本日期是否大于或等于日期d
95 {
96 return (*this > d) || (*this == d);
97 }
98
99 bool operator<(const Date &d) //判断本日期是否小于日期d
100 {
101 return !(*this >= d);
102 }
103 bool operator<=(const Date &d) //判断本日期是否小于等于日期d
104 {
105 return !(*this > d);
106 }
107
108 //赋值运算符重载
109 Date operator=(const Date &d)
110 {
111 if (this != &d)
112 {
113 this->_year = d._year;
114 this->_month = d._month;
115 this->_day = d._day;
116 }
117 return *this;
118 }
119
120 private:
121 bool IsLeapYear(int year) //判断是否闰年
122 {
123 if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0))
124 return true;
125 else
126 return false;
127 }
128
129 int GetMonthDay(int year, int month) //根据已知年月获取该月的天数
130 {
131 int monthArray[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
132
133 int day = monthArray[month];
134
135 if (month == 2 && IsLeapYear(year))
136 {
137 day += 1;
138 }
139
140 return day;
141 }
142
143 public:
144 Date operator+(int day) //给一个日期加day天
145 {
146 if (day < 0)
147 {
148 return operator-(-day);
149 }
150 Date tmp = *this;
151 int sumDays = tmp._day + day;
152 while (sumDays > GetMonthDay(tmp._year, tmp._month))
153 {
154 sumDays -= GetMonthDay(tmp._year, tmp._month);
155 tmp._month++;
156 if (tmp._month > 12)
157 {
158 tmp._year++;
159 tmp._month %= 12;
160 }
161 else
162 {
163 tmp._day = sumDays;
164 }
165 }
166 return tmp;
167 }
168
169 Date & operator+=(int day) //加上相应的天数后还要进行赋值
170 {
171 *this = operator+(day);
172 return *this;
173 }
174
175 Date operator-(int day) //给一个日期减去day天
176 {
177 if (day < 0)
178 {
179 return operator+(-day);
180 }
181 Date tmp = *this;
182 while (day >= tmp._day)
183 {
184 day -= tmp._day;
185 if (tmp._month == 1)
186 {
187 tmp._year--;
188 tmp._month = 12;
189 }
190 else
191 {
192 tmp._month--;
193 }
194 tmp._day = GetMonthDay(tmp._year, tmp._month);
195 }
196 tmp._day -= day;
197 return tmp;
198 }
199
200 Date & operator-=(int day) //减去相应的天数后赋值
201 {
202 *this = operator-(day);
203 return *this;
204 }
205 Date & operator++() //日期加1(前置++)
206 {
207 ++_day;
208 if (_day > GetMonthDay(_year, _month))
209 {
210 _day -= GetMonthDay(_year, _month);
211 ++_month;
212 if (_month > 12)
213 {
214 ++_year;
215 _month = 1;
216 }
217 }
218 return *this;
219 }
220
221 Date & operator++(int) //后置++
222 {
223 Date tmp = *this;
224 *this = operator++();
225 return tmp;
226 }
227
228 Date & operator--() // 日期减1 (前置--)
229 {
230 if (_day > 1)
231 {
232 --_day;
233 }
234 else
235 {
236 if (_month == 1)
237 {
238 --_year;
239 _month = 12;
240 _day = GetMonthDay(_year, _month);
241 }
242 else
243 {
244 --_month;
245 _day = GetMonthDay(_year, _month);
246 }
247 }
248 return *this;
249 }
250
251 Date & operator--(int) //后置--
252 {
253 Date tmp = *this;
254 *this = operator--();
255 return tmp;
256 }
257
258 //计算两个日期相差的天数
259 int operator-(Date & d)
260 {
261 if (_year <d. _year)
262 {
263 Date tmp =* this;
264 *this = d;
265 d = tmp;
266 }
267 else if (_year == d._year&&_month < d._month)
268 {
269 Date tmp = *this;
270 *this = d;
271 d = tmp;
272 }
273 else if (_year ==d. _year&&_month == d._month&&_day < d._day)
274 {
275 Date tmp = *this;
276 *this = d;
277 d = tmp;
278 }
279 Date tmp1(*this);
280 Date tmp2(d);
281 int ret = 0;
282 while (!(tmp2 == tmp1))
283 {
284 tmp2.operator++();
285 ret++;
286 }
287 return ret;
288 }
289
290 private:
291 int _year;
292 int _month;
293 int _day;
294 };
295
296 void Test1() //测试用例
297 {
298 /*Date d1(2016, 1, 15);
299 d1.Display();
300 Date d2 = d1;
301 d2.Display();
302 Date d3;
303 d3 = d1;
304 d2.Display();*/
305
306 Date d1(2016, 1, 15);
307 Date d2(2016, 1, 16);
308 d1.Display();
309 d2.Display();
310 // == 0
311 //bool ret = d1 == d2;
312 //cout << ret << endl;
313 // > 1
314 //bool ret = d1 >d2;
315 //cout << ret << endl;
316 // < 0
317 //bool ret = d1 < d2;
318 //cout << ret << endl;
319 // >= 1
320 //bool ret = d1 >= d2;
321 //cout << ret << endl;
322 // <= 0
323 //bool ret = d1 <= d2;
324 //cout << ret << endl;
325 // =
326 //d1 = d2;
327 //d1.Display();
328 //d2.Display();
329 }
330 void Test2()
331 {
332 Date d1(2016, 2,1);
333 d1.Display();
334 //Date ret = d1+1;
335 //ret.Display();
336 //Date ret=d1+70;
337 //ret.Display();
338 //Date ret=d1-25;
339 //ret.Display();
340 ////Date ret = d1 += 70;
341 ////ret.Display();
342 ////d1.Display();
343 //Date ret = d1 -= 70;
344 //ret.Display();
345 //d1.Display();
346 //d1++;
347 //d1.Display();
348 //++d1;
349 //d1.Display();
350 //--d1;
351 //d1.Display();
352 //d1--;
353 //d1.Display();
354 Date d2(2016, 2, 29);
355 d2.Display();
356 int ret = d1 - d2;
357 cout << ret << endl;
358 }
359 int main()
360 {
361 //Test1();
362 Test2();
363 system("pause");
364 return 0;
365 }