#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int m[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int year,month,i,j,k,ans=1,sum=0,num=0;//ans用来计算所求年份的1月1日开头空多少日;sum用来计算所求月份开头空多少日 ;num用来做换行的计数
int flag=1;
while(flag)//交互
{
cout<<"输入年份:";
cin>>year;
cout<<"输入月份:";
cin>>month;
//闰年判断,若是闰年,二月份就为29天
if ((year%4==0&&year%100!=0)||(year%400==0)) m[2]=29;
//*************************判断所求年份year的1月1号的位置 ******************* ****
for (i=1900;i<=year-1;i++)
{
ans++;
if (ans==7) ans=0;
if ((i%4==0&&i%100!=0)||(i%400==0))//若中途有闰年出现,则再+1
{
ans++;
if (ans==7) ans=0;
}
}
//************************判断所求月份month1月1日的位置******************************
if (month==1) sum=ans;//如果要求1月份的日历,则开始空出的日期直接等于ans
else
{
for (i=1;i<=month-1;i++)//计算那年从1月到month-1个月有多少天
sum+=m[i];
sum=sum-(7-ans);
sum%=7;//sum最终得到的结果是所求月份1月1日前应当空出多少日
}
//----------------------------输出日历--------------------------------------------------
//输出日历的格式(星期)
cout<<setw(10)<<"SUN";
cout<<setw(10)<<"MON";
cout<<setw(10)<<"TUS";
cout<<setw(10)<<"WED";
cout<<setw(10)<<"THU";
cout<<setw(10)<<"FRI";
cout<<setw(10)<<"SAT"<<endl;
for (i=1;i<=71;i++)
cout<<'-';
cout<<endl;
for (k=1;k<=sum;k++)//输出空出的日子,用空格代替
{
cout<<setw(10)<<' ';
num++;
}
for (j=1;j<=m[month];j++)//输出日期
{
cout<<setw(10)<<j;
num++;
if (num==7)//满7天就换行
{
cout<<endl;
num=0;
}
}
cout<<endl<<endl<<"还要继续查询吗?(1:要~,0:不要~)";
cin>>flag;
ans=1;num=0;sum=0;//所有回归原始
}
return 0;
}