#include
/*得到所输入年月的第一天是星期几0~6*/
int getdate(int y,int m);
/*判断所输入的是否是闰月,是则返回1,否则返回0*/
int leap(int y);
/*打印输入月份月历表*/
void print(int y,int m);
int main()
{
int y,m;
printf("输入年份和月份:");
scanf("%d%d",&y,&m);
print(y,m);
return 0;
}
int getdate(int y,int m)
{
int w=(y+(y-1)/4-(y-1)/100+(y-1)/400)%7;
int days=0;
switch(m)
{
case 12: days+=30;
case 11: days+=31;
case 10: days+=30;
case 9: days+=31;
case 8: days+=31;
case 7: days+=30;
case 6: days+=31;
case 5: days+=30;
case 4: days+=31;
case 3: if(leap(y)) days+=29;
else days+=28;
case 2: days+=31;
case 1: days+=0;
}
w=(w+days)%7;
return w; /*返回输入月份1号的星期*/
}
void print(int y,int m)
{
int w=getdate(y,m);
printf("%d年%d月的月历表:\n\n",y,m);
printf("\n==========================="
"========================\n\n");
int month[12]={31,28,31,30,31,30,31,31,30,31,30,31};
if(leap(y)) month[1]=29;
printf("SUN\tMON\tTUE\tWED\tTHU\tFRI\tSAT\n");
int i,j;
for(i=0;i for(i=w,j=1;j<=month[m-1];i++,j++)
{
if(i%7==0) printf("\n");
printf("%d\t",j);
}
printf("\n\n========================"
"===========================\n\n");
}
int leap(int y)
{
if((y%4==0&&y%100!=0)||y%400==0) return 1;
return 0;
}