kikkxxikkxxkxx**kkxkxixikxxk Menu * 1. Query a date * 2. Print calendar of a giv
ID: 3852237 • Letter: K
Question
kikkxxikkxxkxx**kkxkxixikxxk Menu * 1. Query a date * 2. Print calendar of a given month * 3. Print calendar of a given year * 4. Print calendar of a given year to file * 5. Quit kikkxxikkxxkxx**kkxkxixikxxk Please enter your choice: 1 Enter the date in form of mm dd yyyy: 10 25 2016 October 25, 2016 is Tuesday kikkxxikkxxkxx**kkxkxixikxxk Menu * 1. Query a date * 2. Print calendar of a given month * 3. Print calendar of a given year * 4. Print calendar of a given year to file * 5. Quit kikkxxikkxxkxx**kkxkxixikxxk Please enter your choice: 2 Enter the month and year in form of mm yyyy: 10 2016 October, 2016 SUN MON TUE WED THU FRI SAT 2 3 45 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 kikkxxikkxxkxx**kkxkxixikxxk Menu * 1. Query a date * 2. Print calendar of a given month * 3. Print calendar of a given year * 4. Print calendar of a given year to file * 5. Quit kikkxxikkxxkxx**kkxkxixikxxk Please enter your choice: 3 Enter the year as yyyy: 2016Explanation / Answer
#include<iostream>
#include<fstream>
#include<iomanip>
#include<string>
using namespace std;
void get_input(int& month, int& day, int& year);
//postcondition: 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);
//postcondition: 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);
//postcondition: return true if the given year is valid. false otherwise the given year is valid if it is later than year 1582
bool isValid(int month, int day, int year);
bool isValid(int month, int year);
bool isValid(int year);
void print_calendar(int month, int day, int year);
//precondition:
void print_calendar(int month, int year);
void print_calendar(int year);
string monthName(int month);
int daysInMonth(int month, int year);
void menu();
bool isLeapYear(int year);
int getCenturyValue(int year);
int getYearValue(int year);
int getMonthValue(int month, int year);
int dayOfweek(int month, int day, int year);
string dayOfWeek(int day);
bool print_calendar(int year, string fileName);
void print_calendar(int month, int year, ofstream& output);
int main(){
char choice;
int day, month, year;
do{
system("CLS");
menu(); //display menu bt calling menu function
cout<<" Please enter your choice: ";
cin>>choice;
switch (choice){
case '1':
get_input(month,day,year);
print_calendar(month,day,year);
break;
case '2':
get_input(month,year);
print_calendar(month,year);
break;
case '3':
get_input(year);
print_calendar(year);
case '4':
cout << "Enter the year: ";
cin>> year;
if(print_calendar(year, "file.txt")){
cout<< "The calendar of year " << year<< " has been printed to file.txt ";
} else
cout<< " Failed to print out calendar of year " << year << endl;
case '5':
cout<<" Thank you for using calendar program! ";
break;
default:
cout<<"Error, mo such choice. Please choose from menu. ";
break;
}
cout<< " ";
system("pause");
}while (choice != '5');
return 0;
}
void menu(){
cout<<" ******************************************";
cout<<" *";
cout<<" * 1. Query a date*";
cout<<" * 2. Print calendar of a given month *";
cout<<" * 3. Print calendar of a given year *";
cout<<" * 4. Print calendar to file *";
cout<<" * 5. Quit";
cout<<" *******************************************";
}
void get_input(int& month, int& day, int& year){
cout<< " Enter the date in form of mm dd yyyy";
cin>> month >> day >> year;
while (!isValid(month,day,year))
{
cout<< " The date you entered is NOT valid. Please reenter a valid date: ";
cin>> month >> day >> year;
}
}
void get_input(int& month, int& year){
cout<< " Enter the month in form of mm yyyy";
cin>> month >> year;
while (!isValid(month,year))
{
cout<< " The month you entered is NOT valid. Please reenter a valid month: ";
cin>> month >> year;
}
}
void get_input(int& year){
cout<< " Enter the year as yyyy";
cin>> year;
while (!isValid(year))
{
cout<< " The year you entered is NOT valid. Please reenter a valid year: ";
cin>> year;
}
}
bool isValid(int month, int day, int year){
if(!isValid(month, year))
return false; // first check month and year valid
bool result = true;
switch (month){
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);
}
bool isValid(int month, int year){
return month >= 1 && month <= 12 && year > 1582;
}
bool isValid(int year){
return year > 1582;
}
bool isLeapYear(int year){
return (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0));
}
int getCenturyValue(int year)
{
return 2 * (3- (year / 100) % 4);
}
int getYearValue(int year){
return year % 100 + year % 100 /4;
}
int getMonthValue(int month, int year){
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;
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:
result = 0;
break;
}
return result;
}
int dayOfWeek(int month, int day, int year){
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){
if(!isValid(month,day,year)){
cout<< " The date is not valid!";
return;
}
cout<< " " << monthName(month)<< "" <<day << "," << year << " is " << dayOfWeek(dayOfWeek(month,day,year));
}
void print_calendar(int month, int year){
if(!isValid(month,year)){
cout<< " The month is NOT valid!";
return;
}
cout<< " " << monthName(month) << "," << year << endl;
cout<< " SUN MON TUE WED THU FRI SAT ";
int spaceCount = dayOfWeek(month, 1,year);
for (int i = 0; i < spaceCount; ++i)
cout<< setw(4) << "";
int days = daysInMonth(month, year);
for (int j = 1; j < days; ++j) {
cout<< setw(4) << j;
if((!spaceCount + j) % 7 == 0)
cout<< " ";
}
}
void print_calendar(int year){
if(!isValid(year)){
cout<< " The year is NOT valid! ";
return;;
}
for (int i = 1; i <= 12; ++i) {
print_calendar(i, year);
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";
}
}
int daysInMonth(int month, int year){
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;
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 year is not valid print out error message and return false
if(!isValid(year)){
cout << "The year is not valid ";
return false;
}
// declare a locat ofstream variable and open file to write
ofstream fout;
fout.open(fileName);
//if file open failed, print out error message and return false
if(fout.fail()){
cout<< " can not open file " << fileName << "to write ";
return false;
}
//use for loop to print out 12 months to the file
for (int i = 1; i <= 12; ++i) {
print_calendar(i, year, fout);
fout<< " ";
}
//close file
fout.close();
return true;
}
void print_calendar(int month, int year, ofstream& output){
if(!isValid(month,year)){
cout<< " The month is NOT valid!";
return;
}
output<< " " << monthName(month) << "," << year << endl;
output<< " SUN MON TUE WED THU FRI SAT ";
int spaceCount = dayOfWeek(month, 1,year);
for (int i = 0; i < spaceCount; ++i)
output<< setw(4) << "";
int days = daysInMonth(month, year);
for (int j = 1; j < days; ++j) {
output<< setw(4) << j;
if((!spaceCount + j) % 7 == 0)
output<< " ";
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.