设计一个日期类 Date,包括 年月日 三个私有数据成员。设计运算符重载,实现:
·两个日期对象之间的“-”,求得两个日期的相差天数。注意两个日期不能相加。
·实现日期对象“+”“-”“+=”“-=”一个整型数,求得运算后的日期。
1 #include <iostream>
2 using namespace std;
3
4 const int Months[2][13] = {{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
5 {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}};
6 const int Years[] = {365, 366};
7
8 inline int isLeapYear(int y)
9 {
10 if ((y % 4 == 0 && y % 100 != 0) || y % 400 == 0)
11 {
12 return 1;
13 }
14 else
15 {
16 return 0;
17 }
18 }
19
20 class Date
21 {
22 private:
23 int year;
24 int month;
25 int day;
26 private:
27 Date addDay(int d) const;
28 Date subDay(int d) const;
29 public:
30 Date(int y = 0, int m = 0, int d = 0) : year(y), month(m), day(d) {}
31 ~Date() {}
32 Date& operator+=(int d)
33 {
34 *this = addDay(d);
35 return *this;
36 }
37 Date& operator-=(int d)
38 {
39 *this = subDay(d);
40 return *this;
41 }
42 friend bool operator<(const Date& d1, const Date& d2);
43 friend int operator-(const Date& d1, const Date& d2);
44 friend Date operator+(const Date& date, int day);
45 friend Date operator-(const Date& date, int day);
46 friend istream& operator>>(istream& in, Date& date);
47 friend ostream& operator<<(ostream& out, const Date& date);
48 };
49
50 Date Date::addDay(int d) const
51 {
52 // 不能拦路判断,因为在是 2 月前还是在后,判断闰年时许判断今年还是明年,所以为了方便做统一处理。
53 Date t = *this;
54 int dif = 0;
55 for (int m = 1; m < t.month; ++m)
56 {
57 dif += Months[isLeapYear(t.year)][m];
58 }
59 dif += (t.day - 1);
60 d += dif;
61 t.month = 1;
62 t.day = 1;
63
64 while (d > Years[isLeapYear(t.year)])
65 {
66 d -= Years[isLeapYear(t.year)];
67 ++t.year;
68 }
69 while (d > Months[isLeapYear(t.year)][t.month])
70 {
71 d -= Months[isLeapYear(t.year)][t.month];
72 ++t.month;
73 }
74 t.day += d;
75 d = 0;
76 return t;
77 }
78
79 Date Date::subDay(int d) const
80 {
81 Date t = *this;
82 // 从 XXXX1231 开始往后减。
83 int dif = 0;
84 for (int m = t.month + 1; m <= 12; ++m)
85 {
86 dif += Months[isLeapYear(t.year)][m];
87 }
88 dif += (Months[isLeapYear(t.year)][t.month] - t.day);
89 d += dif;
90 t.month = 12;
91 t.day = 31;
92
93 while (d >= Years[isLeapYear(t.year)])
94 {
95 d -= Years[isLeapYear(t.year)];
96 --t.year;
97 }
98 while (d >= Months[isLeapYear(t.year)][t.month])
99 {
100 d -= Months[isLeapYear(t.year)][t.month];
101 --t.month;
102 t.day = Months[isLeapYear(t.year)][t.month];
103 }
104 t.day -= d;
105 d = 0;
106 return t;
107 }
108
109 bool operator<(const Date& d1, const Date& d2)
110 {
111 return d1.year < d2.year || d1.month < d2.month || d1.day << d2.day;
112 }
113
114 int operator-(const Date& t1, const Date& t2)
115 {
116 bool f = false;
117 Date d1, d2;
118 if (t1 < t2)
119 {
120 d1 = t1;
121 d2 = t2;
122 }
123 else
124 {
125 d1 = t2;
126 d2 = t1;
127 f = true;
128 }
129 int ret = 0, dif1 = 0, dif2 = 0, dif3 = 0;
130 for (int m = 1; m < d1.month; ++m)
131 {
132 dif1 += Months[isLeapYear(d1.year)][m];
133 }
134 dif1 += d1.day;
135 for (int m = 1; m < d2.month; ++m)
136 {
137 dif2 += Months[isLeapYear(d2.year)][m];
138 }
139 dif2 += d2.day;
140 for (int y = d1.year; y < d2.year; ++y)
141 {
142 dif3 += Years[isLeapYear(y)];
143 }
144 ret = dif2 + dif3 - dif1;
145 return ret;
146 }
147
148 Date operator+(const Date& date, int day)
149 {
150 if (day > 0)
151 {
152 return date.addDay(day);
153 }
154 else if (day < 0)
155 {
156 return date.subDay(-day);
157 }
158 else
159 {
160 return date;
161 }
162 }
163
164 Date operator-(const Date& date, int day)
165 {
166 if (day > 0)
167 {
168 return date.subDay(day);
169 }
170 else if (day < 0)
171 {
172 return date.addDay(-day);
173 }
174 else
175 {
176 return date;
177 }
178 }
179
180 istream& operator>>(istream& in, Date& date)
181 {
182 in >> date.year >> date.month >> date.day;
183 if (!in)
184 {
185 cerr << "Input error!" << endl;
186 exit(1);
187 }
188 return in;
189 }
190
191 ostream& operator<<(ostream& out, const Date& date)
192 {
193 out << date.year << '-' << date.month << '-' << date.day;
194 return out;
195 }
196
197 int main()
198 {
199 // cout << Date(2001, 1, 1).addDay(3) << endl;
200 Date d1, d2;
201 while (cin >> d1 >> d2)
202 {
203 cout << d1 << endl;
204 cout << d2 << endl;
205 cout << d1 - d2 << endl;
206 int n;
207 cin >> n;
208 cout << d1 + n << endl;
209 cout << d1 - n << endl;
210 cout << d2 + n << endl;
211 cout << d2 - n << endl;
212 d1 += n;
213 cout << d1 << endl;
214 d1 -= n;
215 cout << d1 << endl;
216 d2 += n;
217 cout << d2 << endl;
218 d2 -= n;
219 cout << d2 << endl;
220 }
221 return 0;
222 }
posted on 2011-04-22 11:13
unixfy 阅读(272)
评论(0) 编辑 收藏 引用