# 万年历课程设计

#include<stdio.h>
long int f(int year,int month)
{/*f(年，月)=年-1，如果<3；否则，f（年,月）=年*/
if(month<3) return year-1;
else return year;
}
long int g(int month)
{/*g(月)=月+13；否则，g（月）=月+1*/
if(month<3) return month+13;
else return month+1;
}
long int n(int year,int month,int day)
{
/*N=1461*f(年，月)/4+153*g（月）/5+日*/
return 1461L*f(year,month)/4+153L*g(month)/5+day;
}
int w(int year,int month,int day)
{
/*w=(N-621049)%7(0<=w<7)*/
return(int)((n(year,month,day)%7-621049L%7+7)%7);
}
int date[12][6][7];
int day_tbl[][12]={{31,28,31,30,31,30,31,31,30,31,30,31},
{31,29,31,30,31,30,31,31,30,31,30,31}};
void main()
{
int sw,leap,i,j,k,wd,day;
int year;/*年*/
char title[]="SUN MON TUE WED THU FRI SAT";
printf("please input the year whose calendar you want to know: ");
scanf("%d*c",&year);
sw=w(year,1,1);
leap=year%4==0&&year%100||year%400==0;
for(i=0;i<12;i++)
for(j=0;j<6;j++)
for(k=0;k<7;k++)
date
[j][k]=0;
for(i=0;i<12;i++)
for(wd=0,day=1;day<=day_tbl[leap]
;day++)
{/*将第i+1月的日期填入日期列表*/
date
[wd][sw]=day;
sw=++sw%7;
if(sw==0) wd++;
}
printf("\n|======================the calendar of year &d=====================|\n|",year);
for(i=0;i<6;i++)
{/*先测算第i+1月和第i+7月的最大星期数*/
for(wd=0,k=0;k<7;k++)
wd+=date
[5][k]+date[i+6][5][k];
wd=wd?6:5;
printf("%2d  %s  %2d  %s |\n|",i+1,title,i+7,title);
for(j=0;j<wd;j++)
{ printf("   ");
for(k=0;k<7;k++)
if(date
[j][k])
printf("%4d",date
[j][k]);
else printf("    ");
printf("      ");
for(k=0;k<7;k++)
if(date[i+6][j][k])
printf("%4d",date[i+6][j][k]);
else printf("    ");
printf(" |\n| ");
}
}
puts("==================================================================");
puts("\n press any key to quit...");
getch();
}