看一下这个用c语言编的万年历

2024-12-21 13:10:13
推荐回答(1个)
回答1:

/* 再次修改,满足要求,按ESC可退出,到12月自动退出,希望对你有帮助 */

#include
int leap (int year)
{if(year%4==0&&year%100!=0||year%400==0) /* 判断是否是闰年 */
return 1;
else return 0;
}
int days_month (int month,int year) /* 判断大月和小月 */
{
if(month==1||month==3||month==5||month==7||month==8||month==10||month==12) /* 找出大月 */
return 31;
if(month==4||month==6||month==9||month==11) /* 找出小月 */
return 30;
if(month==2&&leap(year)==1) return 29; /* 判断二月是29天还是28天 */
else return 28;
}
int firstday(int month,int year)
{int w;
w=(1+2*month+3*(month+1)/5+year+year/4+year/400-year/100)%7+1; /* 判断每个月开始的第一天是星期几 */
return w;
}
main()
{int i,j=1,k=1,a,b,month,year;
printf("\n input year and month:\n");
scanf("%d%d",&year,&month);

while(getch()!= 27&&month<=12) /* 按ESC退出*/
{j=1,k=1;
b=days_month(month,year);
a=firstday (month,year);
printf("%d-%d \n",year,month);
printf(" Sun Mon Tue Wed Thu Fri Sat \n"); /* 输出对应当月的日历 */
if(a==7)
{for(i=1;i<=b;i++)
{printf("%4d",i);
if(i%7==0)
{printf("\n");
} }}
if(a!=7)
{while (j<=4*a)
{printf(" ");
j++;}
for(i=1;i<=b;i++)
{printf("%4d",i);
if(i==7*k-a)
{printf("\n");
k++;
} }}
printf("\n");
month++;

/*getch(); */
}}