实现date类

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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
// date.h
#pragma once

#include <iostream>

using std::cin;
using std::cout;
using std::endl;
using std::istream;
using std::ostream;

class Date {
friend ostream& operator<<(ostream& _cout, const Date& d);
friend istream& operator>>(istream& _cin, Date& d);
public:
inline int GetMonthDay(int year, int month) const {
static int monthArray[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
if ((month == 2)
&& ((year % 4 == 0 && year % 100 != 0)
|| (year % 400 == 0))) {
return 29;
}
return monthArray[month];
}

Date(int year = 1900, int month = 1, int day = 1);
int operator-(const Date&d) const;
Date& operator++(); // 前置++
Date operator++(int); // 后置++
Date& operator--(); // 前置--
Date operator--(int); // 后置--
Date operator+(int day) const;
Date operator-(int day) const;
Date& operator+=(int day);
Date& operator-=(int day);
bool operator>(const Date& d) const;
bool operator>=(const Date& d) const;
bool operator<(const Date& d) const;
bool operator<=(const Date& d) const;
bool operator==(const Date& d) const;
bool operator!=(const Date& d) const;
private:
int _year;
int _month;
int _day;
};

// date.cpp
#include "Date.h"

Date::Date(int year, int month, int day) {
if (year >= 1900
&& month > 0 && month < 13
&& day > 0 && day <= GetMonthDay(year, month)) {
_year = year;
_month = month;
_day = day;
} else {
cout << "Input illegal,reset to default value" << endl;
_year = 1900;
_month = 1;
_day = 1;
}
}

ostream& operator<<(ostream& _cout, const Date& d) {
_cout << d._year << "-" << d._month << "-" << d._day << endl;
return _cout;
}

istream& operator>>(istream& _cin, Date& d) {
int year;
int month;
int day;
_cin >> year;
_cin >> month;
_cin >> day;
// new(&d)Date(year, month, day);
d = Date(year, month, day);
return _cin;
}

int Date::operator-(const Date& d) const {
Date maxdate(*this);
Date mindate(d);
int flag = 1;
if (*this < d) {
maxdate = d;
mindate = *this;
flag = -1;
}
int days = 0;
while (mindate != maxdate) {
++mindate;
++days;
}
return days * flag;
}

// ++d 前置++
Date& Date::operator++() {
*this += 1;
return *this;
}

// d++ 后置++
Date Date::operator++(int) {
Date ret(*this);
*this += 1;
return ret;
}

// --d 前置--
Date& Date::operator--() {
*this -= 1;
return *this;
}

// d-- 后置--
Date Date::operator--(int) {
Date tmp(*this);
*this -= 1;
return tmp;
}

Date Date::operator+(int day) const {
Date ret = *this;
ret += day;
return ret;
}

Date Date::operator-(int day) const {
Date ret(*this);
ret -= day;
return ret;
}

Date& Date::operator+=(int day) {
if (day < 0) {
return *this -= -day;
}
_day += day;
while (_day > GetMonthDay(_year, _month)) {
_day -= GetMonthDay(_year, _month);
_month++;
if (_month == 13) {
++_year;
_month = 1;
}
}
return *this;
}

Date& Date::operator-=(int day) {
if (day < 0) {
return *this += -day;
}
_day -= day;
while (_day <= 0) {
--_month;
if (_month == 0) {
_month = 12;
--_year;
}
_day += GetMonthDay(_year, _month);
}
return *this;
}

bool Date::operator>(const Date& d) const {
if (_year > d._year) {
return true;
} else if (_year == d._year) {
if (_month > d._month) {
return true;
} else if (_month == d._month) {
if (_day > d._day) {
return true;
}
}
}
return false;
}

bool Date::operator>=(const Date& d) const {
return *this > d || *this == d;
}

bool Date::operator<(const Date& d) const {
return !(*this >= d);
}

bool Date::operator<=(const Date& d) const {
return !(*this > d);
}

bool Date::operator==(const Date& d) const {
return _year == d._year && _month == d._month && _day == d._day;
}

bool Date::operator!=(const Date& d) const {
return !(*this == d);
}

int main() {
Date date;
cin >> date;
cout << date << endl;
return 0;
}