万年历问题:2月份总是28天
#include <stdio.h>
#include <stdlib.h>
#define Sun 0
#define Mon 1
#define Tue 2
#define Wed 3
#define Thur 4
#define Fri 5
#define Sat 6
char month[12][4]={"Jan","Feb","Mar","Apl","May","Jun","July","Aug","Sept","Oct","Nov","Dec"};
void GiveInstructions() /*prints out instructions to the user*/
{
printf("This program displays a calendar for a full\nyear. the year must not before 1900.\nWhich year?");
}
int GetYearFromUser() /*reads in a year from the user*/
{
int year;
loop:
scanf("%d",&year);
if (year<1900)
{
printf("the year must be at least 1900.\nWhich year?");
goto loop;
}
return year;
}
int IsLeapYear(int year) /*decide if the year input is leap year or not*/
{
if(year%4==0&&year%100==0||year%400==0)
return 1;
else
return 0;
}
int weekday(int year) /*work out the weekday*/
{
int day;
day=(year+(year-1)/4-(year-1)/100+(year-1)/400)%7;
return (day);
}
int MonthDay(int i) /*get the days of all months*/
{
int year;
switch (i)
{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:
if(IsLeapYear(year)==1)
return 29;
else if(IsLeapYear(year)==0)
return 28;
break;}
return 0;
}
int PrintCalendar(int year) /*printf the calendar*/
{
int i,j,k,firstDay;
firstDay=weekday(year);
for (i=0;i<=11;i++)
{
printf("\t\t%s\n",month[i]);
printf("Sun Mon Tue Wed Thu Fri Sat\n");
for(j=0;j<firstDay;j++)
{
printf(" "); /*make the first day align at week*/
}
for (k=1;k<=MonthDay(i+1);k++)
{
if(k<10)
printf("%d ",k);
else
printf("%d ",k);
if((k+j)%7==0)
printf("\n");
}
if((MonthDay(i+1)%7+firstDay)%7==0) /*define the first day for next loop*/
firstDay=0;
else
{firstDay=MonthDay(i+1)%7+firstDay;
if(firstDay>7)
firstDay=firstDay-7;
printf("\n\n");}
}
return 0;
}
int main()
{
int year;
GiveInstructions(); /*prints out instructions to the user*/
year = GetYearFromUser(); /*reads in a year from the user*/
PrintCalendar(year); /* prints a calendar for an entire year*/
return 0;
}
请高手赐教