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
|
//**************************************************
//*This program is designed to print out a calendar*
//*for the year desired by the user and determine *
//*if the year is a loop year and modify a certain *
//*month based on that criteria, it also shows *
//*the process of function decomposition in depth *
//**************************************************
#include<iostream>
#include<cstdlib>
#include<iomanip> //Used for setw
#include <string>
using namespace std;
int firstWeekDay(/* in */int); //Function prototype with one value parameter
void getCalendarInput(/* inout */ int&); //Function prototype with one reference parameter
void yearlyCalendar(/* in */ int); //Function prototype with one value parameter
int main()
{
int year, month, day, totalDaysinMonth, weekDay = 0, startingDay; //Place holders for different situations as local variables
//Array used to store month names more convenient than an if statement that matches number to name
char * monthNames[] ={" January", " February", " March", " April", " May", " June", " July", " August", " September", " October", " November", " December"};
int monthDays[] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; //1-D Array that hold the total amount of days for each month
getCalendarInput(year); //Function call with one argument
yearlyCalendar(year); //Function call with one argument
return 0;
}
//List function definitions below
//*************** Function definition ***************
//*This function prompts the user to input their *
//*desired year they would want a calendar displayed*
//*for and it also includes input validation so the *
//*values entered are unique and not random *
//***************************************************
void getCalendarInput(/* inout */ int& year)
{
cout << "What year do you want the calendar for? ";
cin >> year;
while(year < 0 || year >9999 || !cin)
{
cin.clear();
cin.ignore(200 , '\n');
cout << "non digit entered" << endl;
cout << "What year do you want the calendar for? ";
cin >> year;
}
}
int firstWeekDay(/* in */ int year)
{
int startday;
int num1, num2, num3;
num1 = (year - 1)/ 4;
num2 = (year - 1)/ 100;
num3 = (year - 1)/ 400;
startday = (year + num1 - num2 + num3) %7;
return startday;
}
void yearlyCalendar(/* in */ int year)
{
int month, day, totalDaysinMonth, weekDay = 0, startingDay;
char * monthNames[] ={" January", " February", " March", " April", " May", " June", " July", " August", " September", " October", " November", " December"};
int monthDays[] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if(((year%4==0) && (year%100 !=0))||(year%400==0))
{
monthDays[1] = 29;
}
else
{
monthDays[1] = 28;
}
startingDay = firstWeekDay(year);
for(month = 0; month < 12; month++)
{
cout << endl;
cout << setw(10) << right << monthNames[month]<< "\n";
cout << " S M T W T F S\n";
cout << "_____________________\n";
for(weekDay = 0; weekDay < startingDay; weekDay++)
cout <<" ";
totalDaysinMonth = monthDays[month];
for(day = 1; day <= totalDaysinMonth; day++)
{
cout << setw(3)<< day;
if(++weekDay > 6)
{
cout << "\n";
weekDay = 0;
}
startingDay = weekDay;
}
cout << endl;
}
}
|