/*============================================================================
Name : Exercise.cpp
Author : Haier
Version : 1.01
Copyright : Copyright (c) 2014
Description : Calender in C++, Ansi-style, Compile by Eclipse
============================================================================*/
#include <iostream>
#include <iomanip>
using namespace std;
const static char *Month[12]={"January","February","March","April","May","June","July","August","September","October","November","December"};
const static char *Week[7] ={"Sun.","Mon.","Tue.","Wed.","Thu.","Fri.","Sat."};
void PrintHead(int Number)
{
int i;
cout<<" "<<Month[Number]<<endl;
for(i=0; i<7; i++)
{
cout<<setw(8)<<left<<Week[i];
}
cout<<endl<<"_________________________________________________"<<endl;
}
int DaysOfMonth(int Number,int Year)
{
if(Number==2)
{
if((Year%4==0 && Year%100!=0) || Year%400==0)
{
return 29;
}
return 28;
}
else if(Number==4 || Number==6 || Number==9 || Number==11)
{
return 30;
}
else
{
return 31;
}
}
void skip( unsigned int i )
{
while ( i > 0 )
{
cout << " ";
i--;
}
}
void PrintDays(int Number,int &n)
{
int i;
skip(8*n);
for(i=1; i<=Number; i++)
{
cout<<left<<i<<"\t";
if(n==6)
{
cout<<endl;
n=0;
}
else
{
n++;
}
}
cout<<endl<<"_________________________________________________"<<endl;
}
unsigned int firstDayOfJanuary( unsigned int year )
{
return ( year + ( 97 * year - 97 ) / 400 ) % 7;
}
int main()
{
int i;
int Year;
int firstDayInCurrentMonth;
cout<<"Please input a year: ";
cin>>Year;
firstDayInCurrentMonth=firstDayOfJanuary(Year);
cout<<" "<<Year;
cout<<endl<<"_________________________________________________"<<endl;
for(i=1; i<=12; i++)
{
PrintHead(i);
PrintDays(DaysOfMonth(i,Year),firstDayInCurrentMonth);
}
}