Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2087571
  • 博文数量: 414
  • 博客积分: 10312
  • 博客等级: 上将
  • 技术积分: 4921
  • 用 户 组: 普通用户
  • 注册时间: 2007-10-31 01:49
文章分类

全部博文(414)

文章存档

2011年(1)

2010年(29)

2009年(82)

2008年(301)

2007年(1)

分类: C/C++

2010-09-01 23:47:12

论坛上的同学给出的建议:

QUOTE:
$ cal 9 1752

    September 1752
Su Mo Tu We Th Fr Sa
       1   2  ....
14 15 16 ....
17 18 19 20 21 22 23
...
jamesr 发表于 2010-08-30 08:39


应论坛上同学的建议,新的代码修改如下:

     

    #include <stdio.h>
    #include <ctype.h>

    #define SYEAR 1900
    #define LMonth 31
    #define SMonth 30
    #define VMonth 28

    typedef enum {
        Sun,
        Mon,
        Jue,
        Wed,
        Thu,
        Fri,
        Sat
    } WEEK;

    typedef enum {
        January = 1,
        February,
        Match,
        Apirl,
        May,
        June,
        July,
        August,
        September,
        October,
        November,
        December
    } MONTH;

    void
    printMonHead(void){
        printf(" Sun Mon Tue Wed Thu Fri Sat\n");
        return;
    }

    void
    printMonth(const WEEK firstDay, unsigned long length ){
        WEEK weekDay = firstDay % 7;
        unsigned long i;

        printMonHead();

        for (i=0; i<weekDay;++i)
            printf(" ");

        for(i=1;i<=length;++i) {
            weekDay = (++weekDay)%7;
            printf("%7d", i);

            if(weekDay==0) printf("\n");
        }

        printf("\n");

        return;
    }


    unsigned long
    isLeapYear(const unsigned long yr){
        return ( yr%( yr%100 ? 4:400) ? 0 : 1);
    }


    unsigned long
    getYearDay(const unsigned long year) {
        unsigned long lastYear = year-1;
        unsigned long yearNum = lastYear-1899;

        unsigned long walker,counter=0;
        for(walker=SYEAR; walker < year; ++walker) {
            if(isLeapYear(walker)==1)
                ++counter;
        }

        return (365*yearNum+counter+1)%7;
    }

    void
    printYear(const unsigned long y, const unsigned long m) {

        WEEK yDay = getYearDay(y);
        unsigned long vDay = isLeapYear(y);

        unsigned char* monName [13] = {0,
            "January", "February", "Match", "Apirl",
            "May", "June", "July", "August",
            "September","October", "November", "December"
        };

        unsigned long monLen [13];
        monLen[0]=0;
        monLen[January]=31; monLen[February]=28+vDay; monLen[Match]=31;
        monLen[Apirl]=30; monLen[May]=31; monLen[June]=30;
        monLen[July]=31; monLen[August]=31; monLen[September]=30;
        monLen[October]=31; monLen[November]=30; monLen[December]=31;

        unsigned long monWeeks[13];
        monWeeks[0]=0;
        monWeeks[January] = yDay;
        monWeeks[February] = ((monWeeks[January] +monLen[January] )%7);
        monWeeks[Match] = ((monWeeks[February] +monLen[February] )%7);
        monWeeks[Apirl] = ((monWeeks[Match] +monLen[Match] )%7);
        monWeeks[May] = ((monWeeks[Apirl] +monLen[Apirl] )%7);
        monWeeks[June] = ((monWeeks[May] +monLen[May] )%7);
        monWeeks[July] = ((monWeeks[June] +monLen[June] )%7);
        monWeeks[August] = ((monWeeks[July] +monLen[July] )%7);
        monWeeks[September] = ((monWeeks[August] +monLen[August] )%7);
        monWeeks[October] = ((monWeeks[September] +monLen[September] )%7);
        monWeeks[November] = ((monWeeks[October] +monLen[October] )%7);
        monWeeks[December] = ((monWeeks[November] +monLen[November] )%7);


        printf("\n-------------------------------------------------\n");
        printf(" %s, %d \n",monName[m],y);
        printf("-------------------------------------------------\n");
        printMonth(monWeeks[m],monLen[m]); printf("\n");

        return;
    }


    int
    main(void){
        int yr, mon , ok;

    loop:

        do
        {
            ok=1;

            fflush(stdin);

            printf("\nPlease Enter Year(0 to quit): ");
            scanf("%4d",&yr);

            if(yr ==0)
            {
                return 0;
            }

            if(yr < SYEAR)
            {
                printf("\nOut Of Range! Please Enter A Year Later Than %d",SYEAR);
                ok=0;
            }
        } while(!ok);


        do
        {
            ok=1;
            fflush(stdin);

            printf("\nPlease enter Month (0 to quit): ");
            scanf("%2d",&mon);

            if(mon ==0) return 0;

            if(mon<1 || mon >12)
            {
                printf("\nOut Of Range! Please Input Inside【1-12】");
                ok=0;
            }
        } while(!ok);

        printf("\n"); printYear(yr,mon);printf("\n\n");

        goto loop;

        system("PAUSE");
        return 0;
    }

  •  




Please Enter Year(0 to quit): 1982

Please enter Month (0 to quit): 11

-------------------------------------------------
                     November, 1982                        
-------------------------------------------------
    Sun    Mon    Tue    Wed    Thu    Fri    Sat
             1      2      3      4      5      6
      7      8      9     10     11     12     13
     14     15     16     17     18     19     20
     21     22     23     24     25     26     27
     28     29     30

Please Enter Year(0 to quit):
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/asiainfolf/archive/2010/09/01/5854645.aspx
阅读(1783) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2010-09-04 14:42:08

Download More than 1000 free IT eBooks: http://free-ebooks.appspot.com