文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>迭代进化 -- 万年历(C语言实现 TR4.2)

迭代进化 -- 万年历(C语言实现 TR4.2)

时间:2010-09-01  来源:sohu2000000

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

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;
    }

  •  


Output:

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
相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载