注册 登录
编程论坛 C++教室

万年历问题:2月份总是28天

klpk521 发布于 2011-11-28 20:53, 739 次点击

#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;
}
            
请高手赐教
5 回复
#2
yuccn2011-11-29 10:10
int IsLeapYear(int year)  /*decide if the year input is leap year or not*/
{
    if ((year % 400 == 0) || ((year % 4) == 0) && (year % 100 != 0)) {
        return 1;
    }

    return 0;
}
#3
lucky5635912011-11-29 12:55
闰年判断错误。
#4
kuangdang1782011-11-29 23:09
貌似那数组定义的也有错!@七月跟哪个月的超过宽度了!~
#5
klpk5212011-11-30 00:13
回复 2楼 yuccn
thanX

[ 本帖最后由 klpk521 于 2011-11-30 00:14 编辑 ]
#6
我是菜鸟C2011-11-30 09:19
两个错误:
1,month数组定义错误,有益处,因为你的七月和9月是占5个单位长度的,所以month应改为char month[12][5];
2,闰年判断错误。闰年的判断标准是能被4整除且不能被100整除,或者能被400整除!所以应改为
        (yeah%4==0&&yeah%100!=0)||(yeah%400==0);
1