#include<iostream.h>
#include<iomanip.h>
int IsleapYear(int year);//判断是否为闰年
int dayofMonth(int month,int year);//求一个月有多少天
int showofMonth(int month,int year,int spaceday);//输出每月的日期
long FirstdayofYear(int year);
int main()
{
int year,day;
do{
cout<<"please input the year:";
cin>>year;
if(year<1)
cout<<"the year couldn't less one!"<<endl;
}while (year<1);
day=FirstdayofYear(year);
cout<<endl<<year<<"年年历";
for(int i=1;i<=12;i++)//输出月份
{
day=showofMonth(i,year,day);
}
cout<<endl;
return 0;
}
int IsleapYear(int year)
{
return(year%4==0&&year%100!=0||year%400==0);
}
int dayofMonth(int month,int year)
{
switch(month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:return 31;//1,3,5,7,8,10,12,月每月有31天
case 4:
case 6:
case 9:
case 11:return 30;//4,6,9,11月每月31天
case 2:
if(IsleapYear(year))
return 29;//闰年每年二月29天
else
return 28;//非闰年二月28天
}
}
int showofMonth(int month,int year,int spaceday)
{
int i,days;
cout<<endl<<endl<<endl;
cout<<setw(4)<<month<<"月"<<endl;
cout<<setw(4)<<"日"<<setw(4)<<"一"<<setw(4)<<"二"<<setw(4)<<"三"<<setw(4)<<"四"<<setw(4) <<"五"<<setw(4)<<"六"<<endl;
for(i=1;i<=spaceday;i++)//控制是星期的输出
cout<<setw(4)<<" ";
days=dayofMonth(month,year);//得到每月的天数
for(i=1;i<=days;i++)
{
cout<<setw(4)<<i;
spaceday=(spaceday+1)%7;//算出当天是星期几
if(spaceday==0)
cout<<endl<<endl;
}
return spaceday;
}
long FirstdayofYear(int year)
{
long n;
int i;
n=year*365;//从公元1 月1 日天始算, 到现在所输入年月有多少天
for(i=1;i<=year;i++)
if(IsleapYear(i))
n++;//闰年多一天
return n%7;//返回星期数
}