home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1994 #1 / monster.zip / monster / PROG_C / SNIP9404.ZIP / MOON_AGE.C < prev    next >
C/C++ Source or Header  |  1994-04-03  |  2KB  |  67 lines

  1. /* PD by Michelangelo Jones, 1:1/124. */
  2.  
  3. /*
  4. **  Returns 0 for new moon, 15 for full moon,
  5. **  29 for the day before new, and so forth.
  6. */
  7.  
  8. /*
  9. **  This routine sometimes gets "off" by a few days,
  10. **  but is self-correcting.
  11. */
  12.  
  13. int moon_age(int month, int day, int year)
  14. {
  15.       static short int ages[] =
  16.             {18, 0, 11, 22, 3, 14, 25, 6, 17,
  17.              28, 9, 20, 1, 12, 23, 4, 15, 26, 7};
  18.       static short int offsets[] =
  19.             {-1, 1, 0, 1, 2, 3, 4, 5, 7, 7, 9, 9};
  20.  
  21.       if (day == 31)
  22.             day = 1;
  23.       return ((ages[(year + 1) % 19] + ((day + offsets[month-1]) % 30) +
  24.             (year < 1900)) % 30);
  25. }
  26.  
  27. #ifdef TEST
  28.  
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31.  
  32. static char *description[] = {
  33.       "new",                  /* totally dark                         */
  34.       "waxing crescent",      /* increasing to full & quarter light   */
  35.       "in its first quarter", /* increasing to full & half light      */
  36.       "waxing gibbous",       /* increasing to full & > than half     */
  37.       "full",                 /* fully lighted                        */
  38.       "waning gibbous",       /* decreasing from full & > than half   */
  39.       "in its last quarter",  /* decreasing from full & half light    */
  40.       "waning crescent"       /* decreasing from full & quarter light */
  41.       };
  42.  
  43. static char *months[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
  44.                          "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
  45.  
  46. int main(int argc, char *argv[])
  47. {
  48.       int month, day, year, phase;
  49.  
  50.       if (4 > argc)
  51.       {
  52.             puts("Usage: MOON_AGE month day year");
  53.             return EXIT_FAILURE;
  54.       }
  55.       month = atoi(argv[1]);
  56.       day   = atoi(argv[2]);
  57.       year  = atoi(argv[3]);
  58.       if (100 > year)
  59.             year += 1900;
  60.       printf("moon_age(%d, %d, %d) returned %d\n", month, day, year,
  61.             phase = moon_age(month, day, year));
  62.       printf("Moon phase on %d %s %d is %s\n", day, months[month - 1], year,
  63.             description[(int)((phase + 2) * 16L / 59L)]);
  64. }
  65.  
  66. #endif /* TEST */
  67.