万年历

来源:转载

        1 /* 2 @author zengweilin 
3 @2011/6/13 万年历
4 */
5 #include "stdio.h"
6 #include "stdlib.h"
7 int isLeapYear(int);
8 int main(){
9 /* i 月份前的空格数目,
10 year 用户输入的年份,
11 day 当前月份的天数,
12 year_start 循环小于year年份的变量,
13 per_year_day 年天数(区分闰年与非闰年),
14 */
15 int i,year,day=1,year_start=0,per_year_day=0;
16 /* temp_1 月份循环变量,
17 temp_2 月份前空格的循环变量
18 */
19 int temp_1=0,temp_2;
20 /* year_days 统计总天数,
21 month_day 月天数
22 */
23 long year_days=0;
24 int month_day[]={31,28,31,30,31,30,31,31,30,31,30,31};
25
26 printf("please input the current year: ");
27 scanf("%d",&year);
28
29 /* 计算输入年份之前的天数*/
30 while(year_start<year)
31 {
32 if(isLeapYear(year_start))
33 {
34 per_year_day=366;
35 }
36 else
37 {
38 per_year_day=365;
39 }
40 year_days+=per_year_day;
41 year_start++;
42 }
43 /* 列出十二个月份的日期*/
44 for(;temp_1<12;temp_1++)
45 {
46 switch(temp_1)
47 {
48 case 0:printf("/tJanuary<%d>/n",year);break;
49 case 1:printf("/tFebruary<%d>/n",year);break;
50 case 2:printf("/tMarch<%d>/n",year);break;
51 case 3:printf("/tApril<%d>/n",year);break;
52 case 4:printf("/tMay<%d>/n",year);break;
53 case 5:printf("/tJune<%d>/n",year);break;
54 case 6:printf("/tJuly<%d>/n",year);break;
55 case 7:printf("/tAugust<%d>/n",year);break;
56 case 8:printf("/tSeptember<%d>/n",year);break;
57 case 9:printf("/tOctober<%d>/n",year);break;
58 case 10:printf("/tNoveber<%d>/n",year);break;
59 case 11:printf("/tDecember<%d>/n",year);break;
60 }
61 printf("Sun Mon Tue Wed Thu Fri Sat/n");
62 /*考虑闰年的二月份并输入最终的万年历*/
63 i=year_days%7;
64 temp_2=0;
65 for(;temp_2<i;temp_2++)
66 {
67 printf(" ");
68 }
69 day=1;
70 /* 区分闰年与非闰年*/
71 if(isLeapYear(year)&&temp_1==1)
72 {
73 while(day<=month_day[temp_1])
74 {
75 /* 防止出现首行是空行*/
76 if(year_days%7==0&&day!=1)
77 {
78 printf("/n");
79 }
80 /* 布局需要*/
81 if(day<10)
82 {
83 printf(" %d ",day);
84 }
85 else
86 {
87 printf("%d ",day);
88 }
89 day++;
90 year_days++;
91 }
92 printf(" %d",day);
93 year_days++;
94 }
95 else
96 {
97 while(day<=month_day[temp_1])
98 {
99 /* 防止出现首行是空行*/
100 if(year_days%7==0&&day!=1)
101 {
102 printf("/n");
103 }
104 /* 布局需要*/
105 if(day<10)
106 {
107 printf(" %d ",day);
108 }
109 else
110 {
111 printf("%d ",day);
112 }
113 day++;
114 year_days++;
115 }
116 }
117 printf("/n");
118 }
119 exit(0);
120 }
121 /* 判断是否是闰年*/
122 int isLeapYear(int year)
123 {
124 if((year%4==0&&year%100!=0)||(year%400==0))
125 {
126 return 1;
127 }
128 else
129 {
130 return 0;
131 }
132 }


分享给朋友:
您可能感兴趣的文章:
随机阅读: