include<stdio.h>
int myweek(int year,int mouth)//计算输入的年月的第一天是周几
{
int alldays=0;
int mouthArr[]={0,31,0,31,30,31,30,31,31,30,31,30,31};
if(year<1990||mouth>12||mouth<1){
printf("您输入的信息有误,请参看相关文献输入");
}else{
int i;
for(i=1990;i<year;i++){
if((i%4==0&&i%100!=0)||(i%400==0)){
alldays+=366;
}else{
alldays+=365;
}
}
if((year%4==0&&year%100!=0)||(year%400==0)){
mouthArr[2]=29;
}else{
mouthArr[2]=28;
}
for(i=0;i<mouth;i++){
alldays+=mouthArr[i];
}
int value=alldays%7;
int week=(value+1)%7;
return week;
}
}
void calender(int year,int mouth,int week)//打印万年历
{
int mouthArr[]={0,31,0,31,30,31,30,31,31,30,31,30,31};
if((year%4==0&&year%100!=0)||(year%400==0)){
mouthArr[2]=29;
}else{
mouthArr[2]=28;
}
printf("日\t一\t二\t三\t四\t五\t六\n");
int count=0;
int i;
for(i=0;i<week;i++){
printf(" \t");
count++;
}
for(i=1;i<=mouthArr[mouth];i++){
count++;
printf("%d\t",i);
if(count%7==0){
printf("\n");
}
}
if(count%7!=0){
printf("\n");
}
}
int main()
{
int year,mouth;
printf("请输入要查询的年份:\n");
scanf("%d",&year);
printf("请输入要查询的年份:\n");
scanf("%d",&mouth);
int week=myweek(year,mouth);//计算输入的年月的第一天是周几
printf("%d年%d月的第一天是星期%d\n",year,mouth,week);
calender(year,mouth,week);//根据输入的年月和周几,打印万年历
return 0;
}