-
Notifications
You must be signed in to change notification settings - Fork 0
/
Calendar.cpp
500 lines (450 loc) · 13.5 KB
/
Calendar.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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
void get_input(int& month, int& day, int& year);
// post-condition: get the input of a date. If the date is NOT valid, will prompt the user to reenter a valid date
void get_input(int& month, int& year);
// post-condition: get the input of a month. If the month is NOT valid, will prompt the user to reenter a valid month
void get_input(int& year);
// post-condition: get the input of a year. If the date is NOT valid, will prompt the user to reenter a valid year.
// the year must be later than 1582 in order to be valid.
bool isValid(int month, int day, int year);
// post-condition: return true if the specified date is valid, false otherwise
bool isValid(int month, int year);
// post-condition: return if the specified month is valid, false othewise
bool isValid(int year);
// post-condition: return true if the given year is valid. false otherwise.
// the given year is valid if it is later than year 1582.
void print_calendar(int month, int day, int year);
// pre-condition: the date is a valid date
// post-condition: print out the day of week for specified date. For example, print_calendar(10, 15, 2014) will print out
// October 15, 2014 is Wednesday
void print_calendar(int month, int year);
// pre-condition: the month is a valid month, and the year is a valid year
// post-condition: print out the calendar of specified month
void print_calendar(int year);
// pre-condition: the year is a valid year
// post-condition: print out the calendar of specified year
string monthName(int month);
// pre-condition: the month value is 1, 2, .., 12
// post-condition: return a string that represents the name of the month. If month is 1, the name is "January" and so on
int daysInMonth(int month, int year);
// pre-condition: the month and year are valid
// post-condition: return the number of days in the given month in given year
void menu();
// post-condition: the menu is displayed for choose
bool isLeapYear(int year);
// pre-condition: the year is valid
// post-condition: return true if the year is leap year; false otherwise
int getCenturyValue(int year);
// pre-condition: the year is valid
// post-condition: return the century value of the given year.
int getYearValue(int year);
// pre-condition: the year is valide
// post-condition: return the year value of the given year.
int getMonthValue(int month, int year);
// pre-condition: the month and year are valide
// post-condition: return the month value of the given month in given year.
int dayOfWeek(int month, int day, int year);
// pre-condition: the date is valid
// post-condition: return 0 if the date is Sunday, 1 if the date is Monday and so on
string dayOfWeek(int day);
// pre-condition: day has integer value 0, 1, 2, 3, 4, 5, or 6
// post-condition: the name of the day of week is returned as a string.
// If day has value 0, then SUNDAY is returned; 1, then MONDAY is returned
// and so on
//************************************************************************
// You may have implemented all functions above for project 5
// Implementation all functions above are given in this file
// They are the solution for Project 5 and more.
// You must implement the functions below. Also, you must change the menu
// option accordingly and add one more case in main function
// ***********************************************************************
bool print_calendar(int year, string fileName);
// pre-condition: year is a leagal year, and fileName is the name of output file
// post-condition: if the calendar of the given year is successfully output to the file
// with name fileName, return true; false otherwise
void print_calendar(int month, int year, ofstream& output);
// pre-condition: month is a legal month in a given legal year and output is an ofstream
// that connected to a successfully opened file
// post-condition: the calendar of the given month of given year is output to a file
// through the output.
int main()
{
char choice;
int day, month, year;
do
{
system("CLS");
menu();// display menu by calling menu function
cout << "\n\tPlease enter your choice: ";// ask user to enter his/her choice on menu
cin >> choice;// get user's choice
switch (choice)
{
case '1': // will query a date.
get_input(month, day, year);
print_calendar(month, day, year);// print out the day of week of specified date
break;
case '2': // print out the calendar of a given month.
get_input(month, year);
print_calendar(month, year);//print out the calendar of specified month
break;
case '3': // print out the calendar of a given year.
get_input(year);
print_calendar(year);// print out the calendar of specified year
break;
case '4': // print out the calendar of specified year to yourname.txt file
get_input(year);
print_calendar(year, "ljimenez.txt");
cout << "\n\tCalendar of year 2016 has be print to file ljimenez.txt";// you implement this ******************
break;
case '5': // quit the program.
cout << "\n\tThank you for using FHSU's calendar program!\n\n"; // you may replace your name here
break;
default: // other choices that are not the options on menu.
cout << "\n\tError, no such choice. Please choose from Menu.\n";//print out error message
break;
}
cout << "\n\n\t";
system("pause");
} while (choice != '5');
return 0;
}
// you implement all function stubs here
void menu()
{
cout << "\n\t***************************************************";
cout << "\n\t* Menu *";
cout << "\n\t* 1. Query a date *";
cout << "\n\t* 2. Print calendar of a given month *";
cout << "\n\t* 3. Print calendar of a given year *";
cout << "\n\t* 4. Print calendar of a given year to file *";
cout << "\n\t* 5. Quit *";
cout << "\n\t***************************************************";
}
void get_input(int& month, int& day, int& year)
{
cout << "\n\tEnter the date in form of mm dd yyyy: ";
cin >> month >> day >> year;
while (!isValid(month, day, year)) // as long as the date is NOT valid, keep asking to reenter the date
{
cout << "\n\tThe date you entered is NOT valid. Please reenter a valid date: ";
cin >> month >> day >> year;
}
}
void get_input(int& month, int& year)
{
cout << "\n\tEnter the month and year in form of mm yyyy: ";
cin >> month >> year;
while (!isValid(month, year)) // as long as the month is NOT valid, keep asking to reenter the month
{
cout << "\n\tThe month you entered is NOT valid. Please reenter a valid month: ";
cin >> month >> year;
}
}
void get_input(int& year)
{
cout << "\n\tEnter the year as yyyy: ";
cin >> year;
while (!isValid(year)) // as long as the year is NOT valid, keep asking to reenter the year
{
cout << "\n\tThe year you entered is NOT valid. Please reenter a valid year: ";
cin >> year;
}
}
bool isValid(int month, int day, int year)
{
// Questions 1: Please answer (YES/NO) to indicate if you understand this implementation
// by adding one line of comments at beginning of the file. This worth 1 point
bool result = true; // temporary assume that the result is true
switch (month) // update result based on day
{
case 1: case 3: case 5: case 7: case 8: case 10: case 12:
result = day >= 1 && day <= 31;
break;
case 4: case 6: case 9: case 11:
result = day >= 1 && day <= 30;
break;
case 2:
if (isLeapYear(year))
result = day >= 1 && day <= 29;
else
result = day >= 1 && day <= 28;
break;
}
return result && isValid(month, year); // day is valid and (month and year are valid)
}
bool isValid(int month, int year)
{
return month >= 1 && month <= 12 && year > 1582; // month is between 1 and 12 and year is later than 1582
}
bool isValid(int year)
{
return year > 1582; // year is later than 1582
}
bool isLeapYear(int year)
{
return ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0)); // book's instruction
}
int getCenturyValue(int year)
{
return 2 * (3 - (year / 100) % 4); // book's instruction
}
int getYearValue(int year)
{
return year % 100 + year % 100 / 4; // book's instruction
}
int getMonthValue(int month, int year) // book's instruction
{
int result;
switch (month)
{
case 1:
if (isLeapYear(year))
result = 6;
else result = 0;
break;
case 2:
if (isLeapYear(year))
result = 2;
else
result = 3;
break;
case 3: case 11:
result = 3;
break;
case 4: case 7:
result = 6;
break;
case 5:
result = 1;
break;
case 6:
result = 4;
break;
case 8:
result = 2;
break;
case 9:case 12:
result = 5;
break;
default: // case 10
result = 0;
break;
}
return result;
}
int dayOfWeek(int month, int day, int year) // book's instruction
{
if (!isValid(month, day, year)) return -1;
return (getCenturyValue(year) + getMonthValue(month, year) + getYearValue(year) + day) % 7;
}
void print_calendar(int month, int day, int year)
{
// Questions 2: Add one line of comments at the beginning of the file to indicate
// if you understand what the following if statement does? (YES/NO)
// This worth 1 point
if (!isValid(month, day, year)) { // if the date is NOT valid, print out error message and return
cout << "\n\tThe date is NOT valid!";
return;
}
cout << "\n\t" << monthName(month) << " " << day << " , " << year << " is " << dayOfWeek(dayOfWeek(month, day, year));
}
void print_calendar(int month, int year)
{
if (!isValid(month, year)) { // if the month is NOT valid, print out error message and return
cout << "\n\tThe month is NOT valid!";
return;
}
// print out the month and year
cout << "\n\t " << monthName(month) << ", " << year << endl;
// print out day names
cout << "\n\t SUN MON TUE WED THU FRI SAT\n\t";
int spaceCount = dayOfWeek(month, 1, year);
// print out spaces
for (int i = 0; i < spaceCount; i++)
cout << setw(4) << "";
// calculate the days in the month
int days = daysInMonth(month, year);
// print out all days
for (int j = 1; j <= days; j++)
{
cout << setw(4) << j;
if ((spaceCount + j) % 7 == 0) // go to new line for a new week
cout << "\n\t";
}
}
void print_calendar(int year)
{
if (!isValid(year)) { // if the year is NOT valid, print out error message and return
cout << "\n\tThe year is NOT valid!";
return;
}
for (int i = 1; i <= 12; i++) // print out 12 month calendar
{
print_calendar(i, year); // print out ith month
cout << endl;
}
}
string monthName(int month)
{
string result;
switch (month)
{
case 1:
result = "January";
break;
case 2:
result = "February";
break;
case 3:
result = "March";
break;
case 4:
result = "April";
break;
case 5:
result = "May";
break;
case 6:
result = "June";
break;
case 7:
result = "July";
break;
case 8:
result = "August";
break;
case 9:
result = "September";
break;
case 10:
result = "October";
break;
case 11:
result = "November";
break;
case 12:
result = "December";
break;
default:
result = "Not valid month";
break;
}
return result;
}
string dayOfWeek(int day)
{
string result;
switch (day)
{
case 0:
result = "Sunday";
break;
case 1:
result = "Monday";
break;
case 2:
result = "Tuesday";
break;
case 3:
result = "Wednesday";
break;
case 4:
result = "Thursday";
break;
case 5:
result = "Friday";
break;
case 6:
result = "Saturday";
break;
default:
result = "Invalid date";
break;
}
return result;
}
int daysInMonth(int month, int year)
{
// Question 3: Add one line of comment at the beginning to tell me
// if this implementation is the same as yours in Proj five
// If not, do you understand this? This worth 1 point
int result;
switch (month)
{
case 1: case 3: case 5: case 7: case 8: case 10: case 12:
result = 31;
break;
case 4: case 6: case 9: case 11:
result = 30;
break;
case 2:
if (isLeapYear(year))
result = 29;
else result = 28;
break;
default:
result = -1;
break;
}
return result;
}
bool print_calendar(int year, string fileName)
{
if (!isValid(year))
{
cout << "\nInvalid year" << endl;
return false;
}// if year is not valid, print out error message and return false
ofstream write;
write.open(fileName);
// declare a local ofstream variable and open file to write
if (write.fail())
{
cout << "\nFile open failed" << endl;
return false;
}
// if file open failed, print out error message and return false
for (int i = 1; i <= 12; i++)
{
print_calendar(i, year, write);
write << endl;
}
// use for loop to print out 12 months to the file
write.close();
// close file
return true;
// return true
}
void print_calendar(int month, int year, ofstream& output)
{
if (!isValid(month, year))
{
cout << "\n\tMonth is NOT valid";
return;
}
// if the month is NOT valid, print out error message and return
output << "\n\t" << monthName(month) << "," << year << endl;
// print out the month and year
output << "\n\tSUN MON TUE WED THU FRI SAT";
// print out day names
int spaceCount = dayOfWeek(month, 1, year);
// calculate spaces before the first day of the month
for (int i = 0; i < spaceCount; i++)
output << setw(4) << "";
// print out spaces
int days = daysInMonth(month, year);
// calculate the days in the month
for (int j = 1; j <= days; j++)
{
output << setw(4) << j;
if ((spaceCount + j) % 7 == 0)
output << "\n\t";
}
// print out all days, using a for loop
// two more new lines
// Note this function is very similar to the one print to screen
}