Chinaunix首页 | 论坛 | 博客
  • 博客访问: 50701
  • 博文数量: 27
  • 博客积分: 716
  • 博客等级: 上士
  • 技术积分: 285
  • 用 户 组: 普通用户
  • 注册时间: 2011-08-31 11:12
文章分类

全部博文(27)

文章存档

2012年(8)

2011年(19)

我的朋友

分类: C/C++

2011-09-17 12:26:48

  1. /*

  2. * File : calender.c

  3. * ----------------------

  4. * This program is used to generate a calender for a year entered by user.

  5. */



  6. #include <stdio.h>



  7. /*

  8. * Constants:

  9. * -----------

  10. * Days of the week are represented by the integers 0-6.

  11. * Month of the year are identified by the integers 1-12.

  12. */



  13. #define Sunday 0

  14. #define Monday 1

  15. #define Tuesday 2

  16. #define Wednesday 3

  17. #define Thursday 4

  18. #define Friday 5

  19. #define Saturday 6



  20. #define IsALeapYear(year) ((year % 4 == 0) && (year % 100 !=0)) || (year % 400 == 0)



  21. /* Fuction prototypes */



  22. void GiveInstructions();

  23. int GetYear();

  24. void PrintCalender(int year);

  25. void PrintCalenderMonth(int year,int month);

  26. char * MonthName(int month);

  27. int FirstDayOfMonth(int year,int month);

  28. int MonthDays(int year,int month);

  29. void IndentFirstLine(int weekday);





  30. /* Main Program */



  31. void main()

  32. {

  33. int year;



  34. GiveInstructions();

  35. year = GetYear();

  36. PrintCalender(year);

  37. }





  38. /*

  39. * Function: GiveInstructions

  40. * Usage: GiveInstructions();

  41. * ------------------------------------

  42. * This program prints out instructions to user.

  43. */



  44. void GiveInstructions()

  45. {

  46. printf("\n\n----------This program displays a calender for a full year.----------\n\n\n");

  47. }





  48. /*

  49. * Function: GetYear

  50. * Usage: year = GetYear();

  51. * -----------------------------------

  52. * This program will get a year from user.

  53. */



  54. int GetYear()

  55. {

  56. int year;



  57. printf("-----Please input a year:");

  58. scanf("%d",&year);

  59. printf("\n");

  60. return (year);

  61. }





  62. /*

  63. * Fuction: PrintCalender

  64. * Usage: PrintCalender(year);

  65. * ---------------------------------------

  66. * This program will print out a calender for a entire year.

  67. */



  68. void PrintCalender(int year)

  69. {

  70. int month;



  71. for (month = 1; month <= 12; month++) {

  72. PrintCalenderMonth(year, month);

  73. }

  74. }





  75. /*

  76. * Function: PrintCalenderMonth

  77. * Usage: PrintCalenderMonth(int year, int month)

  78. * -------------------------------------------------------------

  79. * This program will print out a calender for a entire month.

  80. */



  81. void PrintCalenderMonth(int year, int month)

  82. {

  83. int nDays,weekday,i;



  84. printf(" %s %d\n",MonthName(month),year);

  85. printf(" Su Mo Tu We Th Fr Sa\n");



  86. nDays = MonthDays(year,month);

  87. weekday = FirstDayOfMonth(year,month);



  88. IndentFirstLine (weekday);

  89. for (i = 1; i <= nDays; i++) {

  90. printf(" %2d",i);

  91. if (weekday == Saturday) printf("\n");

  92. weekday = (weekday + 1) % 7;

  93. }

  94. if (weekday != Sunday) printf("\n");

  95. }





  96. /*

  97. * Function: MonthName

  98. * Usage: char * MonthName(month)

  99. * ------------------------------------------

  100. * This program prints out the monthname.

  101. */



  102. char * MonthName(int month)

  103. {

  104. switch (month) {

  105. case 1: return ("January");

  106. case 2: return ("February");

  107. case 3: return ("March");

  108. case 4: return ("April");

  109. case 5: return ("May");

  110. case 6: return ("June");

  111. case 7: return ("July");

  112. case 8: return ("August");

  113. case 9: return ("September");

  114. case 10: return ("October");

  115. case 11: return ("November");

  116. case 12: return ("December");

  117. default: return ("Illegal month");

  118. }

  119. }





  120. /*

  121. * Function: FirstDayOfMonth

  122. * Usage: weekday = FirstDayOfMonth(year,month)

  123. * -------------------------------------------------------

  124. * This program returns the weekday of a month.

  125. */



  126. int FirstDayOfMonth(int year,int month)

  127. {

  128. int i,weekday,nDays;

  129. weekday = Monday;



  130. if (year > 1900) {

  131. for (i = 1900; i < year; i++) {

  132. weekday = (weekday + 365) % 7;

  133. if (IsALeapYear(i)) weekday = (weekday + 1) % 7;

  134. }

  135. for (i = 1; i < month; i++) {

  136. nDays = MonthDays(year,i);

  137. weekday = (weekday + nDays) % 7;

  138. }

  139. }

  140. else{

  141. for (i = 1900; i > year; i--) {

  142. weekday = (weekday - 365 + 7 * 53 ) % 7;

  143. if (IsALeapYear(i)) weekday = (weekday + 6) % 7;

  144. }

  145. for (i = 1; i < month; i++) {

  146. nDays = MonthDays(year,i);

  147. weekday = (weekday + nDays) % 7;

  148. }

  149. }

  150. return (weekday);

  151. }





  152. /*

  153. * Function: MonthDays

  154. * Usage: nDays = MonthDays(year,month)

  155. * -----------------------------------------------------

  156. * This program returns the number of days for a month.

  157. */



  158. int MonthDays(int year,int month)

  159. {

  160. switch (month) {

  161. case 2:

  162. if (IsALeapYear(year)) return (29);

  163. return (28);

  164. case 4:case 6:case 9:case 11:

  165. return (30);

  166. default : return (31);

  167. }

  168. }





  169. /*

  170. * Function: IndentFirstLine

  171. * Usage: IndentFirstLine(weekday)

  172. * --------------------------------------

  173. * This program prints out some blank spaces for the calender.

  174. */



  175. void IndentFirstLine(int weekday)

  176. {

  177. int i;



  178. for (i = 0; i < weekday; i++)

  179. {

  180. printf(" ");

  181. }

  182. }





  183. /* 参考《C语言的科学与艺术》*/

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