编译运行都可以了,但为什么输入时候会中止??,
[localimg=400,217]1[/localimg]该程序作用是显示某年的全年的月历,输入年份在1900以后,为什么运行后会出现中止??如图:代码全部如下:
#include<stdio.h>
//#include<string.h>
/*
#define Sunday 0;
#define Monday 1;
#define Tuesday 2;
#define Wednesday 3;
#define Thursday 4;
#define Friday 5;
#define Saturday 6;
*/
void GiveInstructions(void);
int GetYearFromUser(void);
void PrintCalendar(int year);
void PrintCalendarMonth(int month, int year);
void IndentFirstLine(int weekday);
int MonthDays(int month, int year);
int FirstDayOfMonth(int month , int year);
//char MonthName(int month);
int IsLeapYear(int year);
/* main program begin from here */
void main()
{
int year;
GiveInstructions();
year=GetYearFromUser();
PrintCalendar(year);
}
void GiveInstructions(void)
{
printf("This program display a calendar for a full\n");
printf("year. The year must not be before 1900.\n");
}
int GetYearFromUser(void)
{
int year;
while(1)
{
printf("which year? ");
scanf("%d",&year);
if(year>=1900) return(year);
else printf("the year must be at least 1900!\n");
}
}
void PrintCalendar(int year)
{
int month;
for(month=1; month <=12; month++)
{
PrintCalendarMonth(month, year);
printf("\n");
}
}
void PrintCalendarMonth(int month, int year)
{
int weekday, nDay, day;
printf(" %s %d\n",month, year);
printf(" Su Mo Tu We Th Fr Sa\n");
nDay = MonthDays(month, year);
weekday = FirstDayOfMonth(month , year);
IndentFirstLine(weekday);
for(day = 1; day<=nDay; day++)
{
printf(" %2d",day);
if(weekday == 6)
printf("\n");
weekday = (weekday + 1) % 7;
}
if(weekday != 0)
printf("\n");
}
void IndentFirstLine(int weekday)
{
int i;
for(i = 0;i < weekday; i++)
{printf(" ");}
}
int MonthDays(int month, int year)
{
switch (month)
{
case 2: if (IsLeapYear(year)) return (29);
return (28);
case 4: case 6: case 9: case 11:
return(30);
default : return (31);
}
}
int FirstDayOfMonth(int month , int year)
{
int weekday, i;
weekday = 1;
for(i = 1900; i < year; i++)
{
weekday = (weekday + 365) % 7;
if(IsLeapYear(year)) weekday = (weekday + 1) % 7;
}
for(i = 1; i < month ; i++)
{
weekday = (weekday + MonthDays(i, year)) % 7;
}
return (weekday);
}
/*char MonthName(int month)
{
switch(month)
{
case 1: return ("January");
case 2: return ("February");
case 3: return ("March");
case 4: return ("April");
case 5: return ("June");
case 6: return ("July");
case 7: return ("August");
case 8: return ("September");
case 9: return ("October");
case 10: return ("November");
case 11: return ("November");
case 12: return ("December");
default: return ("Illegal month!");
}
}*/
int IsLeapYear(int year)
{
if ( ((year % 4 == 0) && (year % 100 !=0)) || (year % 400 == 0) )
return (1);
else return (0);
}
希望大家可以帮下我,这个问题已经困扰我很久了,我换了编译器也不行啊!!
----------------解决方案--------------------------------------------------------
void PrintCalendarMonth(int month, int year)
{
int weekday, nDay, day;
printf(" %s %d\n",month, year);//-------------->%d %d
printf(" Su Mo Tu We Th Fr Sa\n");
nDay = MonthDays(month, year);
weekday = FirstDayOfMonth(month , year);
IndentFirstLine(weekday);
for(day = 1; day<=nDay; day++)
{
printf(" %2d",day);
if(weekday == 6)
printf("\n");
weekday = (weekday + 1) % 7;
}
if(weekday != 0)
printf("\n");
}
输出结果好像不太对啊。
----------------解决方案--------------------------------------------------------
void PrintCalendarMonth(int month, int year)
{
int weekday, nDay, day;
printf(" %s %d\n",month, year);//-------------->%d %d
printf(" Su Mo Tu We Th Fr Sa\n");
nDay = MonthDays(month, year);
weekday = FirstDayOfMonth(month , year);
IndentFirstLine(weekday);
for(day = 1; day<=nDay; day++)
{
printf(" %2d",day);
if(weekday == 6)
printf("\n");
weekday = (weekday + 1) % 7;
}
if(weekday != 0)
printf("\n");
}
输出结果好像不太对啊。
----------------解决方案--------------------------------------------------------
“case” 为什么这样缩进?
----------------解决方案--------------------------------------------------------