我用C制作了12个月的日历。这个节目显示特定年份的12个月日历。该程序提示用户打印年份,并计算出(a)年份是否为闰年,以及(b)所选年份的哪一天开始。
说明:
•main()提示用户输入,调用您自己设计的函数来确定输入年份的开始日期。然后它调用函数printCalendar()来实际打印十二个月的日历。
•printCalendar()接受两个参数,即年份编号和开始日期。然后循环遍历年份并调用函数printMonth()12次,每月一次。
•printMonth()接受三个参数,即年份编号,月份编号和特定月份的开始日期,它返回下个月开始的日期编号。打印月必须首先调用函数printMonthName(),然后以日历格式打印出该月的日期。
•printMonthName()将年份编号和月份编号作为参数,打印出标识月份的行,并在考虑闰年的情况下返回该月份的天数。
我遵循了这些指示并走到了尽头:
#include <stdio.h>
#include <stdlib.h>
int daysInMonth;
int getDayCode(int year);
void printCalendar(int year, int dayCode);
int getYear(void);
int getYear(void){
int year;
printf("Please enter a year: ");
scanf("%i", &year);
printf("\n");
return year;
}
int getDayCode(int year){
int dayCode;
int x1, x2, x3;
x1 = (year - 1.)/ 4.0;
x2 = (year - 1.)/ 100.;
x3 = (year - 1.)/ 400.;
dayCode = (year + x1 - x2 + x3) %7;
return dayCode;
}
main(){
int year, dayCode;
year = getYear();
dayCode = getDayCode(year);
printCalendar(year, dayCode);
}
void printCalendar(int year, int dayCode){
int month;
printf(" %d Monthly Calendar\n", year);
printf(" \n");
printf(" \n");
for (month = 1; month <= 12; month++){
printMonth(year, month, dayCode);
}
}
int printMonthName(int year, int month){
switch (month){
case 1:
printf("\n\nJanuary %i", year);
daysInMonth = 31;
return daysInMonth;
break;
case 2:
printf("\n\nFebruary %i", year);
if (year%4 == 0 && year%100 != 0 || year%400 == 0){
//printf("This is a leap year.\n");
daysInMonth = 29;
return daysInMonth;
}
else{
//printf("This is not a leap year.\n");
daysInMonth = 28;
return daysInMonth;
}
break;
case 3:
printf("\n\nMarch %i", year);
daysInMonth = 31;
return daysInMonth;
break;
case 4:
printf("\n\nApril %i", year);
daysInMonth = 30;
return daysInMonth;
break;
case 5:
printf("\n\nMay %i", year);
daysInMonth = 31;
return daysInMonth;
break;
case 6:
printf("\n\nJune %i", year);
daysInMonth = 30;
return daysInMonth;
break;
case 7:
printf("\n\nJuly %i", year);
daysInMonth = 31;
return daysInMonth;
break;
case 8:
printf("\n\nAugust %i", year);
daysInMonth = 31;
return daysInMonth;
break;
case 9:
printf("\n\nSeptember %i", year);
daysInMonth = 30;
return daysInMonth;
break;
case 10:
printf("\n\nOctober %i", year);
daysInMonth = 31;
return daysInMonth;
break;
case 11:
printf("\n\nNovember %i", year);
daysInMonth = 30;
return daysInMonth;
break;
case 12:
printf("\n\nDecember %i", year);
daysInMonth = 31;
return daysInMonth;
break;
default:
printf("Invalid input! Please try again!\n");
break;
}
}
int printMonth(int year, int month, int dayCode){
int day;
printMonthName(year, month);
printf("\n\nSun Mon Tue Wed Thu Fri Sat\n" );
/* advance printer to correct position for first date */
for (day = 1; day <= 1 + dayCode * 5; day++)
printf(" ");
/* print the dates for one month */
for (day = 1; day <= daysInMonth; day++){
printf("%2d", day);
if ((day + dayCode) % 7 > 0) /* before Sat? */
/* move to next day in same week */
printf(" ");
else /* skip to next line to start with Sun */
printf("\n ");
}
/* set day_code for next month to begin */
dayCode = (dayCode + daysInMonth % 7);
return dayCode;
}
我把2013年作为输入年份,我的代码从星期二的1月1日开始,这是正确的。但它也是在周二开始的所有12个月,这是问题所在: Calendar Output 2013
请帮忙。我现在一直试图让这个工作很长时间。
答案 0 :(得分:0)
您好像在dayCode
函数中返回printMonth
。但是,您在printCalendar
函数中从不对该值执行任何操作。通过更改printCalendar
中的for循环可以轻松解决此问题。
for (month = 1; month <= 12; month++){
dayCode = printMonth(year, month, dayCode);
}