迭代进化 -- 日历(C语言实现 TR4.2)
论坛上的同学给出的建议:
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
应论坛上同学的建议,新的代码修改如下:
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
1 #include <stdio.h>
2 #include <ctype.h>
3
4 #define SYEAR 1900
5 #define LMonth 31
6 #define SMonth 30
7 #define VMonth 28
8
9 typedef enum {
10 Sun,
11 Mon,
12 Jue,
13 Wed,
14 Thu,
15 Fri,
16 Sat
17 } WEEK;
18
19 typedef enum {
20 January = 1,
21 February,
22 Match,
23 Apirl,
24 May,
25 June,
26 July,
27 August,
28 September,
29 October,
30 November,
31 December
32 } MONTH;
33
34 void
35 printMonHead(void){
36 printf(" Sun Mon Tue Wed Thu Fri Sat\n");
37 return;
38 }
39
40 void
41 printMonth(const WEEK firstDay, unsigned long length ){
42 WEEK weekDay = firstDay % 7;
43 unsigned long i;
44
45 printMonHead();
46
47 for (i=0; i<weekDay;++i)
48 printf(" ");
49
50 for(i=1;i<=length;++i) {
51 weekDay = (++weekDay)%7;
52 printf("%7d", i);
53
54 if(weekDay==0) printf("\n");
55 }
56
57 printf("\n");
58
59 return;
60 }
61
62
63 unsigned long
64 isLeapYear(const unsigned long yr){
65 return ( yr%( yr%100 ? 4:400) ? 0 : 1);
66 }
67
68
69 unsigned long
70 getYearDay(const unsigned long year) {
71 unsigned long lastYear = year-1;
72 unsigned long yearNum = lastYear-1899;
73
74 unsigned long walker,counter=0;
75 for(walker=SYEAR; walker < year; ++walker) {
76 if(isLeapYear(walker)==1)
77 ++counter;
78 }
79
80 return (365*yearNum+counter+1)%7;
81 }
82
83 void
84 printYear(const unsigned long y, const unsigned long m) {
85
86 WEEK yDay = getYearDay(y);
87 unsigned long vDay = isLeapYear(y);
88
89 unsigned char* monName [13] = {0,
90 "January", "February", "Match", "Apirl",
91 "May", "June", "July", "August",
92 "September","October", "November", "December"
93 };
94
95 unsigned long monLen [13];
96 monLen[0]=0;
97 monLen[January]=31; monLen[February]=28+vDay; monLen[Match]=31;
98 monLen[Apirl]=30; monLen[May]=31; monLen[June]=30;
99 monLen[July]=31; monLen[August]=31; monLen[September]=30;
100 monLen[October]=31; monLen[November]=30; monLen[December]=31;
101
102 unsigned long monWeeks[13];
103 monWeeks[0]=0;
104 monWeeks[January] = yDay;
105 monWeeks[February] = ((monWeeks[January] +monLen[January] )%7);
106 monWeeks[Match] = ((monWeeks[February] +monLen[February] )%7);
107 monWeeks[Apirl] = ((monWeeks[Match] +monLen[Match] )%7);
108 monWeeks[May] = ((monWeeks[Apirl] +monLen[Apirl] )%7);
109 monWeeks[June] = ((monWeeks[May] +monLen[May] )%7);
110 monWeeks[July] = ((monWeeks[June] +monLen[June] )%7);
111 monWeeks[August] = ((monWeeks[July] +monLen[July] )%7);
112 monWeeks[September] = ((monWeeks[August] +monLen[August] )%7);
113 monWeeks[October] = ((monWeeks[September] +monLen[September] )%7);
114 monWeeks[November] = ((monWeeks[October] +monLen[October] )%7);
115 monWeeks[December] = ((monWeeks[November] +monLen[November] )%7);
116
117
118 printf("\n-------------------------------------------------\n");
119 printf(" %s, %d \n",monName[m],y);
120 printf("-------------------------------------------------\n");
121 printMonth(monWeeks[m],monLen[m]); printf("\n");
122
123 return;
124 }
125
126
127 int
128 main(void){
129 int yr, mon , ok;
130
131 loop:
132
133 do
134 {
135 ok=1;
136
137 fflush(stdin);
138
139 printf("\nPlease Enter Year(0 to quit): ");
140 scanf("%4d",&yr);
141
142 if(yr ==0)
143 {
144 return 0;
145 }
146
147 if(yr < SYEAR)
148 {
149 printf("\nOut Of Range! Please Enter A Year Later Than %d",SYEAR);
150 ok=0;
151 }
152 } while(!ok);
153
154
155 do
156 {
157 ok=1;
158 fflush(stdin);
159
160 printf("\nPlease enter Month (0 to quit): ");
161 scanf("%2d",&mon);
162
163 if(mon ==0) return 0;
164
165 if(mon<1 || mon >12)
166 {
167 printf("\nOut Of Range! Please Input Inside?1-12?");
168 ok=0;
169 }
170 } while(!ok);
171
172 printf("\n"); printYear(yr,mon);printf("\n\n");
173
174 goto loop;
175
176 system("PAUSE");
177 return 0;
178 }
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
posted on 2010-09-02 00:15 sohu2000000 阅读(313) 评论(0) 编辑 收藏 举报