Question: Modify the code, using Date class(in the end) to implement a C++ application Calendar System , which has two functions : 1. print the
Modify the code, using Date class(in the end) to implement a C++ application " Calendar System ", which has two functions :
1. print the calendar for the given year or a specific month of a given year online with the option to write to a file
2. provided a list of major holidays and the start/end date of the Day Light Saving Time of a given year
#include #include #include #include "Date.h" #include
using namespace std;
void app1(); void app2(); void print_calendar(int year); void print_calendar(int year, ostream &out);
// an array of function pointers Date(*fp[18])(int) = { NewYear, MLKDay, Valentine, PresidentDay, StPatrick, AprilFool, MayDay, MotherDay, MemorialDay, FatherDay, IndependenceDay, Labor, Columbus, Halloween, ElectionDay, VeteransDay, thanksgiving, xmas };
char *major[] = { "New Year Day", "MLK Birthday", "Valentine's Day", "President Day", "St.Patrick's Day", "April Fools", "May Day", "Mother's Day", "Memorial Day", "Father's Day", "Independence Day", "Labor Day", "Columbus Day", "Halloween", "Election Day", "Veterans Day", "Thanksgiving Day", "Christmas" }; char * dayofweek[] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };
void menu() { cout << " \t\tType 1 for Calendar "; cout << " \t\tType 2 for Holidays"; cout << " \t\tType 0 to quit."; cout << " \t\tPlease enter your choice :";;
} int main() {
int choice=0; cout << endl << endl << "\tWelcome to CSC309/404Calendar System: "; while (true) { menu(); cin >> choice;
switch (choice) { case 1: app1(); break; case 2: app2(); break; case 3: break; default: cout << " \t\tInvalid choice "; break; } if (choice == 3) break;
}
cout << " \tThank you for using U.S. Major Holidays Calendar System! "; cout << " \t\tBye! "; return 0; }
/////////////////////////////// Date NewYear(int yr) { Date tmp(1, 1, yr); return tmp; } // MLK day third monday of January Date MLKDay(int yr) { int d; Date tmp; d = Date::get_day_code(yr); switch (d) { case 0: tmp = Date(1, 16, yr); break; case 1: tmp = Date(1, 15, yr); break; case 2: tmp = Date(1, 21, yr); break; case 3: tmp = Date(1, 20, yr); break; case 4: tmp = Date(1, 19, yr); break; case 5: tmp = Date(1, 18, yr); break; case 6: tmp = Date(1, 17, yr); break; } return tmp; }
Date Valentine(int yr) { return Date(2, 14, yr); }
Date PresidentDay(int yr) { // 3rd monday of Febraury int d; Date tmp; d = Date::get_day_code(yr, 2, 1); switch (d) { case 0: tmp = Date(2, 16, yr); break; case 1: tmp = Date(2, 15, yr); break; case 2: tmp = Date(2, 21, yr); break; case 3: tmp = Date(2, 20, yr); break; case 4: tmp = Date(2, 19, yr); break; case 5: tmp = Date(2, 18, yr); break; case 6: tmp = Date(2, 17, yr); break; } return tmp; } Date StPatrick(int yr) { //mar 17
Date temp(3, 17, yr); return temp; }
Date daylightSaving(int yr, Date &end) { int d, m; Date tmp1, tmp2; d = Date::get_day_code(yr, 3, 1); switch (d) { case 0: tmp1 = Date(3, 8, yr); break; case 1: tmp1 = Date(3, 14, yr); break; case 2: tmp1 = Date(3, 13, yr); break; case 3: tmp1 = Date(3, 12, yr); break; case 4: tmp1 = Date(3, 11, yr); break; case 5: tmp1 = Date(3, 10, yr); break; case 6: tmp1 = Date(3, 9, yr); break; } return tmp1; m = Date::get_day_code(yr, 11, 1); switch (m) { case 0: tmp2 = Date(11, 1, yr); break; case 1: tmp2 = Date(11, 7, yr); break; case 2: tmp2 = Date(11, 6, yr); break; case 3: tmp2 = Date(11, 5, yr); break; case 4: tmp2 = Date(11, 4, yr); break; case 5: tmp2 = Date(11, 3, yr); break; case 6: tmp2 = Date(11, 2, yr); break; } end = tmp2; }
Date AprilFool(int yr) { return Date(4, 1, yr); }
Date MayDay(int yr) { // May ist Date tmp(5, 1, yr); return tmp; } Date MotherDay(int yr) { // 2nd sunday in may int d; Date tmp; d = Date::get_day_code(yr, 5, 1); switch (d) { case 0: tmp = Date(5, 8, yr); break; case 1: tmp = Date(5, 14, yr); break; case 2: tmp = Date(5, 13, yr); break; case 3: tmp = Date(5, 12, yr); break; case 4: tmp = Date(5, 11, yr); break; case 5: tmp = Date(5, 10, yr); break; case 6: tmp = Date(5, 9, yr); break; } return tmp;
} Date MemorialDay(int yr) {//last Monday in May int d; Date tmp; d = Date::get_day_code(yr, 5, 1); switch (d) { case 0: tmp = Date(5, 30, yr); break; case 1: tmp = Date(5, 29, yr); break; case 2: tmp = Date(5, 28, yr); break; case 3: tmp = Date(5, 27, yr); break; case 4: tmp = Date(5, 26, yr); break; case 5: tmp = Date(5, 25, yr); break; case 6: tmp = Date(5, 31, yr); break; } return tmp; }
Date FatherDay(int yr) { // 3rd sunday in June int d; Date tmp; d = Date::get_day_code(yr, 6, 1); switch (d) { case 0: tmp = Date(6, 15, yr); break; case 1: tmp = Date(6, 21, yr); break; case 2: tmp = Date(6, 20, yr); break; case 3: tmp = Date(6, 19, yr); break; case 4: tmp = Date(6, 18, yr); break; case 5: tmp = Date(6, 17, yr); break; case 6: tmp = Date(6, 16, yr); break; } return tmp; }
Date IndependenceDay(int yr) { // July 4
Date tmp(7, 4, yr); return tmp; }
Date Labor(int yr) {//first monday in Sep. int d; Date tmp; d = Date::get_day_code(yr, 9, 1); switch (d) { case 0: tmp = Date(9, 2, yr); break; case 1: tmp = Date(9, 1, yr); break; case 2: tmp = Date(9, 7, yr); break; case 3: tmp = Date(9, 6, yr); break; case 4: tmp = Date(9, 5, yr); break; case 5: tmp = Date(9, 4, yr); break; case 6: tmp = Date(9, 3, yr); break; } return tmp; }
Date Columbus(int yr) { // second monday of october int d; Date tmp; d = Date::get_day_code(yr, 10, 1); switch (d) { case 0: tmp = Date(10, 9, yr); break; case 1: tmp = Date(10, 8, yr); break; case 2: tmp = Date(10, 14, yr); break; case 3: tmp = Date(10, 13, yr); break; case 4: tmp = Date(10, 12, yr); break; case 5: tmp = Date(10, 11, yr); break; case 6: tmp = Date(10, 10, yr); break; } return tmp; }
Date Halloween(int yr) { Date tmp(11, 1, yr); // november 1st, a day after Halloween return tmp; } Date ElectionDay(int yr) { // 1st tuesday after 1st monday in november int d; Date tmp; d = Date::get_day_code(yr, 11, 1); switch (d) { case 0: tmp = Date(11, 3, yr); break; case 1: tmp = Date(11, 2, yr); break; case 2: tmp = Date(11, 8, yr); break; case 3: tmp = Date(11, 7, yr); break; case 4: tmp = Date(11, 6, yr); break; case 5: tmp = Date(11, 5, yr); break; case 6: tmp = Date(11, 4, yr); break; } return tmp; }
Date VeteransDay(int yr) { // Nov 11
Date temp(11, 11, yr); return temp;
}
Date thanksgiving(int yr) {// the fourth thursday of Novemeber int d; Date tmp; d = Date::get_day_code(yr, 11, 1); switch (d) { case 0: tmp = Date(11, 26, yr); break; case 1: tmp = Date(11, 25, yr); break; case 2: tmp = Date(11, 24, yr); break; case 3: tmp = Date(11, 23, yr); break; case 4: tmp = Date(11, 22, yr); break; case 5: tmp = Date(11, 28, yr); break; case 6: tmp = Date(11, 27, yr); break; } return tmp; }
Date xmas(int yr) { return Date(12, 25, yr); }
void process(Date(*fun)(int), int yr, int index) { Date d = fun(yr); cout << "/t" << setw(-20) << major[index] << setw(-15) << dayofweek[d.dayOfWeek()]; d.display(); cout << endl; }
void app1() {
int yr;
while (true) {
cout << "\tPlease enter the year (4 digits) or -1 to quit: "; cin >> yr; if (yr < 0) break; cout << " \t\tMajor holidays for year " << yr << endl << endl;
for (int i = 0; i < 18; i++) process(fp[i], yr, i);
cout << endl << endl; }
}
void app2() {
int yr; char resp[10]; string filename; ofstream out;
cout << " \tDo you want to write to a file?"; cin >> resp;
if (resp[0] == 'y' || resp[0] == 'Y') { cout << " \tPlease enter the file name: "; cin >> filename; filename += ".txt"; out.open(filename, ios::app); }
while (true) {
cout << "\tPlease enter the year (4 digits) or -1 to quit: "; cin >> yr; if (yr < 0) break; cout << " \t\tCalendar for year " << yr << endl << endl;
if (resp[0] == 'y' || resp[0] == 'Y') print_calendar(yr, out); else print_calendar(yr, cout);
cout << endl << endl; } if (resp[0] == 'y' || resp[0] == 'Y') out.close(); }
void print_calendar(int year, ostream &out) { int c, d; int dayofmonth[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; if (Date::isLeap(year)) dayofmonth[2] = 29; out << year; for (int i = 1; i < 13; i++) { out << endl << endl << Month[i] << endl << endl; out << setw(3) << "Sun" << setw(5) << "Mon" << setw(5) << "Tue" << setw(5) << "Wed" << setw(5) << "Thu" << setw(5) << "Fri" << setw(5) << "Sat" << endl; int j = 1; c = Date::get_day_code(year, i, j); if (c == 0) out << setw(3) << j; else { out << setw(3 + 5 * c) << j; } for (int j = 2; j < dayofmonth[i] + 1; j++) { d = Date::get_day_code(year, i, j); if (d % 7 == 0) { out << endl; out << setw(3) << j; } else out << setw(5) << j; } } }
// Date Class
#ifndef DATE_H #define DATE_H #include #include #include #include #include #include
using namespace std;
static string name[] = { "", "Jan","Feb", "Mar", "Apr","May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; static string week[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" }; static int days[2][13] = { { 0,31,28,31,30,31,30,31,31,30,31,30,31 },{ 0,31,29,31,30,31,30,31,31,30,31,30,31 } };
class Date { int *day; int *month; int *year;
public:
Date(); // default c'tor set to the date of Date(int dd, int mm, int yy); Date(Date &other); // copy c'tor
Date(int yy); // use the Jan 1st as the default day and month Date(int doy, int yy); // doy=day of the year ~Date(); // d'tor
int getDay(); int getMonth(); int getYear();
// overload operators Date & operator= (Date & rhs); // assignment operator Date & operator++(); // prefix ++ Date & operator ++ (int); // postfix ++ Date & operator --(); // prefix -- Date & operator -- (int); // postfix -- int operator - (Date & other); // returns the number of days between this and other Date & operator + (int n); // n can be positive or negative bool operator > (Date & rhs); bool operator < (Date & rhs); bool operator ==(Date & rhs); friend ostream & operator << (ostream & out, Date& rhs); friend istream &operator >> (istream & in, Date &rhs);
// other auxilary functions
int isLeap(int year); // return 0 if given year is not a leap year, 1 otherwise int day_code(int year); // return the day code for the new year day of given year [ 0 - 6 ] 0 means sunday .. int day_code(int dd, int mm, int yy); int day_code(); // return the day code for the new year day of this->year void print(int yy, ostream& out); // print the calendar for the given year void print(int yy, int mm, ostream& out); // print the calendar for the give n month of a given year
string toString(); // mm/dd/yyyy format string toString(int type); // Jan 01 , 2017 or SUn Jan 01, 2017
int dayOfYear(); // };
Date::Date() { // default c'tor month = new int(1); day = new int(1); year = new int(2017);
} Date::Date(int dd, int mm, int yy) {
day = new int(dd); month = new int(mm); year = new int(yy); }
Date::Date(Date &other) { day = new int(other.getDay()); month = new int(other.getMonth()); year = new int(other.getYear()); }
Date::Date(int yy) { // use the Jan 1st as the default day and month day = new int(1); month = new int(1); year = new int(yy); }
Date::Date(int doy, int yy) { // doy=day of the year day = new int(doy); month = new int(1); year = new int(yy); }
Date::~Date() { // d'tor delete(month); delete(day); delete(year); }
int Date::getDay() { return *day; }
int Date::getMonth() { return *month; }
int Date::getYear() { return *year; }
// overload operators Date & Date::operator= (Date & rhs) { // assignment operator if (this != &rhs) { month = new int(rhs.getMonth()); day = new int(rhs.getDay()); year = new int(rhs.getYear()); } return *this; }
bool Date::operator > (Date & rhs) { if (*year != rhs.getYear()) return(*year > rhs.getYear()); else if (*month != rhs.getMonth()) return (*month > rhs.getMonth()); else return (*day > rhs.getDay()); }
bool Date::operator <(Date & rhs) { if (*year != rhs.getYear()) return(*year < rhs.getYear()); else if (*month != rhs.getMonth()) return (*month < rhs.getMonth()); else return (*day < rhs.getDay()); }
Date & Date::operator++() { // prefix ++ int dayofmonth[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; if (isLeap(*year)) dayofmonth[2] = 29;
if (*month == 12 && *day == 31) { *day = 1; *month = 1; *year += 1; } else if (*month != 12 && *day == dayofmonth[*month]) { *day = 1; *month += 1; } else *day += 1;
return *this; }
Date & Date::operator ++ (int) { // postfix ++ int daysofmonth[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; if (isLeap(*year)) daysofmonth[2] = 29;
Date *tmp; tmp = new Date(*this);
if (*month == 12 && *day == 31) { *day = 1; *month = 1; *year += 1; } else if (*month != 12 && *day == daysofmonth[*month]) { *day = 1; *month += 1; } else *day += 1;
return *tmp; }
Date & Date::operator --() { // prefix -- int dayofmonth[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; if (isLeap(*year)) dayofmonth[2] = 29;
if (*month == 1 && *day == 1) { *day = 31; *month = 12; *year -= 1; } else if (*month != 1 && *day == 1) { *month -= 1; *day = dayofmonth[*month]; } else *day -= 1;
return *this;
} Date & Date::operator -- (int) { // postfix -- int dayofmonth[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; if (isLeap(*year)) dayofmonth[2] = 29;
Date *tmp; tmp = new Date(*this);
if (*month == 1 && *day == 1) { *day = 31; *month = 12; *year -= 1; } else if (*month != 1 && *day == 1) { *month -= 1; *day = dayofmonth[*month]; } else *day -= 1;
return *tmp;
}
int Date::operator - (Date & other) { // returns the number of days between this and other int i = 0; while (operator >(other)) { operator--(); i++; }
while (operator <(other)) { operator++(); i--; }
return i; }
Date & Date::operator + (int n) { // n can be positive or negative for (int i = 0; i < abs(n); i++) { operator++(); } return *this; }
bool Date::operator ==(Date & rhs) { return(*month == rhs.getMonth() && *day == rhs.getDay() && *year == rhs.getYear()); }
ostream & operator << (ostream & out, Date& rhs) {
out << rhs.toString() << endl; return out;
} istream &operator >> (istream & in, Date &rhs) {
in >> *(rhs.month) >> *(rhs.day) >> *(rhs.year);
return in; }
// other auxilary functions
int Date::isLeap(int year) { return ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0)); }
int Date::day_code(int year) { return (year + (year - 1) / 4 - (year - 1) / 100 + (year - 1) / 400) % 7; }
int Date::day_code(int dd, int mm, int yy) { int d = 0; int daycode; int daysofmonth[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; if (isLeap(yy)) daysofmonth[2] = 29;
for (int i = 1; i < mm; i++) d += daysofmonth[i];
d += dd; daycode = (day_code(yy) + d - 1) % 7; return daycode; }
int Date::day_code() { return day_code(*year); }
void Date::print(int yy, ostream& out) { // print the calendar for the given year int c, d; int daysofmonth[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; if (isLeap(yy)) daysofmonth[2] = 29; out << year; for (int i = 1; i < 13; i++) { out << endl << endl << daysofmonth[i] << endl << endl; out << setw(3) << "Sun" << setw(5) << "Mon" << setw(5) << "Tue" << setw(5) << "Wed" << setw(5) << "Thu" << setw(5) << "Fri" << setw(5) << "Sat" << endl; int j = 1; c = day_code(j, i, yy); if (c == 0) out << setw(3) << j; else { out << setw(3 + 5 * c) << j; } for (int j = 2; j < daysofmonth[i] + 1; j++) { d = day_code(j, i, yy); if (d % 7 == 0) { out << endl; out << setw(3) << j; } else out << setw(5) << j; } } }
void Date::print(int yy, int mm, ostream& out) {// print the calendar for the give n month of a given year int c, d; int daysofmonth[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; if (isLeap(yy)) daysofmonth[2] = 29; out << year; out << month; for (int i = 1; i < 13; i++) { if (daysofmonth[i] == mm) { // out << endl << endl << daysofmonth[i] << endl << endl; out << setw(3) << "Sun" << setw(5) << "Mon" << setw(5) << "Tue" << setw(5) << "Wed" << setw(5) << "Thu" << setw(5) << "Fri" << setw(5) << "Sat" << endl; int j = 1; c = day_code(j, i, yy); if (c == 0) out << setw(3) << j; else { out << setw(3 + 5 * c) << j; } for (int j = 2; j < daysofmonth[i] + 1; j++) { d = day_code(j, i, yy); if (d % 7 == 0) { out << endl; out << setw(3) << j; } else out << setw(5) << j; } } } }
string Date::toString() { // mm/dd/yyyy format
ostringstream convert; // stream used for the conversion
convert << *month << "/" << *day << "/" << *year;
return convert.str(); }
string Date::toString(int type) { // type 1 : Jan 01 , 2017 or type 2: SUn Jan 01, 2017 string tmp; char buf[80];
int week_day = day_code(*year); sprintf_s(buf, "%d %d , %d", *month, *day, *year); if (type == 1) tmp = string(buf); else if (type == 2) tmp = string(buf + week[week_day]);
return tmp;
}
int Date::dayOfYear() { int doy = 0; int daysofmonth[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; if (isLeap(*year)) daysofmonth[2] = 29; for (int i = 1; i < *month; i++) { doy += daysofmonth[i]; } doy += *day; return doy; }
#endif
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
