Using Date class to implement a C++ application \" Calendar System \", which has
ID: 3811207 • Letter: U
Question
Using Date class 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. Provides a list of major holidays and the start/end date of the Day Light Saving Time of a given year.
Main CPP:
#include <iostream>
#include <string>
#include <iomanip>
#include "Date.h"
#include <fstream>
using namespace std;
void options(int &choice);
void calendar1(int &choice);
void calendar11_year(int yr);
void calendar12_month(int yr, int mm);
void calendar2_holiday(int yr);
void output(int yr);
void process(Date(*fun)(int), int yr, int index);
Date NewYear(int yr);
Date MLKDay(int yr);
Date LincolnBday(int yr);
Date Valentine(int yr);
Date PresidentDay(int yr);
Date StPatrick(int yr);
Date daylightSaving(int yr, Date &end);
Date AprilFool(int yr);
Date MayDay(int yr);
Date MotherDay(int yr);
Date MemorialDay(int yr);
Date FlagDay(int yr);
Date FatherDay(int yr);
Date IndependenceDay(int yr);
Date Labor(int yr);
Date Columbus(int yr);
Date Halloween(int yr);
Date ElectionDay(int yr);
Date VeteransDay(int yr);
Date thanksgiving(int yr);
Date xmas(int yr);
// 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" };
int main() {
int choice =0;
int year = 2017, month = 1;
cout << endl << endl << " Welcome to CSC309/404 Calendar System ";
do {
options(choice);
cout << endl;
if (choice == 1) {
calendar1(choice);
if (choice == 1) {
output(year);
calendar11_year(year);
}
else if (choice == 2) {
output(year);
calendar12_month(year, month);
}
else if (choice == 0)
break;
else
continue;
}
else if (choice == 2) {
output(year);
calendar2_holiday(year);
}
else if (choice == 0)
break;
else
continue;
} while (choice !=0);
return 0;
}
void options(int &choice) {
cout << " Options: ";
cout << " Enter 1 for Calendar"
<< " 2 for Holidays"
<< " 0 to quit.";
cout << " Your choice is ";
cin >> choice;
}
void calendar1(int &choice) {
cout << " Enter 1 for the calendar for the given year"
<< " 2 for the calendar for the given month of the given year"
<< " 0 tor exit"
<< " Please enter your choice : ";
cin >> choice;
}
void calendar11_year(int yr) {
Date d;
cout << " Please enter the year : ";
cin >> yr;
if (yr < 0) return;
cout << " Calendar for year " << yr << endl << endl;
d.print(yr, cout);
}
void calendar12_month(int yr, int mm) {
Date d;
cout << " Please enter the year : ";
cin >> yr;
cout << " Please enter the month :";
cin >> mm;
if (yr < 0||mm<0) return;
cout << " Calendar for "<<mm << yr << endl << endl;
d.print(yr, mm,cout);
}
void calendar2_holiday(int yr){
cout << " Please enter the year: ";
cin >> yr;
if (yr < 0) return;
cout << " Major holidays for year " << yr << endl << endl;
for (int i = 0; i < 18; i++)
process(fp[i], yr, i);
cout << endl << endl;
}
void output(int yr){
char resp[10];
string filename;
ofstream out;
cout << " Do you want to write to a file too?";
cin >> resp;
if (resp[0] == 'y' || resp[0] == 'Y') {
cout << " Please enter the file name: ";
cin >> filename;
filename += ".txt";
cout << " append mode (Y/N) ?";
cin >> resp;
if(resp[0] == 'y' || resp[0] == 'Y')
out.open(filename, ios::app);
else
out.open(filename);
}else
return;
}
void process(Date(*fun)(int), int yr, int index) {
Date d = fun(yr);
int week_day = d.day_code(yr);
cout << "/t" << setw(-20) << major[index] << setw(-15) << dayofweek[week_day];
d.toString();
cout << endl;
}
///////////////////////////////
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 = tmp.day_code(yr);
switch (d) {
case 0: tmp = Date(16, 1, yr); break;
case 1: tmp = Date(15, 1, yr); break;
case 2: tmp = Date(21, 1, yr); break;
case 3: tmp = Date(20, 1, yr); break;
case 4: tmp = Date(19, 1, yr); break;
case 5: tmp = Date(18, 1, yr); break;
case 6: tmp = Date(17, 1, yr); break;
}
return tmp;
}
Date LincolnBday(int yr) {
return Date(12, 2, yr);
}
Date Valentine(int yr) {
return Date(14, 2, yr);
}
Date PresidentDay(int yr) { // 3rd monday of Febraury
int d;
Date tmp;
d = tmp.day_code(1, 2, yr);
switch (d) {
case 0: tmp = Date(16, 2, yr); break;
case 1: tmp = Date(15, 2, yr); break;
case 2: tmp = Date(21, 2, yr); break;
case 3: tmp = Date(20, 2, yr); break;
case 4: tmp = Date(19, 2, yr); break;
case 5: tmp = Date(18, 2, yr); break;
case 6: tmp = Date(17, 2, yr); break;
}
return tmp;
}
Date StPatrick(int yr) { //mar 17
return Date(17, 3, yr);
// Date temp(3, 17, yr);
// return temp;
}
Date daylightSaving(int yr, Date &end) {
int d, m;
Date tmp1, tmp2;
d = tmp1.day_code(1, 3, yr);
switch (d) {
case 0: tmp1 = Date(8, 3, yr); break;
case 1: tmp1 = Date(14, 3, yr); break;
case 2: tmp1 = Date(13, 3, yr); break;
case 3: tmp1 = Date(12, 3, yr); break;
case 4: tmp1 = Date(11, 3, yr); break;
case 5: tmp1 = Date(10, 3, yr); break;
case 6: tmp1 = Date(9, 3, yr); break;
}
return tmp1;
m = tmp2.day_code(1, 11, yr);
switch (m) {
case 0: tmp2 = Date(1, 11, yr); break;
case 1: tmp2 = Date(7, 11, yr); break;
case 2: tmp2 = Date(6, 11, yr); break;
case 3: tmp2 = Date(5, 11, yr); break;
case 4: tmp2 = Date(4, 11, yr); break;
case 5: tmp2 = Date(3, 11, yr); break;
case 6: tmp2 = Date(2, 11, yr); break;
}
end = tmp2;
}
Date AprilFool(int yr) {
return Date(1, 4, yr);
}
Date MayDay(int yr) { // May ist
// Date tmp(5, 1, yr);
// return tmp;
return Date(1, 5, yr);
}
Date MotherDay(int yr) { // 2nd sunday in may
int d;
Date tmp;
d = tmp.day_code(1, 5, yr);
switch (d) {
case 0: tmp = Date(8, 5, yr); break;
case 1: tmp = Date(14, 5, yr); break;
case 2: tmp = Date(13, 5, yr); break;
case 3: tmp = Date(12, 5, yr); break;
case 4: tmp = Date(11, 5, yr); break;
case 5: tmp = Date(10, 5, yr); break;
case 6: tmp = Date(9, 5, yr); break;
}
return tmp;
}
Date MemorialDay(int yr) {//last Monday in May
int d;
Date tmp;
d = tmp.day_code(1, 5, yr);
switch (d) {
case 0: tmp = Date(30, 5, yr); break;
case 1: tmp = Date(29, 5, yr); break;
case 2: tmp = Date(28, 5, yr); break;
case 3: tmp = Date(27, 5, yr); break;
case 4: tmp = Date(26, 5, yr); break;
case 5: tmp = Date(25, 5, yr); break;
case 6: tmp = Date(31, 5, yr); break;
}
return tmp;
}
Date FlagDay(int yr) { // July 4
return Date(14, 6, yr);
}
Date FatherDay(int yr) { // 3rd sunday in June
int d;
Date tmp;
d = tmp.day_code(1, 6, yr);
switch (d) {
case 0: tmp = Date(15, 6, yr); break;
case 1: tmp = Date(21, 6, yr); break;
case 2: tmp = Date(20, 6, yr); break;
case 3: tmp = Date(19, 6, yr); break;
case 4: tmp = Date(18, 6, yr); break;
case 5: tmp = Date(17, 6, yr); break;
case 6: tmp = Date(16, 6, yr); break;
}
return tmp;
}
Date IndependenceDay(int yr) { // July 4
return Date(4, 7, yr);
// Date tmp(7, 4, yr);
// return tmp;
}
Date Labor(int yr) {//first monday in Sep.
int d;
Date tmp;
d = tmp.day_code(1, 9, yr);
switch (d) {
case 0: tmp = Date(2, 9, yr); break;
case 1: tmp = Date(1, 9, yr); break;
case 2: tmp = Date(7, 9, yr); break;
case 3: tmp = Date(6, 9, yr); break;
case 4: tmp = Date(5, 9, yr); break;
case 5: tmp = Date(4, 9, yr); break;
case 6: tmp = Date(3, 9, yr); break;
}
return tmp;
}
Date Columbus(int yr) { // second monday of october
int d;
Date tmp;
d = tmp.day_code(1, 10, yr);
switch (d) {
case 0: tmp = Date(9, 10, yr); break;
case 1: tmp = Date(8, 10, yr); break;
case 2: tmp = Date(14, 10, yr); break;
case 3: tmp = Date(13, 10, yr); break;
case 4: tmp = Date(12, 10, yr); break;
case 5: tmp = Date(11, 10, 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;
return Date(31, 10, yr);
}
Date ElectionDay(int yr) { // 1st tuesday after 1st monday in november
int d;
Date tmp;
d = tmp.day_code(1, 11, yr);
switch (d) {
case 0: tmp = Date(3, 11, yr); break;
case 1: tmp = Date(2, 11, yr); break;
case 2: tmp = Date(8, 11, yr); break;
case 3: tmp = Date(7, 11, yr); break;
case 4: tmp = Date(6, 11, yr); break;
case 5: tmp = Date(5, 11, yr); break;
case 6: tmp = Date(4, 11, yr); break;
}
return tmp;
}
Date VeteransDay(int yr) { // Nov 11
return Date(11, 11, yr);
// Date temp(11, 11, yr);
// return temp;
}
Date thanksgiving(int yr) {// the fourth thursday of Novemeber
int d;
Date tmp;
d = tmp.day_code(1, 11, yr);
switch (d) {
case 0: tmp = Date(26, 11, yr); break;
case 1: tmp = Date(25, 11, yr); break;
case 2: tmp = Date(24, 11, yr); break;
case 3: tmp = Date(23, 11, yr); break;
case 4: tmp = Date(22, 11, yr); break;
case 5: tmp = Date(28, 11, yr); break;
case 6: tmp = Date(27, 11, yr); break;
}
return tmp;
}
Date xmas(int yr) {
return Date(25, 12, yr);
}
// Date.h
#ifndef DATE_H
#define DATE_H
#include <iostream>
#include <string>
#include <ctime>
#include <sstream>
#include <iomanip>
#include <fstream>
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
// put your code here
month = new int(1);
day = new int(1);
year = new int(2017);
}
Date::Date(int dd, int mm, int yy) {
// put your code here
// this->day = new int(dd);
// this->month = new int(mm);
// this->year = new int(yy);
day = new int(dd);
month = new int(mm);
year = new int(yy);
}
Date::Date(Date &other) {
// put your code here
// month = other.month;
// day = other.day;
// year = other.year;
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
// put your code here
day = new int(1);
month = new int(1);
year = new int(yy);
}
Date::Date(int doy, int yy) { // doy=day of the year
// put your code here
day = new int(doy);
month = new int(1);
year = new int(yy);
}
Date::~Date() { // d'tor
// put your code here
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
// put your code here
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) {
// put your code here
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) {
// put your code here
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 ++
// put your code here
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 ++
// put your code here
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 --
// put your code here
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 --
// put your code here
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
// put your code here
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
// put your code here
for (int i = 0; i < abs(n); i++) {
operator++();
}
return *this;
}
bool Date::operator ==(Date & rhs) {
// put your code here
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) {
// put your code here
return ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0));
}
int Date::day_code(int year) {
// put your code here
return (year + (year - 1) / 4 - (year - 1) / 100
+ (year - 1) / 400) % 7;
}
int Date::day_code(int dd, int mm, int yy) {
// put your code here
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() {
// put your code here
return day_code(*year);
}
void Date::print(int yy, ostream& out) { // print the calendar for the given year
// put your code here
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
// put your code here
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
// put your code here
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() {
// put your code here
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
Explanation / Answer
This is the answer to your question. I hope it was helpful.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.