因为痛,所以叫青春

我有一个梦想:穷屌丝变身富屌丝
根据所给的年月,打印该月份的日历表
View Code
#include<stdio.h>
int Is_Leap_Year(int year)

{                    
       if(year % 4 == 0)

       {
              if(year % 100 == 0)
              {
                     if(year % 400 == 0)
                            return 1;     
                     else
                           return 0;  
              }
              else
                     return 1;  
       }
       else
              return 0; 
}
int Get_Week(int year,int month,int day)
{
       int leap,week;
       int y;
       int mon1[12] = {0,3,3,6,1,4,0,3,5,0,3,5};//此处为闰年时的月基数
       int mon2[12] = {0,3,4,0,2,5,0,3,6,1,4,6};//此处为平年时的月基数
       //另外,平年时的年基数为1,闰年时的月基数为2;
       leap = Is_Leap_Year(year);
       if(leap)
       {
              y = year + year / 4 + year /400 - year / 100 - 2 + mon2[month - 1]  + day;
       }
       else 
       {
              y = year + year / 4 + year /400 - year / 100 - 1 + mon1[month - 1]  + day;
       }
       week = y % 7;
       return week;
}
int Day(int month)
{
    switch(month)
    {
        case 1:return 31;
        case 2:return 28;
        case 3:return 31;
        case 4:return 30;
        case 5:return 31;
        case 6:return 30;
        case 7:return 31;
        case 8:return 31;
        case 9:return 30;
        case 10:return 31;
        case 11:return 30;
        case 12:return 31;
    }
}
int main()
{
    int year,i,month,week,temp;
    scanf("%d%d",&year,&month);
    week = Get_Week(year,month,1);
    if(week == 0)
        temp = 7;
    else
        temp = week;
    printf("         %04d年日历\n",year);
    printf("         ==========        \n");
    printf("           %2d月\n",month);
    printf("--------------------------------------\n");
    printf("日    一    二    三    四    五    六\n");
    int day;
    day = Day(month);
    if(month == 2)
    {
        if(Is_Leap_Year(year))
            day += 1;
    }
    for(i = 0;i < temp; i++)
    {
        printf("  ");
        if(i)
        printf("    ");
    }
    for(i = 1;i <= day;i++)
    {
        if(temp != 0)
            printf("    ");
        printf("%-2d",i);
        if(temp == 6)
        {
            printf("\n");
            temp = 0;
        }
        else
            temp++;
    }
    printf("\n");
    printf("--------------------------------------\n");
    return 0;
}

posted on 2012-05-25 17:48  Nice!  阅读(405)  评论(0编辑  收藏  举报