• 问题反馈、粉丝交流 QQ群327452739 蓝桥杯训练群:113766799 申请群时请备注排名里的昵称
  • C语言研究中心 为您提供有图、有料、解渴的C语言专题! 欢迎讨论!
  • 欢迎访问C语言网www.dotcpp.com 比赛栏每月有奖月赛!举办比赛联系QQ:2045302297

C语言小程序实现万年历

项目源码 CTO 21926次浏览 3个评论

分享一个简单的小程序,看看如何打印万年历吧~

程序打开时时这样的,输入一个年份:

C语言小程序实现万年历

比如输入2016再回车,效果如下:

C语言小程序实现万年历

下面我们来看看实现这个程序的代码吧:

  1. #include<stdio.h>
  2. #include<windows.h>
  3.  
  4. #define Mon 1
  5. #define Tues 2
  6. #define Wed 3
  7. #define Thur 4
  8. #define Fri 5
  9. #define Sat 6
  10. #define Sun 0
  11.  
  12. #define January_days 31
  13. #define February_days 28
  14. #define March_days 31
  15. #define April_days 30
  16. #define May_days 31
  17. #define June_days 30
  18. #define July_days 31
  19. #define August_days 31
  20. #define September_days 30
  21. #define October_days 31
  22. #define November_days 30
  23. #define December_days 31
  24.  
  25. #define first1month January_days
  26. #define first2month January_days+February_days
  27. #define first3month January_days+February_days+March_days
  28. #define first4month January_days+February_days+March_days+April_days
  29. #define first5month January_days+February_days+March_days+April_days+May_days
  30. #define first6month January_days+February_days+March_days+April_days+May_days+June_days
  31. #define first7month January_days+February_days+March_days+April_days+May_days+June_days \
  32. +July_days
  33. #define first8month January_days+February_days+March_days+April_days+May_days+June_days \
  34. +July_days+August_days
  35. #define first9month January_days+February_days+March_days+April_days+May_days+June_days \
  36. +July_days+August_days+September_days
  37. #define first10month January_days+February_days+March_days+April_days+May_days+June_days \
  38. +July_days+August_days+September_days+October_days
  39. #define first11month January_days+February_days+March_days+April_days+May_days+June_days \
  40. +July_days+August_days+September_days+October_days+November_days
  41.  
  42. int known_weekday = Tues;
  43. int known_year = 1901;
  44. int konwn_month = 1;
  45. int known_day = 1;
  46.  
  47. int day_count(int month)
  48. {
  49. switch(month)
  50. {
  51. case 1: return 0;break;
  52. case 2: return first1month;break;
  53. case 3: return first2month;break;
  54. case 4: return first3month;break;
  55. case 5: return first4month;break;
  56. case 6: return first5month;break;
  57. case 7: return first6month;break;
  58. case 8: return first7month;break;
  59. case 9: return first8month;break;
  60. case 10: return first9month;break;
  61. case 11: return first10month;break;
  62. case 12: return first11month;break;
  63. }
  64. }
  65.  
  66.  
  67. /*char * month_name(int month)
  68. {
  69. switch(month)
  70. {
  71. case 1:
  72. return "January";
  73. break;
  74. case 2:
  75. return "February";
  76. break;
  77. case 3:
  78. return "March";
  79. break;
  80. case 4:
  81. return "April";
  82. break;
  83. case 5:
  84. return "May";
  85. break;
  86. case 6:
  87. return "June";
  88. break;
  89. case 7:
  90. return "July";
  91. break;
  92. case 8:
  93. return "August";
  94. break;
  95. case 9:
  96. return "September";
  97. break;
  98. case 10:
  99. return "October";
  100. break;
  101. case 11:
  102. return "November";
  103. break;
  104. case 12:
  105. return "December";
  106. break;
  107. default:
  108. break;
  109. }
  110. }*/
  111.  
  112. char * month_name(int month)
  113. {
  114. switch(month)
  115. {
  116. case 1:
  117. return "一月";
  118. break;
  119. case 2:
  120. return "二月";
  121. break;
  122. case 3:
  123. return "三月";
  124. break;
  125. case 4:
  126. return "四月";
  127. break;
  128. case 5:
  129. return "五月";
  130. break;
  131. case 6:
  132. return "六月";
  133. break;
  134. case 7:
  135. return "七月";
  136. break;
  137. case 8:
  138. return "八月";
  139. break;
  140. case 9:
  141. return "九月";
  142. break;
  143. case 10:
  144. return "十月";
  145. break;
  146. case 11:
  147. return "十一月";
  148. break;
  149. case 12:
  150. return "十二月";
  151. break;
  152. default:
  153. break;
  154. }
  155. }
  156.  
  157. /*按格式打印某年某月名称*/
  158. void first_line_print(int month, int year)
  159. {
  160. //printf("%20s %d \n",month_name(month),year);
  161. printf("%8d年 %s \n",year, month_name(month));
  162. }
  163.  
  164. /*按格式打印星期名称*/
  165. void week_print()
  166. {
  167. //printf("%-6s%-6s%-6s%-6s%-6s%-6s%-6s\n","Sun.", "Mon.", "Tues.", "Wed.", "Thur.", "Fri.", "Sat.");
  168. printf("%-3s%-3s%-3s%-3s%-3s%-3s%-3s\n","日", "一", "二", "三", "四", "五", "六");
  169. }
  170.  
  171. /*计算该年该月份与已知日子之间的距离天数*/
  172. int date_distance_count(int month, int year)
  173. {
  174. int leap_year_count = 0;
  175. int i;
  176. int distance;
  177. if (year > known_year)
  178. {
  179. for (i=known_year; i<year; i++)
  180. {
  181. if(((i%4 == 0) && (i%100 != 0) ) || (i%400 == 0))
  182. {
  183. leap_year_count++;
  184. }
  185. }
  186. if (month > 2)
  187. {
  188. if(((year%4 == 0) && (year%100 != 0) ) || (year%400 == 0))
  189. {
  190. leap_year_count++;
  191. }
  192. }
  193. }
  194. else
  195. if (year == known_year)
  196. {
  197. if (month > 2)
  198. {
  199. leap_year_count = 1;
  200. }
  201. }
  202. distance = (year - known_year)*365 + leap_year_count + day_count(month);
  203. return distance;
  204. }
  205.  
  206. /*确定该月份第一天是星期几*/
  207. int makesure_firstday_weekday(int month, int year)
  208. {
  209. int date_distance = 0;
  210. int weekday;
  211. date_distance = date_distance_count(month, year);
  212. weekday = (known_weekday + date_distance)%7;
  213. return weekday;
  214. }
  215.  
  216. /*依次打印出该月份的日子*/
  217. void print_in_turn(int month, int firstday, int year)
  218. {
  219. int i = 1;
  220. int weekday;
  221. switch(firstday)
  222. {
  223. case Sun:
  224. break;
  225. case Mon:
  226. printf("%-3s","");
  227. break;
  228. case Tues:
  229. printf("%-6s","");
  230. break;
  231. case Wed:
  232. printf("%-9s","");
  233. break;
  234. case Thur:
  235. printf("%-12s","");
  236. break;
  237. case Fri:
  238. printf("%-15s","");
  239. break;
  240. case Sat:
  241. printf("%-18s","");
  242. break;
  243. }
  244. switch(month)
  245. {
  246. case 1:
  247. case 3:
  248. case 5:
  249. case 7:
  250. case 8:
  251. case 10:
  252. case 12:
  253. {
  254. for(i=0; i<31; i++)
  255. {
  256. weekday = (firstday + i)%7;
  257. printf("%-3d",i+1);
  258. if(weekday == Sat)
  259. {
  260. printf("\n");
  261. }
  262. }
  263. break;
  264. }
  265. case 2:
  266. {
  267. if(((year%4 == 0) && (year%100 != 0) ) || (year%400 == 0)) /*闰年*/
  268. {
  269. for(i=0; i<29; i++)
  270. {
  271. weekday = (firstday + i)%7;
  272. printf("%-3d",i+1);
  273. if(weekday == Sat)
  274. {
  275. printf("\n");
  276. }
  277. }
  278. break;
  279. }
  280. else /*平年*/
  281. {
  282. for(i=0; i<28; i++)
  283. {
  284. weekday = (firstday + i)%7;
  285. printf("%-3d",i+1);
  286. if(weekday == Sat)
  287. {
  288. printf("\n");
  289. }
  290. }
  291. break;
  292. }
  293. }
  294. case 4:
  295. case 6:
  296. case 9:
  297. case 11:
  298. {
  299. for(i=0; i<30; i++)
  300. {
  301. weekday = (firstday + i)%7;/*计算该天是星期几*/
  302. printf("%-3d",i+1);
  303. if(weekday == Sat)
  304. {
  305. printf("\n");/*如果是星期六,则换行*/
  306. }
  307. }
  308. break;
  309. }
  310. }
  311. }
  312.  
  313. void date_print(int month, int year)
  314. {
  315. int firstday;
  316. firstday = makesure_firstday_weekday(month, year);
  317. print_in_turn(month, firstday, year);
  318. printf("\n");
  319. }
  320.  
  321. void main_month(int month, int year)
  322. {
  323. first_line_print(month, year);
  324. week_print();
  325. date_print(month, year);
  326. printf("\n\n");
  327. }
  328.  
  329. void main_calendar(int year)
  330. {
  331. int i;
  332. for(i=1; i<=12; i++)
  333. {
  334. main_month(i, year);
  335. }
  336. }
  337.  
  338. int main()
  339. {
  340. int year;
  341. system("title c语言研究中心 www.dotcpp.com");
  342. printf("请输入年份:year = ");
  343. scanf("%d",&year);
  344. printf("\n");
  345. while(year < 1902)
  346. {
  347. printf("请输入大于1901的年份\n");
  348. printf("请输入年份:year = ");
  349. scanf("%d",&year);
  350. printf("\n");
  351. }
  352. main_calendar(year);
  353. scanf(" ");
  354. return 0;
  355. }

C语言研究中心(www.dotcpp.com)

C语言网提供「C语言、C++、算法竞赛」在线课程,全部由资深研发工程师或ACM金牌大佬亲授课,更科学、全面的课程体系,以在线视频+在线评测的学习模式学习,学练同步,拒绝理论派,真正学会编程!还有奖学金等增值福利等你!

C语言网, 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明C语言小程序实现万年历
喜欢 (56)
[jinyangH@aliyun.com]
分享 (0)
发表我的评论
取消评论
表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
(3)个小伙伴在吐槽
  1. else if (year == known_year)//不懂 { if (month > 2) { leap_year_count = 1;//为什么这里要等于1,之前不是判断过输入1901要求重新输入吗 }, }
    坤坤2018-04-10 10:17 回复
    • 二月是一个特殊年,如果月份超过了需要用一个方法去判断当前年份二月的天数
      2021-11-03 19:08 回复
  2. vs出错
    sss2019-01-24 21:22 回复