Write a program to generate the entire calendar for one year. The program must g
ID: 675135 • Letter: W
Question
Write a program to generate the entire calendar for one year. The program must get two values from the user: (1) the year and (2) the day of the week for January 1st of that year. The year, which should be positive, is needed to check for and handle leap years1. The day of the week for January 1st is needed so that you know where to start the calendar. The user should enter 0 for Sunday, 1 for Monday, … or 6 for Saturday. As always, you need to validate the user's input. To actually print the calendar, you must use a single method that prints out the calendar for one month and then call this function 12 times from main(), once for each month in the year. To check for a leap year you will need to write another method that takes the year as a parameter and returns true if it’s a leap year, or false otherwise. Stubs (i.e. method signatures without any code) for both of these methods have been provided for you. The calendar should be printed in the form below. In this example, January starts on a Saturday (day 6). Note that February starts on a Tuesday in this example because January ended on a Monday. January 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 February 1 2 3 4 5 6 7 8 9 10 11 12 … 1 See https://en.wikipedia.org/wiki/Leap_year - Algorithm This is a very complex program with many pieces, and there are many different ways to approach it. Before writing any code, take some time to think through the problem. The most difficult piece is to understand how to write a single method that can print out the calendar for any month. Here is a suggestion for how to proceed: 1. Try to write the isLeapYear method – this is self-contained and there is a unit test to help you determine if you understand the logic. 2. Now focus on the printMonth method – there is another unit test to help you make sure you understand the logic here. Start with printing the name of the month (supplied as a parameter) and trailing new lines. Then try getting the correct number of days printed (also supplied as a parameter) without worrying about which day the month starts on. Then focus on getting the numbers to line up (hint: how are you going to deal with a single digit vs. double digits), then on starting a new line after each Saturday. Now make sure your return value is correct – it should be one day of the week later than the last printed day (with special handling if the last day is Saturday). Finally, print “blank” entries for each day until the first of the month. For example, if January 1st is a Wednesday, you need to print 3 blank entries. 3. Next, shift your focus to the main() method. Start by getting input from the user and validating them (there are unit tests focused on these, as well as error messages provided for you). 4. Now, assuming the inputs are supplied correctly, call your methods to print out each month (i.e. 12 calls to printMonth). Note that you will need to use the return value from this method to decide where to start each subsequent month. 5. Finally, add correct support for leap years – think about how to use your isLeapYear method to correctly output February. You have been supplied JUnit tests for the two methods, validating the inputs in main(), as well as the output for the entire calendar for the year 2015.
Explanation / Answer
Here is the code for creating a calendar for one year:
#include <iostream>
#include <cstdlib>
#include <iomanip>
using namespace std;
bool isLeapYear (int); //good check for leap years
int firstDayofnewyearMonth (int );
int numOfDaysInMonth (int, bool); // takes the number of the month, a flag saying whether year is leap
void printHeader (int); // takes the number of the month, and the first day, prints, and updates
void printMonth (int, int&); // the first day of the next month
void skip (int);// prints the specified amount of spaces
void skipToDay (int);// prints leading spaces in monthly calendar
void disaster (); // terminates program in case of unrecoverable errors
int main ()
{
system ("color f1 ");
int year, firstDayInCurrentMonth;
int currentMonth = 1;
int numDays;
bool leap;
cout << "What year do you want a calendar for? ";
cin >>year;
cout<<endl;
firstDayInCurrentMonth=firstDayofnewyearMonth(year);
leap = isLeapYear(year);
skip(9);
cout << year << endl;
while (currentMonth <= 12)
{
numDays = numOfDaysInMonth(currentMonth, leap);
printHeader(currentMonth);
printMonth(numDays, firstDayInCurrentMonth);
cout << endl << endl << endl;
currentMonth = currentMonth + 1;
}
cout << endl;
}
bool isLeapYear (int year)
{
return ((year%4==0) && (year%100 !=0))||(year%400==0) ;
}
int firstDayofnewyearMonth(int year)
{
int day_start;
int x1, x2, x3;
x1 = (year - 1)/ 4;
x2 = (year - 1)/ 100;
x3 = (year - 1)/ 400;
day_start = (year + x1 - x2 + x3) %7;
return day_start;
}
int numOfDaysInMonth (int m, bool leap)
{
if (m == 1) return(31);
else if (m == 2) if (leap) return(29);else return(28);
else if (m == 3) return(31);
else if (m == 4) return(30);
else if (m == 5) return(31);
else if (m == 6) return(30);
else if (m == 7) return(31);
else if (m == 8) return(31);
else if (m == 9) return(30);
else if (m == 10) return(31);
else if (m == 11) return(30);
else if (m == 12) return(31);
else disaster();
}
void printHeader (int m)
{
if (m == 1)
{
skip(7);
cout << "January" << endl;
}
else if (m == 2) { skip(7); cout << "February" << endl; }
else if (m == 3) { skip(7); cout << "March" << endl; }
else if (m == 4) { skip(7); cout << "April" << endl; }
else if (m == 5) { skip(7); cout << "May" << endl; }
else if (m == 6) { skip(7); cout << "June" << endl; }
else if (m == 7) { skip(7); cout << "July" << endl; }
else if (m == 8) { skip(7); cout << "August" << endl; }
else if (m == 9) { skip(7); cout << "September" << endl; }
else if (m == 10) { skip(7); cout << "October" << endl; }
else if (m == 11) { skip(7); cout << "November" << endl; }
else if (m == 12) { skip(7); cout << "December" << endl; }
else disaster();
cout << " S M T W T F S" <<endl;
cout << "____________________" << endl;
}
void skip (int i)
{
while (i > 0)
{
cout << " ";
i = i - 1;
}
}
void printMonth (int numDays, int &weekDay)
{
int day = 1;
skipToDay(weekDay);
while (day <= numDays)
{
cout << setw(2) << day << " ";
if (weekDay == 6)
{
cout << endl;
weekDay = 0;
}
else weekDay = weekDay + 1;
day = day + 1;
}
}
void skipToDay (int d)
{
return skip(3*d);
}
void disaster ()
{
cout << "Disaster! Exiting ..." << endl;
exit ( -1);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.