Advertisement
Guest User

Calendar

a guest
Feb 22nd, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 13.79 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <string>
  4. #include <stdio.h>
  5. #include "date.h"
  6. using namespace std;
  7.  
  8. Date::Date(int m, int d, int y)
  9. {
  10.     //century used for if the year ends on a number divisble by 1000
  11.     int century;
  12.     bool error = false;
  13.     //needed for leap year
  14.     if (y % 1000 == 0)
  15.     {
  16.         century = y;
  17.     }
  18.     //Month error check
  19.     if (m < 1 || m > 12)
  20.     {
  21.         error = true;
  22.     }
  23.     //Year error check
  24.     if (y < 1900)
  25.     {
  26.         error = true;
  27.     }
  28.     //Day less than 0 Check
  29.     if (d <= 0)
  30.     {
  31.         error = true;
  32.     }
  33.  
  34.     //Error check for correct day for each month
  35.     switch (m)
  36.     {
  37.         //if february calculate if its a leap year and allow 29 if not do not allow 29
  38.         case 2:
  39.             if (d > 28)
  40.             {
  41.                 error = true;
  42.             }
  43.             if (d == 29 && (y % 4 == 0 || century % 400 == 0))
  44.             {
  45.                 d = 29;
  46.             }
  47.             break;
  48.         //cascade effect for months that end in 30
  49.         case 4:
  50.         case 6:
  51.         case 11:
  52.             if (d > 30)
  53.             {
  54.                 error = true;
  55.             }
  56.             break;
  57.         //cascade effect for months that end in 31
  58.         case 1:
  59.         case 3:
  60.         case 5:
  61.         case 7:
  62.         case 8:
  63.         case 10:
  64.         case 12:
  65.             if (d > 31)
  66.             {
  67.                 error = true;
  68.             }
  69.             break;
  70.     }
  71.     //Assigning values to Private class
  72.     //if the error is false than the values put into the constructor are
  73.     //the private member functions
  74.     if (error == false)
  75.     {
  76.         day = d;
  77.         month = m;
  78.         year = y;
  79.     }
  80.     //if the error is true than change the values to the defualt values
  81.     else
  82.     {
  83.         day = 1;
  84.         month = 1;
  85.         year = 2019;
  86.     }
  87. }
  88.  
  89. //allows the user to input a date and see if its legal or not
  90. void Date::input()
  91. {
  92.     //numbers are initalized so that if they don't enter it right it wont change and will results
  93.     //in an error
  94.  
  95.     int m1 = -1, d2 = -1, y3 = -1, temp, temp2;
  96.     string m, d, y;
  97.     string date;
  98.     bool illegal = false;
  99.     //while loop so if you enter a bad value it prompt so enter again
  100.     do
  101.     {
  102.         cout << "input date in month/day/year format: ";
  103.         cin >> date;
  104.  
  105.         //if else blocks used for all the scenarios of two digit and one digit dates
  106.         if (date[1] == '/')
  107.         {
  108.             m1 = '0' - date[0];
  109.             m1 *= -1;
  110.         }
  111.         else
  112.         {
  113.             m = date[0] + date[1];
  114.             temp = '0' - date[1];
  115.             temp2 = '0' - date[0];
  116.             m1 = temp2 * 10 + temp;
  117.             m1 *= -1;
  118.         }
  119.         if (date[1] == '/' && date[3] == '/')
  120.         {
  121.             d2 = ('0' - date[2]) * -1;
  122.         }
  123.         else if (date[1] == '/' && date[4] == '/')
  124.         {
  125.             temp = '0' - date[2];
  126.             temp2 = '0' - date[3];
  127.             d2 = temp * 10 + temp2;
  128.             d2 *= -1;
  129.         }
  130.         else if (date[2] == '/' && date[4] == '/')
  131.         {
  132.             d2 = ('0' - date[3]) * -1;
  133.         }
  134.         else if (date[2] == '/' && date[5] == '/')
  135.         {
  136.             temp = ('0' - date[3]);
  137.             temp2 = '0' - date[4];
  138.             d2 = temp2 + temp * 10;
  139.             d2 *= -1;
  140.         }
  141.         //the year only matters where the slash is and there are 4 possible places after that the year
  142.         //is always four digits, so i use exponential addition to add them
  143.         if (date[3] == '/')
  144.         {
  145.             y3 = (('0' - date[4]) * 1000 + ('0' - date[5]) * 100 + ('0' - date[6]) * 10 + ('0' - date[7])) * -1;
  146.         }
  147.         else if (date[4] == '/')
  148.         {
  149.             y3 = (('0' - date[5]) * 1000 + ('0' - date[6]) * 100 + ('0' - date[7]) * 10 + ('0' - date[8])) * -1;
  150.         }
  151.         else if (date[5] == '/')
  152.         {
  153.             y3 = (('0' - date[6]) * 1000 + ('0' - date[7]) * 100 + ('0' - date[8]) * 10 + ('0' - date[9])) * -1;
  154.         }
  155.         Date iii(m1, d2, y3);
  156.         if (iii.month == m1 || iii.day == d2 || iii.year == y3)
  157.         {
  158.             illegal = false;
  159.         }
  160.         else
  161.         {
  162.             illegal = true;;
  163.             cout << "Invalid date. Try again" << endl;
  164.         }
  165.     }
  166.     while (illegal == true);
  167. }
  168.  
  169. //returns the month in number value
  170. int Date::GetMonth() const
  171. {
  172.     return month;
  173. }
  174. //returns the dat in number value
  175. int Date::GetDay() const
  176. {
  177.     return day;
  178. }
  179. //returns the year in number value
  180. int Date::GetYear() const
  181. {
  182.     return year;
  183. }
  184. //same as the constructor, except if false, doesn't change anything
  185. bool Date::Set(int m, int d, int y)
  186. {
  187.     int century;
  188.     bool error;
  189.     //needed for leap year
  190.     if (y % 1000 == 0)
  191.     {
  192.         century = y;
  193.     }
  194.     //Month error check
  195.     if (m < 1 || m > 12)
  196.     {
  197.         error = true;
  198.     }
  199.     //Year error check
  200.     if (y < 1900)
  201.     {
  202.         error = true;
  203.     }
  204.     //Day less than 0 Check
  205.     if (d <= 0)
  206.     {
  207.         error = true;
  208.     }
  209.     //Error check for correct day for each month
  210.     switch (m)
  211.     {
  212.  
  213.         case 2:
  214.             if (d > 28)
  215.             {
  216.                 error = true;
  217.             }
  218.             if (d == 29 && (y % 4 == 0 || century % 400 == 0))
  219.             {
  220.                 d = 29;
  221.             }
  222.             break;
  223.         case 4:
  224.         case 6:
  225.         case 11:
  226.             if (d > 30)
  227.             {
  228.                 error = true;
  229.             }
  230.             break;
  231.         case 1:
  232.         case 3:
  233.         case 5:
  234.         case 7:
  235.         case 8:
  236.         case 10:
  237.         case 12:
  238.             if (d > 31)
  239.             {
  240.                 error = true;
  241.             }
  242.             break;
  243.     }
  244.     //Assigning values to Private class
  245.     //if there is no error change private variables, if there is error dont change
  246.     if (error == false)
  247.     {
  248.         day = d;
  249.         month = m;
  250.         year = y;
  251.         return true;
  252.     }
  253.     else
  254.     {
  255.         return false;
  256.     }
  257. }
  258.  
  259. //increments the day by 1 and changes the month if the day goes over the month
  260. void Date::Increment()
  261. {
  262.     int m, d, y;
  263.     m = month;
  264.     d = day;
  265.     y = year;
  266.  
  267.     d++;
  268.  
  269.     int century;
  270.     //needed for leap year
  271.     if (y % 1000 == 0)
  272.     {
  273.         century = y;
  274.     }
  275.  
  276.  
  277.     //Error check for correct day for each month
  278.     //Switch statement for different months because some months have different days
  279.     switch (m)
  280.     {
  281.         case 2:
  282.             if (d == 29 && (y % 4 == 0 || century % 400 == 0))
  283.             {
  284.                 d = 29;
  285.             }
  286.             else if (d > 28)
  287.             {
  288.                 m++;
  289.                 d = 1;
  290.             }
  291.             break;
  292.         case 4:
  293.         case 6:
  294.         case 11:
  295.             if (d > 30)
  296.             {
  297.                 m++;
  298.                 d = 1;
  299.             }
  300.             break;
  301.         case 1:
  302.         case 3:
  303.         case 5:
  304.         case 7:
  305.         case 8:
  306.         case 10:
  307.         case 12:
  308.             if (d > 31)
  309.             {
  310.                 //checks so if the month is 12, change hte month to 1 instead of 13
  311.                 if (m != 12)
  312.                 {
  313.                     m++;
  314.                 }
  315.                 else
  316.                 {
  317.                     m = 1;
  318.                 }
  319.                 d = 1;
  320.             }
  321.             break;
  322.     }
  323.     //Assigning values to Private class
  324.     day = d;
  325.     month = m;
  326.     year = y;
  327. }
  328.  
  329. //returns a value 0-6 0 being sunday and 6 being Friday
  330. int Date::DayofWeek()
  331. {
  332.     //this function was given to us
  333.     int d, y, m;
  334.  
  335.     y = year;
  336.     m = month;
  337.     d = day;
  338.  
  339.     static int t[] = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4};
  340.     y -= m < 3;
  341.     return ((y + y / 4 - y / 100 + y / 400 + t[m - 1] + d) % 7);
  342. }
  343.  
  344. //compares the current date with another date and returns -1 for lower
  345. //, 0 for equal and 1 for greater
  346. int Date::Compare(const Date &d)
  347. {
  348.     int d1, m1, y1, d2, m2, y2;
  349.     d1 = day;
  350.     m1 = month;
  351.     y1 = year;
  352.  
  353.     d2 = d.day;
  354.     m2 = d.month;
  355.     y2 = d.year;
  356.  
  357.     if (y1 > y2)
  358.     {
  359.         return 1;
  360.     }
  361.     else if (y1 < y2)
  362.     {
  363.         return -1;
  364.     }
  365.     else
  366.     {
  367.         if (m1 > m2)
  368.         {
  369.             return 1;
  370.         }
  371.         else if (m1 < m2)
  372.         {
  373.             return -1;
  374.         }
  375.         else
  376.         {
  377.             if (d1 > d2)
  378.             {
  379.                 return 1;
  380.             }
  381.             else if (d1 < d2)
  382.             {
  383.                 return -1;
  384.             }
  385.             else
  386.             {
  387.                 return 0;
  388.             }
  389.         }
  390.     }
  391. }
  392.  
  393. //prints out the weekday with the month, day, and year.
  394. void Date::ShowByDay()
  395. {
  396.     //usese the day of the week member function to accurately get the right answer
  397.     switch (Date::DayofWeek())
  398.     {
  399.         case 0:
  400.             cout << "Sunday " << month << "/" << day << "/" << year;
  401.             break;
  402.         case 1:
  403.             cout << "Monday " << month << "/" << day << "/" << year;
  404.             break;
  405.         case 2:
  406.             cout << "Tuesday " << month << "/" << day << "/" << year;
  407.             break;
  408.         case 3:
  409.             cout << "Wednesday " << month << "/" << day << "/" << year;
  410.             break;
  411.         case 4:
  412.             cout << "Thursday " << month << "/" << day << "/" << year;
  413.             break;
  414.         case 5:
  415.             cout << "Friday " << month << "/" << day << "/" << year;
  416.             break;
  417.         case 6:
  418.             cout << "Saturday " << month << "/" << day << "/" << year;
  419.             break;
  420.     }
  421.     cout << "\n";
  422. }
  423.  
  424. //shows the date by the month and all its days
  425. void Date::ShowByMonth()
  426. {
  427.  
  428.     int y = year;
  429.     //uses a temp date so it can accurately calculate how many
  430.     //spaces is needed for the month
  431.     Date temp(month, 1, year);
  432.     //number of spaces is equal to the week day number
  433.     int numspace = temp.DayofWeek();
  434.     bool leapyear = false;
  435.     const string space = "      ";
  436.     const string HEADER = "Su    Mo    Tu    We    Th    Fr    Sa";
  437.     cout << HEADER << endl;
  438.  
  439.  
  440.     int century;
  441.     //needed for leap year
  442.     if (y % 1000 == 0)
  443.     {
  444.         century = y;
  445.     }
  446.     if (y % 4 == 0 || century % 400 == 0)
  447.     {
  448.         leapyear = true;
  449.     }
  450.  
  451.     switch (month)
  452.     {
  453.         //for these months they end in 31 days so the space is caluclated and
  454.         //modulus is used so it wraps around correctly;
  455.         case 1:
  456.         case 3:
  457.         case 5:
  458.         case 7:
  459.         case 8:
  460.         case 10:
  461.         case 12:
  462.             for (int i = 1, wrap = (i + numspace); i <= 31; i++)
  463.             {
  464.                 if (i == 1)
  465.                 {
  466.                     for (int j = 0; j < numspace; j++)
  467.                     {
  468.                         cout << space;
  469.                     }
  470.                 }
  471.                 if (i < 10)
  472.                 {
  473.                     cout << "0" << i << "    ";
  474.                 }
  475.                 else if (i > 10 && i < 20)
  476.                 {
  477.                     cout << i << "    ";
  478.                 }
  479.                 else if (i > 20 && i < 30)
  480.                 {
  481.                     cout << i << "    ";
  482.                 }
  483.                 else
  484.                 {
  485.                     cout << i << "    ";
  486.                 }
  487.  
  488.                 if (wrap % 7 == 0)
  489.                 {
  490.                     cout << "\n";
  491.                 }
  492.                 wrap++;
  493.             }
  494.             break;
  495.         //february needs its own because it has a leap year that needs to be accounted
  496.         //for, so thats in a vairable that changes the number it goes to based
  497.         //if its a leap year or not.
  498.         case 2:
  499.             int leap;
  500.             if (leapyear == true)
  501.             {
  502.                 leap = 29;
  503.             }
  504.             else
  505.             {
  506.                 leap = 28;
  507.             }
  508.  
  509.             for (int i = 1, wrap = (i + numspace); i <= leap; i++)
  510.             {
  511.                 if (i == 1)
  512.                 {
  513.                     for (int j = 0; j < numspace; j++)
  514.                     {
  515.                         cout << space;
  516.                     }
  517.                 }
  518.  
  519.                 if (i < 10)
  520.                 {
  521.                     cout << "0" << i << "    ";
  522.                 }
  523.                 else if (i > 10 && i < 20)
  524.                 {
  525.                     cout << i << "    ";
  526.                 }
  527.                 else if (i > 20 && i < 30)
  528.                 {
  529.                     cout << i << "    ";
  530.                 }
  531.                 else
  532.                 {
  533.                     cout << i << "    ";
  534.                 }
  535.  
  536.                 if (wrap % 7 == 0)
  537.                 {
  538.                     cout << "\n";
  539.                 }
  540.                 wrap++;
  541.             }
  542.             break;
  543.         //these months end in 30 days
  544.         case 4:
  545.         case 6:
  546.         case 11:
  547.             for (int i = 1, wrap = (i + numspace); i <= 30; i++)
  548.             {
  549.                 if (i == 1)
  550.                 {
  551.                     for (int j = 0; j < numspace; j++)
  552.                     {
  553.                         cout << space;
  554.                     }
  555.                 }
  556.                 if (i < 10)
  557.                 {
  558.                     cout << "0" << i << "    ";
  559.                 }
  560.                 else if (i > 10 && i < 20)
  561.                 {
  562.                     cout << i << "    ";
  563.                 }
  564.                 else if (i > 20 && i < 30)
  565.                 {
  566.                     cout << i << "    ";
  567.                 }
  568.                 else
  569.                 {
  570.                     cout << i << "    ";
  571.                 }
  572.  
  573.                 if (wrap % 7 == 0)
  574.                 {
  575.                     cout << "\n";
  576.                 }
  577.                 wrap++;
  578.                 break;
  579.             }
  580.     }
  581.     cout << endl;
  582. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement