home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / yacl-012.zip / basedemo / holidays / holidays.cxx next >
C/C++ Source or Header  |  1995-04-08  |  3KB  |  127 lines

  1.  
  2.  
  3. // YACL demo to print out the holidays for a given year
  4. //
  5. // This program is based on the one in "Data abstraction and
  6. // object-oriented programming in C++" by Gorlen, Orlow & Plexico (John
  7. // Wiley, 1990), p. 74.
  8.  
  9. // Invocation:
  10. //
  11. //           holidays [year]
  12. //
  13. // For example,
  14. //
  15. //           holidays 1994
  16. //
  17. // prints the list of holidays in 1994. Without an argument, the list of
  18. // holidays for the current year is printed.
  19.  
  20.  
  21. // M. A. Sridhar, 5/30/94
  22.  
  23.  
  24. #include "base/date.h"
  25. #include <stdio.h>
  26.  
  27. const int HolidayCount = 11;
  28.  
  29. class Holidays {
  30.  
  31.     struct Holiday {
  32.         CL_Date date;
  33.         CL_String desc;
  34.     } day [HolidayCount];
  35.   
  36. public:
  37.     Holidays (short year);
  38.     // Build an array of holidays for the given year.
  39.  
  40.     short Size() {return HolidayCount;};
  41.     // Return the number of holidays in the year.
  42.     
  43.     const Holiday& operator[] (short i) {return day[i];};
  44.     // Return the i-th holiday of the year.
  45.  
  46.  
  47.  
  48. };
  49.  
  50.  
  51.  
  52.  
  53. Holidays::Holidays (short year)
  54. {
  55.     // New year's day:
  56.     day[0].date = CL_Date (year, CL_Date::January, 1);
  57.     day[0].desc = "New year's day";
  58.  
  59.     // M. L. King's birthday: third Monday of January
  60.     day[1].date = CL_Date (year, CL_Date::January, 21).PreviousWeekday
  61.         ("Monday");
  62.     day[1].desc = "M. L. King's birthday";
  63.  
  64.     // Washington's birthday: third Monday of February
  65.     day[2].date = CL_Date (year, CL_Date::February, 21).PreviousWeekday
  66.         (CL_Date::Monday);
  67.     day[2].desc = "Washington's birthday";
  68.  
  69.     // Easter: third Monday in April
  70.     day[3].date = CL_Date (year, CL_Date::April, 21).PreviousWeekday
  71.         (CL_Date::Monday);
  72.     day[3].desc = "Easter";
  73.  
  74.     // Memorial day: last Monday of May
  75.     day[4].date = CL_Date (year, CL_Date::May, 31).PreviousWeekday ("Monday");
  76.     day[4].desc = "Memorial day";
  77.  
  78.     // Independence day
  79.     day[5].date = CL_Date (year, CL_Date::July, 4);
  80.     day[5].desc = "Independence day";
  81.  
  82.     // Labor day: first Monday of September
  83.     day[6].date = CL_Date (year, CL_Date::August, 31).NextWeekday ("Monday");
  84.     day[6].desc = "Labor day";
  85.  
  86.     // Columbus day: second Monday of October
  87.     day[7].date = CL_Date (year, CL_Date::October, 14).PreviousWeekday
  88.         ("Monday");
  89.     day[7].desc = "Columbus day";
  90.  
  91.     // Veteran's day: November 11th
  92.     day[8].date = CL_Date (year, CL_Date::November, 11);
  93.     day[8].desc = "Veteran's day";
  94.  
  95.     // Thanksgiving: fourth Thursday of November
  96.     day[9].date = CL_Date (year, CL_Date::November, 28).PreviousWeekday
  97.         ("Thursday");
  98.     day[9].desc = "Thanksgiving";
  99.  
  100.     // Christmas
  101.     day[10].date = CL_Date (year, CL_Date::December, 25);
  102.     day[10].desc = "Christmas day";
  103. }
  104.  
  105.  
  106.  
  107.  
  108.  
  109. main (int argc, char* argv[])
  110. {
  111.     short year;
  112.     if (argc <= 1) 
  113.         year = CL_Date::Today().Year ();
  114.     else
  115.         year = minl (1999, maxl (1901, CL_String (argv[1]).AsLong()));
  116.  
  117.     Holidays all_hols (year);
  118.     for (short i = 0; i < all_hols.Size(); i++) {
  119.         CL_Date d = all_hols[i].date;
  120.         CL_String dt = d.AsString () + " ("
  121.             + CL_Date::DayName (d.DayOfWeek()) + "): ";
  122.         dt.PadTo (25);
  123.         printf ("%s %s\n", dt.AsPtr(), all_hols[i].desc.AsPtr());
  124.     }
  125.     return 0;
  126. }
  127.