Advertisement
ArtisOracle

Untitled

Sep 29th, 2012
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.28 KB | None | 0 0
  1. /*
  2.  name:  Stefan Arambasich
  3.  class :   CIS 150
  4.  due:       12/10
  5.  description: This program is an implementation of a date class and will perform various functions such as setting the date,
  6.                 comparing two dates, and advancing the date one day at a time.
  7.  
  8.  */
  9. #include <iostream>
  10. using namespace std;
  11.  
  12. class date
  13. {
  14. private:
  15.     //Declarations
  16.     int month, day, year;
  17.    
  18. public:
  19.     //Constructors
  20.     date ();
  21.     date (int m, int d, int y);
  22.    
  23.     //Member function prototypes
  24.     int compareDates (date);
  25.     void setDate (int, int, int);
  26.     void increment ();
  27.     int getDay ();
  28.     int getMonth();
  29.     int getYear();
  30.  
  31. };
  32.  
  33. int main ()
  34. {
  35.     //Declare our variables
  36.     int userDay, userMonth, userYear, compareDay, compareMonth, compareYear, day, month, year, when;
  37.     date date1, date2, test;
  38.     char selection;
  39.     //Instead of leaving the user with more numbers than he/she needs, this array converts unclear numbers into specific names
  40.     char monthName[13][20] = {" ", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
  41.    
  42.     //Test the 3 parameter constructor
  43.     test.setDate(3, 1, 1900);
  44.     day = test.getDay();
  45.     month = test.getMonth();
  46.     year = test.getYear();
  47.     cout << "Testing the 3 parameter constructor" << endl << day << " " << month << " " << year << endl;
  48.    
  49.     //The loop affords the user opportunity to use the menu/program as many times as he/she pleases.
  50.     while (selection != 'Q')
  51.     {
  52.         cout << "Menu" << endl
  53.         << "1. Compare two dates" << endl
  54.         << "2. Advance the day by one" << endl
  55.         << "3. Get the date" << endl
  56.         << "4. Get the month" << endl
  57.         << "5. Get the year" << endl
  58.         << "6. Set the date" << endl
  59.         << "Enter your selection below followed by the return key or enter. Or type 'Q' (uppercase) to quit the program.\n\n";
  60.         cin >> selection;
  61.  
  62.         switch (selection)
  63.         {
  64.             case '1':
  65.                 // DO STUFF HERE
  66.                 break;
  67.             case '2':
  68.                 // DO OTHER STUFF HERE
  69.                 break;
  70.             case '3':
  71.                 // DO OTHER STUFF HERE
  72.                 break;
  73.             case '4':
  74.                 // DO OTHER STUFF HERE
  75.                 break;
  76.             case '5':
  77.                 // DO OTHER STUFF HERE
  78.                 break;
  79.             case '6':
  80.                 // DO OTHER STUFF HERE
  81.                 break;
  82.  
  83.             default:
  84.                 cout << "Invalid selection. Try again\n";
  85.                 break;
  86.         }
  87.        
  88.     }
  89. }
  90.  
  91. //Regular functions
  92. int checkMonthLength(int &day, int &month, int &year)
  93. {
  94.     //Define what the length of each month is
  95.     int lengthsOfMonths[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  96.     //Check leap year
  97.     if (day == 28 && month == 2 && (year % 4) == 0)
  98.         day++;
  99.    
  100.     else if (day < lengthsOfMonths[month])
  101.         day++;
  102.    
  103.     else if ((day + 1) > lengthsOfMonths[month] && month != 12)
  104.     {
  105.         day = 1;
  106.         month++;
  107.     }
  108.     else if (month == 12)
  109.     {
  110.         day = 1;
  111.         month = 1;
  112.         year++;
  113.     }
  114.     //Compiler complains if I don't have this for some reason.
  115.     return 0;
  116. }
  117.  
  118. //Member functions
  119. int date::getDay ()
  120. {
  121.     return day;
  122. }
  123.  
  124. int date::getMonth()
  125. {
  126.     return month;
  127. }
  128.  
  129. int date::getYear ()
  130. {
  131.     return year;
  132. }
  133.  
  134.  
  135. void date::increment ()
  136. {
  137.     //Increment the day regardless if it's less than 28. If it's 28, we need to check for Feb.
  138.     if (day < 28)
  139.         day++;
  140.     else
  141.         checkMonthLength(day, month, year);
  142.    
  143. }
  144.  
  145. int date::compareDates (date x)
  146. {
  147.     /* If the day entered is the same, return 0.  After that, this process goes from broad (years) to more specific (days) to analyze whether a user's input
  148.     occurs before or after the date set by the constuctor or by case 6 in the original switch statement */
  149.    
  150.      if (year < x.year)
  151.         return -1;
  152.     else if (year > x.year)
  153.         return 1;
  154.     else if (year == x.year)
  155.     {
  156.         if (month < x.month)
  157.             return -1;
  158.         else if (month > x.month)
  159.             return 1;
  160.         else if (month == x.month)
  161.         {
  162.             if (day < x.day)
  163.                 return -1;
  164.             else if (day > x.day)
  165.                 return 1;
  166.         }
  167.     }
  168.     return 0;
  169. }
  170.  
  171. //Since the constructor cannot be called upon, we must write a separate function to re-set the date.
  172. void date::setDate (int userDay, int userMonth, int userYear)
  173. {
  174.     month = userMonth;
  175.     day = userDay;
  176.     year = userYear;
  177. }
  178.  
  179. //Sets the date to 1/1/1900 initally
  180. date::date()
  181. {
  182.     month = 1;
  183.     day = 1;
  184.     year = 1900;
  185. }
  186.  
  187. //3 parameter constructor. Unfortunately serves no purpose in this program, but I like full credit, so I will add it in here.
  188. date::date (int m, int d, int y)
  189. {
  190.     month = m;
  191.     day = d;
  192.     year = y;
  193. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement