满意答案
#include
//输出指定月份的名称以及星期表头
void prt_title(int month)
{
switch (month)
{
case 1:puts("January");break;
case 2:puts("February");break;
case 3:puts("March");break;
case 4:puts("April");break;
case 5:puts("May");break;
case 6:puts("June");break;
case 7:puts("July");break;
case 8:puts("August");break;
case 9:puts("September");break;
case 10:puts("October");break;
case 11:puts("November");break;
case 12:puts("December");break;
}
printf("Sun.\tMon.\tTues.\tWed.\tThur.\tFri.\tSat.\n");
}
//计算指定月的第一天是星期几
int first_day_month(int year, int month)
{
if (month<3)
{
month += 12;
year--;
}
return (1 + 2 * month + 3 * (month + 1) / 5 + year + year / 4 - year / 100 + year / 400) % 7;
}
//计算指定月有几天
int days_of_month(int year, int month)
{
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:return ((year % 4 == 0 && year % 100) || (year % 400 == 0)) ? 29 : 28;break;
}
}
//输出指定月的日历
void prt_the_month(int year, int month)
{
int fd, i, sp = 0, days;
putchar('\n');
prt_title(month);
fd = first_day_month(year, month);
for (i = 0;i
{
putchar('\t');
sp++;
}
days = days_of_month(year, month);
for (i = 1;i <= days;sp++, i++)
{
if (sp&&sp % 7 == 0) putchar('\n');
printf("%2d\t", i);
}
putchar('\n');
}
//输出指定年的日历
void prt_the_year(int year)
{
int i;
printf("Calendar of %d : \n", year);
for (i = 1;i <= 12;i++)
{
prt_the_month(year, i);
}
}
//主函数,用2012年进行测试
int main(void)
{
prt_the_year(2012);
getchar();
return 0;
}
谢谢采纳
00分享举报