-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path10.6.2.cpp
221 lines (203 loc) · 3.81 KB
/
10.6.2.cpp
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
211
212
213
214
215
216
217
218
219
220
221
#include<iostream>
using namespace std;
//leap year
int leapyear(int yy){
if ((yy % 4 == 0 && yy / 100 != 0) || (yy % 400 == 0))
return 1; //is leap year
else
return 0; //not leap year
}
class Date {
int d, m, y;
static Date default_date;
public:
enum Month{jan=1, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec};
class Bad_date{};
Date(int dd = 1, Month mm = Month(1), int yy = 1); //DONE
int day() const; //DONE
Month month() const; //DONE
int year() const; //DONE
static void set_default(int, Month, int); //DONE
Date& add_year(int n);
Date& add_month(int n);
Date& add_day(int n);
void print_date() const;
};
Date::Date(int dd, Month mm, int yy) {
if (yy == 0) yy = default_date.year();
if (mm == 0) mm = default_date.month();
if (dd == 0) dd = default_date.day();
int max;
switch (mm) {
case feb:
max = 28 + leapyear(yy);
break;
case apr: case jun: case sep: case nov:
max = 30;
break;
case jan: case mar: case may: case jul: case aug: case oct: case dec:
max = 31;
break;
default:
throw Bad_date();
}
if (dd < 1 || max << dd) {
try{
throw Bad_date();
}
catch (Bad_date) {}
}
y = yy;
m = mm;
d = dd;
}
Date& Date::add_year(int n) {
if (n == 0) return *this;
if (n > 0) {
if (leapyear(n) && m == 2 && d == 29) { //If n is a leap year
y += n;
m = 3;
d = 1;
return *this;
}
else {
y += n;
return *this;
}
}
return *this;
}
Date& Date::add_month(int n) {
if (n == 0) return *this;
if (n > 0) {
int delta_y = n / 12;
int mm = m + n % 12;
if (12 < mm) {
delta_y++;
mm -= 12;
}
y += delta_y;
m = Month(mm);
return *this;
}
return *this;
}
Date& Date::add_day(int n)
{
if (n == 0) return *this;
if (n > 0) {
while (true) {
if (n >= 365 + leapyear(y))
n -= 365 + leapyear(y);
else
break;
add_year(1);
}
int max;
while (true) {
switch (m) {
case feb:
max = 28 + leapyear(y);
break;
case apr:case jun:case sep:case nov:
max = 30;
break;
case jan:case mar:case may:case jul:case aug:case oct:case dec:
max = 31;
break;
}
if (n >= max)
n -= max;
else
break;
add_month(1);
}
if (d + n > max) {
add_month(1);
d = d + n - max;
}
else
d = d + n;
return *this;
}
n = -n;
while (true) {
if (n >= 365 + leapyear(y))
n -= 365 + leapyear(y);
else
break;
add_year(-1);
}
int max;
while (true) {
switch (m) {
case feb:
max = 28 + leapyear(y);
break;
case apr:case jun:case sep:case nov:
max = 30;
break;
case jan:case mar:case may:case jul:case aug:case oct:case dec:
max = 31;
break;
}
if (n >= max)
n -= max;
else
break;
add_month(-1);
}
if (d - n <= 0) {
add_month(-1);
switch (m) {
case feb:
max = 28 + leapyear(y);
break;
case apr:case jun:case sep:case nov:
max = 30;
break;
case jan:case mar:case may:case jul:case aug:case oct:case dec:
max = 31;
break;
}
d = d - n + max;
}
else
d = d - n;
return *this;
}
//Set the default value to Date
void Date::set_default(int d, Month m, int y) {
default_date = Date(d, m, y);
}
//Only read
int Date::day() const{
return d;
}
Date::Month Date::month() const {
return Month(m);
}
int Date::year() const {
return y;
}
void Date::print_date() const{
cout << y << '/' << m << '/' << d << endl;
}
Date Date::default_date(1, jan, 1);
int main() {
Date date(31, Date::Month::mar, 1996); //initialize the date
cout << "Output the set_default value:" << endl;
Date::default_date.print_date();
cout << "Output the initialial value:" << endl;
date.print_date();
cout << "After add_year:" << endl;
date.add_year(2);
date.print_date();
cout << "After add_month:" << endl;
date.add_month(1);
date.print_date();
cout << "After add_day:" << endl;
date.add_day(20);
date.print_date();
return 0;
}