[关闭]
@SiberiaBear 2016-03-09T10:24:25.000000Z 字数 3011 阅读 1591

BlackFriday.c

代码


  1. #include <stdio.h>
  2. #define FIRSTDAY_OF_BEGINYEAR (4) // the first January of 1970 is Thursday
  3. #define BEGIN_YEAR (1970) // begin with 1970
  4. //#define TEST
  5. 地臩ꮽ
  6. int find_leapyear(int beginyear, int endyear);
  7. int main(int argc, char *argv[])
  8. {
  9. int input_year = 0;
  10. int leapyear = 0;
  11. int before_full_day = 0;
  12. int weekday_of_inputyear_firstday = 0;
  13. int weekday_count = 0;
  14. int yearday_count = 1;
  15. int monthday_count = 1;
  16. int month_count = 1;
  17. int inputyear_leap_flag = 0; // zero is normal year, one is leap year
  18. if( 1 == argc) {
  19. fprintf(stderr, "BlackFriday: error: without any year input!\n");
  20. return -1;
  21. }
  22. if( argc > 2) {
  23. fprintf(stderr, "BlackFriday: error: out of parameters!\n");
  24. fprintf(stdout, "BlackFriday: Please input only one year.\n");
  25. return -2;
  26. }
  27. input_year = ((unsigned int)argv[1][0]-48)*1000 + ((unsigned int)argv[1][1]-48)*100 + ((unsigned int )argv[1][2]-48)*10 + ((unsigned int )argv[1][3]-48)*1;
  28. if(input_year <= 1970 || input_year > 3000) {
  29. fprintf(stderr, "BlackFriday: error: out of allowed year!\n");
  30. fprintf(stderr, "BlackFriday: error: Please input a year within the range of 1970 to 3000.\n");
  31. return -3;
  32. }
  33. fprintf(stdout, "Input year is %d\n", input_year);
  34. if( ((input_year%4 == 0) && (input_year % 100 != 0)) || (input_year % 400 == 0)) {
  35. inputyear_leap_flag = 1;
  36. fprintf(stdout, "Input year leap year\n");
  37. }
  38. else {
  39. inputyear_leap_flag = 0;
  40. fprintf(stdout, "Input year normal year\n");
  41. }
  42. leapyear = find_leapyear(BEGIN_YEAR, input_year);
  43. fprintf(stdout, "before input year(without input year) have %d leap year\n", leapyear);
  44. before_full_day = leapyear * 366 + (input_year - BEGIN_YEAR - leapyear) * 365;
  45. #ifdef TEST
  46. fprintf(stdout, "before_full_day is %d days\n", before_full_day);
  47. #endif
  48. weekday_of_inputyear_firstday = (FIRSTDAY_OF_BEGINYEAR + (before_full_day % 7))%7;
  49. #ifdef TEST
  50. fprintf(stdout, "weekday of %s firstday is %d\n", argv[1], weekday_of_inputyear_firstday);
  51. #endif
  52. weekday_count = weekday_of_inputyear_firstday;
  53. fprintf(stdout, "In the year of %d :\n", input_year);
  54. while(month_count <= 12) // Begin a year loop
  55. {
  56. #ifdef TEST
  57. fprintf(stdout, "Calendar: weekday_count: %d, monthday_count: %2d, month_count: %2d, yearday_count: %3d\n", weekday_count, monthday_count, month_count, yearday_count);
  58. #endif
  59. if(monthday_count == 13) {
  60. if(weekday_count == 5)
  61. fprintf(stdout, "BlackFriday: Month %d . Day %d\n", month_count, monthday_count);
  62. }
  63. weekday_count ++;
  64. monthday_count ++;
  65. yearday_count ++;
  66. if(weekday_count > 6)
  67. weekday_count = 0;
  68. if(month_count == 1 || month_count == 3 || month_count == 5 || month_count == 7 || month_count == 8 || month_count == 10 || month_count == 12) {
  69. if(monthday_count > 31) {
  70. month_count ++;
  71. monthday_count = 1;
  72. }
  73. }
  74. else if(month_count == 4 || month_count == 6 || month_count == 9 || month_count == 11 ) {
  75. if(monthday_count > 30) {
  76. month_count ++;
  77. monthday_count = 1;
  78. }
  79. }
  80. else if(month_count == 2) {
  81. if(monthday_count > (28 + inputyear_leap_flag) ) {
  82. month_count ++;
  83. monthday_count = 1;
  84. }
  85. }
  86. }// End a year loop
  87. return 0;
  88. }
  89. int find_leapyear(int beginyear, int endyear) {
  90. #ifdef TEST
  91. fprintf(stdout, "beginyear: %d, endyear: %d\n", beginyear, endyear);
  92. #endif
  93. int year_count = beginyear;
  94. int leap_count = 0;
  95. do {
  96. #ifdef TEST
  97. fprintf(stdout, "leap_year: %d, year_count: %d\n", leap_count, year_count);
  98. #endif
  99. if ((year_count % 4 == 0)&&(year_count % 100 != 0) || (year_count % 400 == 0)) {
  100. fprintf(stdout, "%d is leap year\n", year_count);
  101. leap_count ++;
  102. }
  103. year_count ++;
  104. }while(year_count != endyear);
  105. return leap_count;
  106. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注