我試圖打印出特定年份和月份的日曆,但每個月都會收到相同的日曆。我試圖添加一個聲明,以查看年份是否爲閏年以增加天數,但它沒有區別。我是新來的c。請幫助任何建議。提前致謝。特定年份和月份的打印日曆
void print_month_calendar(int year, int month)
{
int day;
int daycode = ddaycode(year);
int days_in_month[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
char *months[]=
{
" ",
"\n\n\nJanuary",
"\n\n\nFebruary",
"\n\n\nMarch",
"\n\n\nApril",
"\n\n\nMay",
"\n\n\nJune",
"\n\n\nJuly",
"\n\n\nAugust",
"\n\n\nSeptember",
"\n\n\nOctober",
"\n\n\nNovember",
"\n\n\nDecember"
};
printf("%s", months[month]);
printf("\n\nSun Mon Tue Wed Thu Fri Sat\n");
if((year%4==0 && year%100 !=0) || year%400==0)
days_in_month[2] = 29;
for (day = 1; day <= 1 + daycode * 5; day++)
{
printf(" ");
}
//Print all the dates for the month
for (day = 1; day <= days_in_month[month]; day++)
{
printf("%2d", day);
if ((day + daycode) % 7 > 0)
printf(" ");
else
printf("\n ");
}
}
int ddaycode(int year)
{
int daycode;
int d1, d2, d3;
d1 = (year - 1.)/ 4.0;
d2 = (year - 1.)/ 100.;
d3 = (year - 1.)/ 400.;
daycode = (year + d1 - d2 + d3) %7;
return daycode;
}
你daycode計算沒有考慮月份考慮在內。 – Cimbali