u011063451 于 2013.06.14 13:11 提问

``````#include <iostream>
#include <iomanip>
using namespace std;
char *WD="   Sun  Mon  Tue  Wed  Thu  Fri  Sat";
char *month[12]={"一月","二月","三月","四月","五月","六月","七月","八月","九月", "十月","十一月","十二月"};
static day[12]={31,28,31,30,31,30,31,31,30,31,30,31};
int leap(int year);
int week(int year);
int main()
{int i,j,year,date;
cout<<"请输入年份：";
cin>>year;
if(leap(year)) day[1]=29; //确定2月份的日期
date=week(year);
cout<<setw(21)<<endl;   //打印年的标题
for(i=0;i<12;i++)          //控制12个月份的打印,循环12次
{ cout<<endl;
for(int k=0;k<35;k++)
cout<<"     ";
cout<<endl<<setw(21)<<month[i];
cout<<endl<<WD<<endl;  //打印星期的标题
for(k=0;k<date;k++)   //格式控制
cout<<"     ";

for(j=1;j<=day[i];j++) //日期打印的控制
{
if((j+date)%7==1)
cout<<endl;
cout<<setw(5)<<j;
}
cout<<endl;
for(k=0;k<32;k++)   //格式控制
cout<<" ";
date=date+day[i]%7;
if(date>6)
date-=7;  }
cout<<endl;

}
int leap(int year)
{    //该函数确定年份是闰年,或平年
int leap;
if(year%400==0||year%100!=0&&year%4==0)
leap=1;
else leap=0;
return leap;
}

int week(int year)
{    //用于计算年份的1月1日是星期几
if (year%400==0||year%100!=0&&year%4==0)
return (year-1+year/4-year/100+year/400)%7;
else
return (year+year/4-year/100+year/400)%7;
}
``````