Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <iomanip>
- #include <string>
- #include <stdio.h>
- #include "date.h"
- using namespace std;
- Date::Date(int m, int d, int y)
- {
- //century used for if the year ends on a number divisble by 1000
- int century;
- bool error = false;
- //needed for leap year
- if (y % 1000 == 0)
- {
- century = y;
- }
- //Month error check
- if (m < 1 || m > 12)
- {
- error = true;
- }
- //Year error check
- if (y < 1900)
- {
- error = true;
- }
- //Day less than 0 Check
- if (d <= 0)
- {
- error = true;
- }
- //Error check for correct day for each month
- switch (m)
- {
- //if february calculate if its a leap year and allow 29 if not do not allow 29
- case 2:
- if (d > 28)
- {
- error = true;
- }
- if (d == 29 && (y % 4 == 0 || century % 400 == 0))
- {
- d = 29;
- }
- break;
- //cascade effect for months that end in 30
- case 4:
- case 6:
- case 11:
- if (d > 30)
- {
- error = true;
- }
- break;
- //cascade effect for months that end in 31
- case 1:
- case 3:
- case 5:
- case 7:
- case 8:
- case 10:
- case 12:
- if (d > 31)
- {
- error = true;
- }
- break;
- }
- //Assigning values to Private class
- //if the error is false than the values put into the constructor are
- //the private member functions
- if (error == false)
- {
- day = d;
- month = m;
- year = y;
- }
- //if the error is true than change the values to the defualt values
- else
- {
- day = 1;
- month = 1;
- year = 2019;
- }
- }
- //allows the user to input a date and see if its legal or not
- void Date::input()
- {
- //numbers are initalized so that if they don't enter it right it wont change and will results
- //in an error
- int m1 = -1, d2 = -1, y3 = -1, temp, temp2;
- string m, d, y;
- string date;
- bool illegal = false;
- //while loop so if you enter a bad value it prompt so enter again
- do
- {
- cout << "input date in month/day/year format: ";
- cin >> date;
- //if else blocks used for all the scenarios of two digit and one digit dates
- if (date[1] == '/')
- {
- m1 = '0' - date[0];
- m1 *= -1;
- }
- else
- {
- m = date[0] + date[1];
- temp = '0' - date[1];
- temp2 = '0' - date[0];
- m1 = temp2 * 10 + temp;
- m1 *= -1;
- }
- if (date[1] == '/' && date[3] == '/')
- {
- d2 = ('0' - date[2]) * -1;
- }
- else if (date[1] == '/' && date[4] == '/')
- {
- temp = '0' - date[2];
- temp2 = '0' - date[3];
- d2 = temp * 10 + temp2;
- d2 *= -1;
- }
- else if (date[2] == '/' && date[4] == '/')
- {
- d2 = ('0' - date[3]) * -1;
- }
- else if (date[2] == '/' && date[5] == '/')
- {
- temp = ('0' - date[3]);
- temp2 = '0' - date[4];
- d2 = temp2 + temp * 10;
- d2 *= -1;
- }
- //the year only matters where the slash is and there are 4 possible places after that the year
- //is always four digits, so i use exponential addition to add them
- if (date[3] == '/')
- {
- y3 = (('0' - date[4]) * 1000 + ('0' - date[5]) * 100 + ('0' - date[6]) * 10 + ('0' - date[7])) * -1;
- }
- else if (date[4] == '/')
- {
- y3 = (('0' - date[5]) * 1000 + ('0' - date[6]) * 100 + ('0' - date[7]) * 10 + ('0' - date[8])) * -1;
- }
- else if (date[5] == '/')
- {
- y3 = (('0' - date[6]) * 1000 + ('0' - date[7]) * 100 + ('0' - date[8]) * 10 + ('0' - date[9])) * -1;
- }
- Date iii(m1, d2, y3);
- if (iii.month == m1 || iii.day == d2 || iii.year == y3)
- {
- illegal = false;
- }
- else
- {
- illegal = true;;
- cout << "Invalid date. Try again" << endl;
- }
- }
- while (illegal == true);
- }
- //returns the month in number value
- int Date::GetMonth() const
- {
- return month;
- }
- //returns the dat in number value
- int Date::GetDay() const
- {
- return day;
- }
- //returns the year in number value
- int Date::GetYear() const
- {
- return year;
- }
- //same as the constructor, except if false, doesn't change anything
- bool Date::Set(int m, int d, int y)
- {
- int century;
- bool error;
- //needed for leap year
- if (y % 1000 == 0)
- {
- century = y;
- }
- //Month error check
- if (m < 1 || m > 12)
- {
- error = true;
- }
- //Year error check
- if (y < 1900)
- {
- error = true;
- }
- //Day less than 0 Check
- if (d <= 0)
- {
- error = true;
- }
- //Error check for correct day for each month
- switch (m)
- {
- case 2:
- if (d > 28)
- {
- error = true;
- }
- if (d == 29 && (y % 4 == 0 || century % 400 == 0))
- {
- d = 29;
- }
- break;
- case 4:
- case 6:
- case 11:
- if (d > 30)
- {
- error = true;
- }
- break;
- case 1:
- case 3:
- case 5:
- case 7:
- case 8:
- case 10:
- case 12:
- if (d > 31)
- {
- error = true;
- }
- break;
- }
- //Assigning values to Private class
- //if there is no error change private variables, if there is error dont change
- if (error == false)
- {
- day = d;
- month = m;
- year = y;
- return true;
- }
- else
- {
- return false;
- }
- }
- //increments the day by 1 and changes the month if the day goes over the month
- void Date::Increment()
- {
- int m, d, y;
- m = month;
- d = day;
- y = year;
- d++;
- int century;
- //needed for leap year
- if (y % 1000 == 0)
- {
- century = y;
- }
- //Error check for correct day for each month
- //Switch statement for different months because some months have different days
- switch (m)
- {
- case 2:
- if (d == 29 && (y % 4 == 0 || century % 400 == 0))
- {
- d = 29;
- }
- else if (d > 28)
- {
- m++;
- d = 1;
- }
- break;
- case 4:
- case 6:
- case 11:
- if (d > 30)
- {
- m++;
- d = 1;
- }
- break;
- case 1:
- case 3:
- case 5:
- case 7:
- case 8:
- case 10:
- case 12:
- if (d > 31)
- {
- //checks so if the month is 12, change hte month to 1 instead of 13
- if (m != 12)
- {
- m++;
- }
- else
- {
- m = 1;
- }
- d = 1;
- }
- break;
- }
- //Assigning values to Private class
- day = d;
- month = m;
- year = y;
- }
- //returns a value 0-6 0 being sunday and 6 being Friday
- int Date::DayofWeek()
- {
- //this function was given to us
- int d, y, m;
- y = year;
- m = month;
- d = day;
- static int t[] = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4};
- y -= m < 3;
- return ((y + y / 4 - y / 100 + y / 400 + t[m - 1] + d) % 7);
- }
- //compares the current date with another date and returns -1 for lower
- //, 0 for equal and 1 for greater
- int Date::Compare(const Date &d)
- {
- int d1, m1, y1, d2, m2, y2;
- d1 = day;
- m1 = month;
- y1 = year;
- d2 = d.day;
- m2 = d.month;
- y2 = d.year;
- if (y1 > y2)
- {
- return 1;
- }
- else if (y1 < y2)
- {
- return -1;
- }
- else
- {
- if (m1 > m2)
- {
- return 1;
- }
- else if (m1 < m2)
- {
- return -1;
- }
- else
- {
- if (d1 > d2)
- {
- return 1;
- }
- else if (d1 < d2)
- {
- return -1;
- }
- else
- {
- return 0;
- }
- }
- }
- }
- //prints out the weekday with the month, day, and year.
- void Date::ShowByDay()
- {
- //usese the day of the week member function to accurately get the right answer
- switch (Date::DayofWeek())
- {
- case 0:
- cout << "Sunday " << month << "/" << day << "/" << year;
- break;
- case 1:
- cout << "Monday " << month << "/" << day << "/" << year;
- break;
- case 2:
- cout << "Tuesday " << month << "/" << day << "/" << year;
- break;
- case 3:
- cout << "Wednesday " << month << "/" << day << "/" << year;
- break;
- case 4:
- cout << "Thursday " << month << "/" << day << "/" << year;
- break;
- case 5:
- cout << "Friday " << month << "/" << day << "/" << year;
- break;
- case 6:
- cout << "Saturday " << month << "/" << day << "/" << year;
- break;
- }
- cout << "\n";
- }
- //shows the date by the month and all its days
- void Date::ShowByMonth()
- {
- int y = year;
- //uses a temp date so it can accurately calculate how many
- //spaces is needed for the month
- Date temp(month, 1, year);
- //number of spaces is equal to the week day number
- int numspace = temp.DayofWeek();
- bool leapyear = false;
- const string space = " ";
- const string HEADER = "Su Mo Tu We Th Fr Sa";
- cout << HEADER << endl;
- int century;
- //needed for leap year
- if (y % 1000 == 0)
- {
- century = y;
- }
- if (y % 4 == 0 || century % 400 == 0)
- {
- leapyear = true;
- }
- switch (month)
- {
- //for these months they end in 31 days so the space is caluclated and
- //modulus is used so it wraps around correctly;
- case 1:
- case 3:
- case 5:
- case 7:
- case 8:
- case 10:
- case 12:
- for (int i = 1, wrap = (i + numspace); i <= 31; i++)
- {
- if (i == 1)
- {
- for (int j = 0; j < numspace; j++)
- {
- cout << space;
- }
- }
- if (i < 10)
- {
- cout << "0" << i << " ";
- }
- else if (i > 10 && i < 20)
- {
- cout << i << " ";
- }
- else if (i > 20 && i < 30)
- {
- cout << i << " ";
- }
- else
- {
- cout << i << " ";
- }
- if (wrap % 7 == 0)
- {
- cout << "\n";
- }
- wrap++;
- }
- break;
- //february needs its own because it has a leap year that needs to be accounted
- //for, so thats in a vairable that changes the number it goes to based
- //if its a leap year or not.
- case 2:
- int leap;
- if (leapyear == true)
- {
- leap = 29;
- }
- else
- {
- leap = 28;
- }
- for (int i = 1, wrap = (i + numspace); i <= leap; i++)
- {
- if (i == 1)
- {
- for (int j = 0; j < numspace; j++)
- {
- cout << space;
- }
- }
- if (i < 10)
- {
- cout << "0" << i << " ";
- }
- else if (i > 10 && i < 20)
- {
- cout << i << " ";
- }
- else if (i > 20 && i < 30)
- {
- cout << i << " ";
- }
- else
- {
- cout << i << " ";
- }
- if (wrap % 7 == 0)
- {
- cout << "\n";
- }
- wrap++;
- }
- break;
- //these months end in 30 days
- case 4:
- case 6:
- case 11:
- for (int i = 1, wrap = (i + numspace); i <= 30; i++)
- {
- if (i == 1)
- {
- for (int j = 0; j < numspace; j++)
- {
- cout << space;
- }
- }
- if (i < 10)
- {
- cout << "0" << i << " ";
- }
- else if (i > 10 && i < 20)
- {
- cout << i << " ";
- }
- else if (i > 20 && i < 30)
- {
- cout << i << " ";
- }
- else
- {
- cout << i << " ";
- }
- if (wrap % 7 == 0)
- {
- cout << "\n";
- }
- wrap++;
- break;
- }
- }
- cout << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement