用C语言编写万年历

2024-11-26 11:59:09
推荐回答(1个)
回答1:

#include
#include
int day_s();
int year_s();
int week_s();
void output_month();
void menu();
void output_year();
int year,month;
int main()
{
printf("请输入年月以打印该月日历!\n");
printf("年:");
scanf("%d",&year);
printf("月:");
scanf("%d",&month);
system("cls");
printf("\t\t公元%d年\n",year);
output_month();
menu();
return 0;
}
int day_s()
{
switch(month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:return 31;break;
case 4:
case 6:
case 9:
case 11:return 30;break;
case 2:
{
if(year%4==0&&year%100!=0||year%400==0) return 29;
else return 28;
break;
}
}
}
int year_s()
{
int s=1;
switch(month-1)
{
case 11:s+=30;
case 10:s+=31;
case 9:s+=30;
case 8:s+=31;
case 7:s+=31;
case 6:s+=30;
case 5:s+=31;
case 4:s+=30;
case 3:s+=31;
case 2:
{
if(year%4==0&&year%100!=0||year%400==0)s+=29;
else s+=28;
}
case 1:s+=31;
}
return s;
}
int week_s()
{
int s=1,n;
n=(year-1)%400;
for(int i=1;i<=n;i++)
{
if(year%4==0&&year%100!=0||year%400==0)
s+=2;
else s+=1;
}
s+=year_s();
s%=7;
return s;
}
void output_month()
{
int week,day,i;
printf("\t\t公元%d月\n",month);
printf("星期日\t星期一\t星期二\t星期三\t星期四\t星期五\t星期六\n");
day=day_s();
week=week_s();
for(i=1;i<=week;i++)
{
printf(" \t");
}
for(i=1;i<=day;i++)
{
printf("%d",i);
if((i+week)%7==0) printf("\n");
else printf("\t");
}
for(int j=1;j<=42-week-i;j++)
{
printf("\t");
}
printf("\n\n");
}
void output_year()
{
printf("\t\t公元%d年\n",year);
for(int i=1;i<=12;i++)
{
month=i;
output_month();
}
printf("1查看下年\n");
printf("2查看上年\n");
printf("3重新输入年月\n");
printf("0退出\n");
printf("请选择:");
int n;
scanf("%d",&n);
system("cls");
switch(n)
{
case 1:year+=1;output_year();break;
case 2:year-=1;output_year();break;
case 3:main();break;
case 0:;break;
}
}
void menu()
{
printf("1查看下个月\n");
printf("2查看上个月\n");
printf("3查看本年全部日历\n");
printf("4重新输入年月\n");
printf("0退出\n");
printf("请选择:");
int n;
scanf("%d",&n);
system("cls");
switch(n)
{
case 1:
{
if(month+1==13)
{
year+=1;
month=1;
}
else month+=1;
printf("\t\t公元%d年\n",year);
output_month();
menu();
break;
}
case 2:
{
if(month-1==0)
{
year-=1;month=12;
}
else
{
month-=1;
printf("\t\t公元%d年\n",year);
output_month();
menu();
}
break;
}
case 3:output_year();break;
case 4:main();break;
case 0:break;
}
}