sepo

Answer to Exercise 5-8, page 112 [1]

Apr 16th, 2012
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.13 KB | None | 0 0
  1. /*
  2.  * A solution to exercise 5-8 in K&R2, page 112:
  3.  *
  4.  *  There is no error checking in day_of_year or month_day. Remedy
  5.  *  this defect.
  6.  *
  7.  * The error to check for is invalid argument values. That is simple, what's
  8.  * hard is deciding what to do in case of error. In the real world, I would
  9.  * use the assert macro from assert.h, but in this solution I take the
  10.  * approach of returning -1 instead. This is more work for the caller, of
  11.  * course.
  12.  *
  13.  * I have selected the year 1752 as the lowest allowed year, because that
  14.  * is when Great Britain switched to the Gregorian calendar, and the leap
  15.  * year validation is valid only for the Gregorian calendar.
  16.  *
  17.  */
  18.  
  19. #include <stdio.h>
  20.  
  21. static char daytab[2][13] =  {
  22.     {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
  23.     {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
  24. };
  25.  
  26. /* day_of_year: set day of year from month & day */
  27. int day_of_year(int year, int month, int day)
  28. {
  29.     int i, leap;
  30.    
  31.     if (year < 1752 || month < 1 || month > 12 || day < 1)
  32.         return -1;
  33.  
  34.     leap = (year%4 == 0 && year%100 != 0) || year%400 == 0;
  35.     if (day > daytab[leap][month])
  36.         return -1;
  37.  
  38.     for (i = 1; i < month; i++)
  39.         day += daytab[leap][i];
  40.     return day;
  41. }
  42.  
  43. /* month_day: set month, day from day of year */
  44. int month_day(int year, int yearday, int *pmonth, int *pday)
  45. {
  46.     int i, leap;
  47.    
  48.     if (year < 1752 || yearday < 1)
  49.         return -1;
  50.  
  51.     leap = (year%4 == 0 && year%100 != 0) || year%400 == 0;
  52.     if ((leap && yearday > 366) || (!leap && yearday > 365))
  53.         return -1;
  54.  
  55.     for (i = 1; yearday > daytab[leap][i]; i++)
  56.         yearday -= daytab[leap][i];
  57.     *pmonth = i;
  58.     *pday = yearday;
  59.    
  60.     return 0;
  61. }
  62.  
  63.  
  64. /* main: test day_of_year and month_day */
  65. int main(void)
  66. {
  67.     int year, month, day, yearday;
  68.    
  69.     for (year = 1970; year <= 2000; ++year) {
  70.         for (yearday = 1; yearday < 366; ++yearday) {
  71.             if (month_day(year, yearday, &month, &day) == -1) {
  72.                 printf("month_day failed: %d %d\n",
  73.                     year, yearday);
  74.             } else if (day_of_year(year, month, day) != yearday) {
  75.                 printf("bad result: %d %d\n", year, yearday);
  76.                 printf("month = %d, day = %d\n", month, day);
  77.             }
  78.         }
  79.     }
  80.    
  81.     return 0;
  82. }
Advertisement
Add Comment
Please, Sign In to add comment