-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path01-Date-Data-Type.cpp
340 lines (293 loc) · 7.79 KB
/
01-Date-Data-Type.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
// Convert a date in yyyy/mm/dd into "name of the month" dd, yyyy
// Implement at least the following operations: DayOfWeek(date), DateSub(date1,
// date2), DateAdd(date, n) date:2020-09-18
#include <iostream>
#include <string>
#include <vector>
using namespace std;
vector<string> strSplit(string, string);
bool isLeapYear(int year);
bool isValidDate(string date);
string monthConverter(int month);
string dateConverter(string date);
int DaysInYear(string date);
string DateSub(string date1, string date2);
string DayOfWeek(string date);
string DateAdd(string date, int n);
int month_days[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
typedef struct dateinfo {
int year, month, day;
} Date;
int main() {
string dateinput;
string msg = "";
cin >> dateinput;
if (dateinput.find("-") != dateinput.npos) {
vector<string> date;
date = strSplit(dateinput, "-");
if (!isValidDate(date[0]) || !isValidDate(date[1])) {
msg = "It is not a valid date.";
} else {
msg = DateSub(date[0], date[1]) + " days from " + dateConverter(date[0]) +
" to " + dateConverter(date[1]);
}
} else if (dateinput.find("+") != dateinput.npos) {
vector<string> date;
date = strSplit(dateinput, "+");
if (!isValidDate(date[0])) {
msg = "It is not a valid date.";
} else {
msg = DateAdd(date[0], atoi(date[1].c_str()));
}
} else if (dateinput.find("/") != dateinput.npos) {
if (isValidDate(dateinput)) {
msg = dateConverter(dateinput) + " is " + DayOfWeek(dateinput);
} else {
msg = "It is not a valid date.";
}
} else {
msg = "Please enter a valid date.";
}
cout << msg << endl;
system("pause");
return 0;
}
//split string by a character
vector<string> strSplit(string s, string sep) {
vector<string> str_split;
int pos_current = 0;
int pos_next = 0;
while (pos_next < s.length()) {
pos_next = s.find_first_of(sep, pos_current);
if (pos_next != pos_current) {
string strtmp = s.substr(pos_current, pos_next - pos_current);
if (!strtmp.empty()) {
str_split.push_back(strtmp);
}
}
pos_current = pos_next + 1;
}
return str_split;
}
//check if the year is leap year
bool isLeapYear(int year) {
if ((year % 4 == 0) && (year % 100 != 0))
return true;
else if (year % 400 == 0)
return true;
else
return false;
}
//check if the type of input data is valid
bool isValidDate(string date) {
vector<string> vstrDate = strSplit(date, "/");
int intYear = 0, intMonth = 0, intDay = 0;
intYear = atoi(vstrDate[0].c_str());
intMonth = atoi(vstrDate[1].c_str());
intDay = atoi(vstrDate[2].c_str());
if (vstrDate.size() != 3)
return false;
if ((intMonth < 1) || (intMonth > 12))
return false;
if (isLeapYear(intYear))
month_days[1] = 29;
else
month_days[1] = 28;
if (intDay > month_days[intMonth - 1])
return false;
return true;
}
//convert month(a number) into its name
string monthConverter(int month) {
string strMonth;
switch (month) {
case 1:
strMonth = "January";
break;
case 2:
strMonth = "Febuary";
break;
case 3:
strMonth = "March";
break;
case 4:
strMonth = "April";
break;
case 5:
strMonth = "May";
break;
case 6:
strMonth = "June";
break;
case 7:
strMonth = "July";
break;
case 8:
strMonth = "August";
break;
case 9:
strMonth = "September";
break;
case 10:
strMonth = "October";
break;
case 11:
strMonth = "November";
break;
case 12:
strMonth = "December";
break;
default:
strMonth = "Not a valid month.";
break;
}
return strMonth;
}
//convert input into a specified format
string dateConverter(string date) {
string convDate = "";
int intMonth = 0;
if (!isValidDate(date)) {
convDate = "It is not a valid date.";
} else {
intMonth = atoi(strSplit(date, "/")[1].c_str());
convDate = monthConverter(intMonth) + " " + strSplit(date, "/")[2] + ", " +
strSplit(date, "/")[0];
}
return convDate;
}
//count how many days pass
int DaysInYear(string date) {
vector<string> vstrDate = strSplit(date, "/");
int intYear = 0, intMonth = 0, intDay = 0;
intYear = atoi(vstrDate[0].c_str());
intMonth = atoi(vstrDate[1].c_str());
intDay = atoi(vstrDate[2].c_str());
if (isLeapYear(intYear))
month_days[1] = 29;
else
month_days[1] = 28;
for (int i = 0; i < intMonth - 1; i++) {
intDay += month_days[i];
}
return intDay;
}
//convert the day of week into its name
string DayOfWeek(string date) {
int subDate = atoi(DateSub("1753/01/01", date).c_str());
string strDayofWeek = "";
switch (subDate % 7) {
case 6:
strDayofWeek = "Sunday";
break;
case 0:
strDayofWeek = "Monday";
break;
case 1:
strDayofWeek = "Tuesday";
break;
case 2:
strDayofWeek = "Wednesday";
break;
case 3:
strDayofWeek = "Thursday";
break;
case 4:
strDayofWeek = "Friday";
break;
case 5:
strDayofWeek = "Saturday";
break;
default:
break;
}
return strDayofWeek;
}
//return numbers of days from date1 to date2
string DateSub(string date1, string date2) {
string msg = "";
vector<string> vstrDate1, vstrDate2;
Date dtDate1, dtDate2;
vstrDate1 = strSplit(date1, "/");
vstrDate2 = strSplit(date2, "/");
dtDate1.year = atoi(vstrDate1[0].c_str());
dtDate1.month = atoi(vstrDate1[1].c_str());
dtDate1.day = atoi(vstrDate1[2].c_str());
dtDate2.year = atoi(vstrDate2[0].c_str());
dtDate2.month = atoi(vstrDate2[1].c_str());
dtDate2.day = atoi(vstrDate2[2].c_str());
int days1, days2;
days1 = DaysInYear(date1);
days2 = DaysInYear(date2);
if (!isValidDate(date1) || !isValidDate(date2)) {
msg = "It is not a valid date.";
} else {
if (dtDate1.year == dtDate2.year && dtDate1.month == dtDate2.month) {
if (dtDate1.day >= dtDate2.day) {
msg = to_string(dtDate1.day - dtDate2.day);
} else {
msg = to_string(dtDate2.day - dtDate1.day);
}
} else if (dtDate1.year == dtDate2.year) {
msg = to_string(days1 > days2 ? days1 - days2 : days2 - days1);
} else {
if (dtDate1.year > dtDate2.year) {
swap(dtDate1.year, dtDate2.year);
swap(dtDate1.month, dtDate2.month);
swap(dtDate1.day, dtDate2.day);
swap(days1, days2);
}
if (isLeapYear(dtDate1.year))
days1 = 366 - days1;
else
days1 = 365 - days1;
int days3 = 0;
for (int i = dtDate1.year + 1; i < dtDate2.year; i++) {
if (isLeapYear(i))
days3 += 366;
else
days3 += 365;
}
msg = to_string(days1 + days2 + days3);
}
}
return msg;
}
//return the day which is n days after date
string DateAdd(string date, int n) {
string msg = "", newdate = "";
vector<string> vstrDate1;
Date dtDate1;
int cntDays = 0;
if (!isValidDate(date) || n < 0) {
msg = "It is not a valid date.";
} else {
vstrDate1 = strSplit(date, "/");
dtDate1.year = atoi(vstrDate1[0].c_str());
dtDate1.month = atoi(vstrDate1[1].c_str());
dtDate1.day = atoi(vstrDate1[2].c_str());
cntDays = dtDate1.day + n;
if (isLeapYear(dtDate1.year))
month_days[1] = 29;
else
month_days[1] = 28;
while (month_days[dtDate1.month - 1] < cntDays) {
cntDays -= month_days[dtDate1.month - 1];
dtDate1.month += 1;
if (dtDate1.month > 12) {
dtDate1.year += 1;
dtDate1.month = 1;
if (isLeapYear(dtDate1.year))
month_days[1] = 29;
else
month_days[1] = 28;
}
}
dtDate1.day = cntDays;
newdate = to_string(dtDate1.year) + "/" + to_string(dtDate1.month) + "/" +
to_string(dtDate1.day);
}
msg = to_string(n) + " days after " + dateConverter(date) + " is " +
dateConverter(newdate);
return msg;
}