OK,以下是输出和LZ的程序完全相同的程序……
程序代码:
#include <stdio.h>
#include <ctype.h>
int is_leap_year(int y)
{
return y != 0 && y % 4 == 0 && (y % 100 != 0 || y % 400 == 0);
}
int get_week_of_date(int y, int m, int d)
{
static int t[] = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4};
y -= m < 3;
return (y + y / 4 - y / 100 + y / 400 + t[m-1] + d) % 7;
}
int get_month_days(int y, int m)
{
if (m == 2)
return 28 + is_leap_year(y);
return 30 + ((m <= 7) == (m % 2));
}
void print_calendar(int y, int m)
{
int week = get_week_of_date(y, m, 1), day;
int month_day = get_month_days(y, m);
puts(" Sun Mon Tue Wed Thu Fri Sat");
if (week != 0)
printf("%*c", week * 5, ' ');
for (day = 1; day <= month_day; day++)
{
printf("%5d", day);
if (++week % 7 == 0)
putchar('\n');
}
if (week % 7 != 0)
putchar('\n');
}
int main(void)
{
int year, month;
puts("Input the year and month like \"2008-10\"");
while (putchar(':'), scanf("%d-%d", &year, &month) == 2)
{
if (year >= 0 && month >= 1 && month <= 12)
print_calendar(year, month);
else
puts("date error");
}
return 0;
}
[[it] 本帖最后由 风居住的街道 于 2008-11-3 11:55 编辑 [/it]]