输入年月,输出月份表
问题描述:根据给定的日期(格式为yyyy.mm.dd)显示该日是星期几,根据输入的月份(格式为yyyy.mm)以日历的形式显示本月各日所对应的星期,要考虑日期以及程序命令行参数的方式输入。
#include<stdio.h>
void printweek(int week);
int test_year(int yyyy);
void printmonth(int week,int mm,int test);
int main(void)
{
int yyyy,mm,dd,week,choice,test,sum_days=0,days,i;
int sum_mdays[11]={31,59,90,120,151,181,212,243,273,304,334};
printf("1.根据给定的日期(格式为yyyy.mm.dd)显示该日是星期几;\n2.根据输入的月份(格式为yyyy.mm)以日历的形式显示本月各日所对应的星期.\n请选择1或2:");
input:
scanf(" %i",&choice);
if((choice!=1)&&(choice!=2))
{
printf("输入错误!请输入1或2.");
goto input;
}
puts("请输入年份YYYY:");
scanf(" %i",&yyyy);
puts("请输入月份MM:");
scanf(" %i",&mm);
if(choice==1)
{
puts("请输入号数DD:");
scanf(" %i",&dd);
}
else{dd=1;}
for(i=1;i<yyyy;i++)
{
test=0;
test=test_year(i);
if(test==1){days=366;}
else{days=365;}
sum_days+=days;
if(sum_days>700){sum_days-=700;}
}
sum_days+=sum_mdays[mm-2];
sum_days+=dd;
week=sum_days%7;
if(choice==1)
{
printf("%i-%i-%i:",yyyy,mm,dd);
printweek(week);
}
test=test_year(yyyy);
if(choice==2)
{printmonth(week,mm,test);}
return 0;
}
void printweek(int week)
{
switch (week)
{
case 0:printf("Sunday\n");break;
case 1:printf("Monday\n");break;
case 2:printf("Tuesday\n");break;
case 3:printf("Wednesday\n");break;
case 4:printf("Thursday\n");break;
case 5:printf("Friday\n");break;
case 6:printf("Saturday\n");break;
default:printf("error!!!\n");
}
}
int test_year(int yyyy)
{
int yes=0;
if(((yyyy%100!=0)&&(yyyy%4==0))||(yyyy%400==0))
{yes=1;}
return yes;
}
void printmonth(int week,int mm,int test)
{
int i,mdays[12]={31,28,31,30,31,30,31,31,30,31,30,31};
if(test==1)
{mdays[1]=29;}
printf("%s %s %s %s %s %s %s\n","Sun","Mon","Tue","Wed","Thu","Fri","Sat");
for(i=0;i<week;i++)
{
printf("%s "," ");
}
for (i=1;i<=mdays[mm-1];i++)
{
if((i==(8-week))||(i==(15-week))||(i==(22-week))||(i==(29-week))||(i==(36-week)))
{printf("\n");}
printf("%3d ",i);
}
printf("\n");
}