制作日历:
输入年份和月份,输出该年该月的日历表
代码:
#include <stdio.h>
#include <conio.h>
#include <string.h>
int main()
{
int year,month,d1,week,i;
int m1=31,m2=28,m3=31,m4=30,m5=31,m6=30,m7=31,m8=31;
int m9=30,m10=31,m11=30,m12=31; //每个月份的天数
agn:printf("请输入年份和月份:\n");
scanf("%d%d",&year,&month);
if(year<=0||month<1||month>12)
{
printf("error!输入错误,请重新输入:\n");
goto agn;
}
m2=28+(year%4==0)-(year%100==0)+(year%400==0); //判断闰年:能被4整除,但不能被100整除,或者是能被400整除
year--;
d1=year+year/4-year/100+year/400+1; //计算一个月有多少天
for(i=1;i<month;i++)
switch(1)
{
case 1 :d1=d1+m1;break;
case 2 :d1=d1+m2;break;
case 3 :d1=d1+m3;break;
case 4 :d1=d1+m4;break;
case 5 :d1=d1+m5;break;
case 6 :d1=d1+m6;break;
case 7 :d1=d1+m7;break;
case 8 :d1=d1+m8;break;
case 9 :d1=d1+m9;break;
case 10 :d1=d1+m10;break;
case 11 :d1=d1+m11;break;
case 12 :d1=d1+m12;break;
}
week=d1%7;
printf("%d year %d month :\n",year+1,month);
for(i=0;i<week;i++)
printf(" ");
if(month==2)
{
for(i=week;i<week+m2;i++)
{
printf("%5d",i-week+1);
if(i%7==6)
printf("\n");
}
}
if(month==4||month==6||month==9||month==11)
{
for(i=week;i<week+30;i++)
{
printf("%5d",i-week+1);
if(i%7==6)
printf("\n");
}
}
if(month==1||month==3||month==5||month==7||month==8||month==10||month==12)
{
for(i=week;i<week+31;i++)
{
printf("%5d",i-week+1);
if(i%7==6)
printf("\n");
}
}printf("\n");
return 0;
}