Loading

实例15_C语言绘制万年历

实例说明:

哎呀,我写了好一会,最后还是不理想,确实不想写了。

  1 /**
  2  * Copyright (c) 1991 - 2016 Arvin Tang.
  3  * All rights reserved.
  4  *
  5  * 文件名称:实例_C语言绘制万年历
  6  * 简要描述:
  7  *
  8  * 当前版本:1.0
  9  * 作    者:
 10  * 完成日期:2016-1-7
 11  * 修订说明:
 12  *
 13  * 取代版本:
 14  * 作    者:
 15  * 完成日期:
 16  * 修订说明:
 17  */
 18 #include <stdio.h>
 19 #include <stdlib.h>
 20 //
 21 //clrscr();函数不是标准的C函数,只存在Tuber C中且#include <conio.h>。
 22 //但是可以用system("cls");代替,且#include <stdlib.h>。
 23 
 24 int leap(int year);
 25 int week(int year);
 26 void dayin(int,int);
 27 
 28 int main(int argc, char const *argv[])
 29 {
 30     //实例15_编制万年历
 31     
 32     int year;//年份
 33 
 34     int temp;//闰年返回值
 35 
 36     int wee;//每年第一天周几返回值
 37 
 38     printf("请输入你要打印的年份:\n");
 39     scanf("%d", &year);
 40 
 41     temp = leap(year);
 42 
 43     switch(temp)
 44     {
 45         case 0:
 46         printf("平年\n");
 47         break;
 48         case 1:
 49         printf("闰年\n");
 50         break;
 51         default:
 52         printf("系统错误!\n");
 53         break;
 54     }
 55 
 56     wee = week(year);
 57 
 58     printf("本年第一天周%d\n", wee);
 59 
 60     dayin(temp,wee);
 61 
 62     return 0;
 63 }
 64 
 65 /**
 66  * 函数名称:判断一个年份是否为闰年
 67  * 输入参数:year
 68  * 输出参数:
 69  * 返 回 值:闰年返回1,平年返回0。
 70  */
 71 int leap(int year)
 72 {
 73     if((year % 4 ==0 && year % 100 !=0) || year % 400 ==0)
 74     {
 75         return 1;
 76     }
 77     else
 78     {
 79         return 0;
 80     }
 81 }
 82 
 83 /**
 84  * 函数名称:判断某年的第一天星期几
 85  * 输入参数:year
 86  * 输出参数:
 87  * 返 回 值:返回周几
 88  */
 89 int week(int year)
 90 {
 91     int week;
 92 
 93     week = ((year-1) + (year-1)/4 - (year-1)/100 + (year-1)/400 + 1) % 7;
 94 
 95     if(week == 7)
 96     {
 97         week = 0;
 98     }
 99 
100     return week;
101 }
102 
103 /**
104  * 函数名称:打印万年历
105  * 输入参数:temp,week
106  * 输出参数:
107  * 返 回 值:返回周几
108  */
109 void dayin(int temp,int week)
110 {
111     int l,m,n;
112 
113     int number = 1;
114 
115     int date[12][6][7];//12个月,每个月最多6周,每周最多7天
116 
117     int lin = 0;
118 
119     //给三维数组赋值
120     for(l = 0; l < 12; l++)
121     {
122         for(m = 0; m < 6; m++)
123         {
124             for(n = 0; n < 7; n++, number++)
125             {
126                 if(lin < week+1)
127                 {
128                     date[l][m][n] = 0;//由每年第一天周几判断
129                     lin++;
130                     if(lin == week+1)
131                     {
132                         number = 1;
133                     }
134                 }
135                 else
136                 {
137                     date[l][m][n] = number;
138                 }
139 
140                 if(temp != 0)//非0即为闰年
141                 {
142                     //闰年月份1,3,5,7,8,10,12每月31天,2月29天,其余月份30天
143                     if(l == 0 || l == 2 || l == 4 || l == 6 || l == 7 || l == 9 || l == 11)
144                     {
145                         if(number == 31)
146                         {
147                             number = 0;
148                         }
149                     }
150                     else if(l == 1)
151                     {
152                         if(number == 29)
153                         {
154                             number = 0;
155                         }
156                     }
157                     else
158                     {
159                         if(number == 30)
160                         {
161                             number = 0;
162                         }
163                     }
164                 }
165                 else//平年
166                 {
167                     //平年月份1,3,5,7,8,10,12每月31天,2月28天,其余月份30天
168                     if(l == 0 || l == 2 || l == 4 || l == 6 || l == 7 || l == 9 || l == 11)
169                     {
170                         if(number == 31)
171                         {
172                             number = 0;
173                         }
174                     }
175                     else if(l == 1)
176                     {
177                         if(number == 28)
178                         {
179                             number = 0;
180                         }
181                     }
182                     else
183                     {
184                         if(number == 30)
185                         {
186                             number = 0;
187                         }
188                     }
189                 }
190             }
191         }
192     }
193 
194     //开始打印
195     printf("1月:\n");
196     printf("周日\t周一\t周二\t周三\t周四\t周五\t周六\n");
197     for(l = 0; l < 12; l++)
198     {
199         for(m = 0; m < 6; m++)
200         {
201             for(n = 0; n < 7; n++ , number++)
202             {
203                 if(date[l][m][n] == 0)
204                 {
205                     printf("\t");
206                 }
207                 else if(date[l][m][n] != 0 && date[l][m][n] ==1)
208                 {
209                     printf("\n\n");
210                     printf("%d月:\n", l+2);
211                     printf("周日\t周一\t周二\t周三\t周四\t周五\t周六\n");
212                     printf("%d\t", date[l][m][n]);
213 
214                 }
215                 else
216                 {
217                     printf("%d\t", date[l][m][n]);
218                 }
219             }
220             printf("\n");
221         }
222     }
223 }
我的错误代码

书上代码:

posted @ 2016-01-08 11:09  哈利路亚健儿奋起步伐  阅读(347)  评论(0编辑  收藏  举报