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
|
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
void printmonth(const int month, const int startday, const bool leap);
void printyear(const int year);
int dayofweek(const int day, const int month, const int year);
int main(void)
{
int year;
cout << "Enter a year after 1582 to be printed" << endl;
cin >> year;
cout << year << endl;
printyear(year);
}
void printyear(const int year)
{
bool leapyear = false;
if(year % 4 == 0)
{
if(year % 100 !=0)
leapyear = true;
}
int mon = 1;
for(mon = 1;mon < 13; mon++)
printmonth(mon,dayofweek(1,mon,year),leapyear);
}
//Finds what day of the week the year starts on
int dayofweek(const int day, const int month, const int year)
{
int a,y,m;
a=(14-month)/12;
y=year-a;
m= month+12*a-2;
return (day+y+y/4-y/100 + y/400 + (31*m/12))%7;
}
void printmonth(const int month, const int startday, const bool leap)
{
//Makes the arrays for the days of the week, the months, the days in each in month, and changes the days in Feb to 29 if leap is true.
string Days[7] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
string Months[13] = {" ", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
int DaysinMonth[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
//Checks to see if it is a leap year and changes the days in February accordingly.
if(leap)
DaysinMonth[2] = 29;
// Finds the length of the name of the month,divides it by 2, and adds that length + 14 spaces to the beginning, centering it.
string str = Months[month];
int len = str.length();
cout << setw((len/2)+14) << Months[month] << endl;
cout << " Sun Mon Tue Wed Thu Fri Sat" << endl;
// Adds four spaces for each day before startday
int i = 1;
int count = 0;
for(count = 0; count < startday ; count++)
{
cout << " ";
}
// Counts through the days of the month.
for(i=1;i<=DaysinMonth[month];i++)
{
// Outputs each days numerical value, checks to see if it is the end of the week, and goes to the next day
cout << setw(4) << i;
if((startday+i) % 7 == 0)
cout << endl;
}
cout << endl;
}
|