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

全部博文(414)

文章存档

2011年(1)

2010年(29)

2009年(82)

2008年(301)

2007年(1)

分类: C/C++

2010-08-29 13:19:49

        经过近这几天的努力,终于写完了这个"万年历"的程序了,共享一下, 特别是同"勉"共享

[需求]

    请打印出任意年份的日历

[代码]

#include <stdio.h>

#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 Jue Wed Thu Fri Sat\n");
}

void printMonth(const WEEK firstDay, int length )
{
WEEK weekDay
= firstDay % 7;

int 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");
}


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


int getYearDay(const int year)
{

int lastYear = year-1;
int yearNum = lastYear-1899;

int walker,counter=0;
for(walker=1900; walker < year; ++walker)
{
if(isLeapYear(walker)==1)
++counter;
}

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


void printYear(const int y)
{
WEEK yDay
= getYearDay(y);
int vDay = isLeapYear(y);

int monWeeks[13];
int 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;

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

int i;
for(i=January; i<=December; ++i)
{
printf(
"\n-------------------------------------------------\n");
printf(
" %d, %d \n",y,i);
printf(
"-------------------------------------------------\n");
printMonth(monWeeks[i],monLen[i]);
printf(
"\n");
}

return;
}


int main(void)
{

int y;
int quit=0;

do
{
printf(
"Please enter which Year to Print For You (0 to quit): ");
scanf(
"%d",&y);
printf(
"\n");

if(y==0)
quit
= 1;
else
printYear(y);

}
while(!quit);

return 0;
}

输出

Please enter which Year to Print For You (0 to quit): 1973

 

 

 

-------------------------------------------------

                     1946, 1                     

-------------------------------------------------

    Sun    Mon    Jue    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     31

 

 

-------------------------------------------------

                     1946, 2                     

-------------------------------------------------

    Sun    Mon    Jue    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

 

 

-------------------------------------------------

                     1946, 3                     

-------------------------------------------------

    Sun    Mon    Jue    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

     31

 

 

-------------------------------------------------

                     1946, 4                     

-------------------------------------------------

    Sun    Mon    Jue    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

 

 

-------------------------------------------------

                     1946, 5                     

-------------------------------------------------

    Sun    Mon    Jue    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     31

 

 

-------------------------------------------------

                     1946, 6                     

-------------------------------------------------

    Sun    Mon    Jue    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

 

 

-------------------------------------------------

                     1946, 7                     

-------------------------------------------------

    Sun    Mon    Jue    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     31

 

 

-------------------------------------------------

                     1946, 8                     

-------------------------------------------------

    Sun    Mon    Jue    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     31

 

 

 

-------------------------------------------------

                     1946, 9                     

-------------------------------------------------

    Sun    Mon    Jue    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

 

 

-------------------------------------------------

                     1946, 10                     

-------------------------------------------------

    Sun    Mon    Jue    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     31

 

 

-------------------------------------------------

                     1946, 11                     

-------------------------------------------------

    Sun    Mon    Jue    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

 

 

 

-------------------------------------------------

                     1946, 12                     

-------------------------------------------------

    Sun    Mon    Jue    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     31

 

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/asiainfolf/archive/2010/08/29/5847101.aspx

阅读(965) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~