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 111 112
|
#include <iostream>
#include <stdlib.h>
#include <ctype.h>
#include <time.h>
#include <cctype>
#include <string>
#include <iomanip>
#include <stdio.h>
#include <stdlib.h>
#include <sstream>
using namespace std;
enum Month {Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec}; //defines month
bool isLeap(int year); // check for leap years
void dayName(); // prints the names for the day of the week
void monthNameHeader(int year); // puts head for the month name
int startDay(int year); // decides what week day Jan starts on
int monthCount(int counter); // how many days are in each month
void printAll(int year); // puts everything together, prints to screen
int year = 0; // User inputed year
int counter = 1; // counter for month name & # days in month
int startDOW, // day of the week Jan starts on
wrap, // check for if weekday is Saturday
daysInMonth; // total days in each month
int weekNumber = 0; // flag for first week of the month
int main(int argc, char ** argv)
{
int year=0;
string strInput = "";
cout<<"Please input the year number:"<<endl;
while (true){
getline(cin, strInput);
// This code converts from string to number safely.
stringstream myStream(strInput);
if ( (myStream >> year) )
break;
cout << "Invalid input, please try again" << endl;
}
if (year<1980 || year > 2099){
year=1980;
}
printAll(year);
return 0;
}
bool isLeap(int year){ // checking for possible leap year
if (year % 400 == 0){
return true;
}
else if (year % 100 == 0){
return false;
}
else if (year % 4 == 0){
return true;
}
else{
return false; // else return false
}
}
void dayName(){
string day_str[7]={"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
for(int i=0; i<7; i++){
cout << day_str[i] << " ";
}
cout << endl;
cout << "----------------------------------" << endl;
}
void monthNameHeader(int counter){
string month_str[12]={"January", "Feburary", "March", "April","May", "June", "July", "August","September", "October", "November", "December"};
cout << " "<<month_str[counter-1]<<endl;
}
int monthCount(int counter){ // how many days are in the month
int monthDays[12]= {31,28,31,30,31,30,31,31,30,31,30,31};
if (isLeap(year)==true){ //if it is a leap year, there are 29 days in Feb
monthDays[1]=29;
}
daysInMonth=monthDays[counter-1];
return daysInMonth;
}
int startDay(int year){
startDOW = (year + (year - 1 ) /4 - (year - 1) / 100 + (year - 1) /400) %7;
return startDOW; // formula for what DoWeek year starts on
}
void printAll(int year){
for (counter = 1; counter <= 12; counter++){
monthNameHeader(counter); // prints month day
dayName(); // prints the name of days
if (counter==1)
wrap = startDay(year) ; // what day Jan starts on
else
startDOW = wrap; // what day other months start on
cout << " ";
if (wrap != 7) // 1st extra if
for (int loopCount = 0; loopCount < startDOW; loopCount++){
cout << " "; // how many space to indent new month
}
monthCount(counter); // how many days in month
for (int dayCounter=1; dayCounter<=daysInMonth; dayCounter++){
if (wrap == 7){ //if Saturday, carriage return
if (dayCounter != 1)
cout << "\n "; // 2nd extra if
wrap = 0; //resets day of week counter
weekNumber++; //no longer first week of month
}
if (dayCounter<10) //adds space for single digit days
cout << " ";
cout << dayCounter << " "; //prints the day #
wrap++;
}
cout << endl; //space between months
cout << endl;
cout << endl;
} // end the master for loop
} //end main
|