Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- using namespace std;
- class Date {
- private:
- unsigned day;
- unsigned month;
- string monthName;
- unsigned year;
- public:
- // creates the date January 1st, 2000.
- Date();
- /* parameterized constructor: month number, day, year
- - e.g. (3, 1, 2010) will construct the date March 1st, 2010
- If any of the arguments are invalid (e.g. 15 for month or 32 for day)
- then the constructor will construct instead a valid Date as close
- as possible to the arguments provided - e.g. in above example,
- Date(15, 32, 2010), the Date would be corrected to Dec 31st, 2010.
- In case of such invalid input, the constructor will issue a console error message:
- Invalid date values: Date corrected to 12/31/2010.
- (with a newline at the end).
- */
- Date(unsigned m, unsigned d, unsigned y);
- /* parameterized constructor: month name, day, year
- - e.g. (December, 15, 2012) will construct the date December 15th, 2012
- If the constructor is unable to recognize the string argument as a valid month name,
- then it will issue a console error message:
- Invalid month name: the Date was set to 1/1/2000.
- (with a newline at the end).
- If the day argument is invalid for the given month (but the month name was valid),
- then the constructor will handle this error in the same manner as the other
- parameterized constructor.
- This constructor will recognize both "december" and "December"
- as month name.
- */
- Date(const string &mn, unsigned d, unsigned y);
- /* Outputs to the console (cout) a Date exactly in the format "3/15/2012".
- Does not output a newline at the end.
- */
- void printNumeric() const;
- /* Outputs to the console (cout) a Date exactly in the format "March 15, 2012".
- The first letter of the month name is upper case, and the month name is
- printed in full - January, not Jan, jan, or january.
- Does not output a newline at the end.
- */
- void printAlpha() const;
- private:
- /* Returns true if the year passed in is a leap year, otherwise returns false.
- */
- bool isLeap(unsigned y) const;
- /* Returns number of days allowed in a given month
- - e.g. daysPerMonth(9, 2000) returns 30.
- Calculates February's days for leap and non-leap years,
- thus, the reason year is also a parameter.
- */
- unsigned daysPerMonth(unsigned m, unsigned y) const;
- /* Returns the name of a given month
- - e.g. name(12) returns the string "December"
- */
- string name(unsigned m) const;
- /* Returns the number of a given named month
- - e.g. number("March") returns 3
- */
- unsigned number(const string &mn) const;
- };
- // Implement the Date member functions here
- //Private Helper functions
- bool Date::isLeap(unsigned y) const {
- bool leap = false;
- if(y % 4 == 0) {
- if(y % 100 == 0) {
- if(y % 400 == 0) {
- leap = true;
- }
- } else {
- leap = true;
- }
- }
- return leap;
- }
- unsigned Date::daysPerMonth(unsigned m, unsigned y) const{
- unsigned int dayNum = 0;
- if(m == 1 || m == 3 || m == 5 ||
- m == 7 || m == 8 || m == 10 ||
- m == 12) {
- dayNum = 31;
- } else if (m == 2) {
- if(isLeap(y)) {
- dayNum = 29;
- } else {
- dayNum = 28;
- }
- } else {
- dayNum = 30;
- }
- return dayNum;
- }
- string Date::name(unsigned m) const {
- string name;
- if(m == 1) {
- name = "January";
- } else if(m == 2) {
- name = "February";
- } else if(m == 3) {
- name = "March";
- } else if(m == 4) {
- name = "April";
- } else if(m == 5) {
- name = "May";
- } else if(m == 6) {
- name = "June";
- } else if(m == 7) {
- name = "July";
- } else if(m == 8) {
- name = "August";
- } else if(m == 9) {
- name = "September";
- } else if(m == 10) {
- name = "October";
- } else if(m == 11) {
- name = "November";
- } else if(m == 12) {
- name = "December";
- }
- return name;
- }
- unsigned Date::number(const string &mn) const {
- unsigned number;
- if(mn == "January") {
- number = 1;
- } else if (mn == "February") {
- number = 2;
- } else if (mn == "March") {
- number = 3;
- } else if (mn == "April") {
- number = 4;
- } else if (mn == "May") {
- number = 5;
- } else if (mn == "June") {
- number = 6;
- } else if (mn == "July") {
- number = 7;
- } else if (mn == "August") {
- number = 8;
- } else if (mn == "September") {
- number = 9;
- } else if (mn == "October") {
- number = 10;
- } else if (mn == "November") {
- number = 11;
- } else if (mn == "December") {
- number = 12;
- } else {
- number = -1;
- }
- return number;
- }
- // Constructors
- Date::Date() {
- day = 1;
- month = 1;
- monthName = "January";
- year = 2000;
- }
- Date::Date(unsigned m, unsigned d, unsigned y) {
- bool incorrect = false;
- if(m > 12) {
- incorrect = true;
- m = 12;
- } else if (m < 1) {
- incorrect = true;
- m = 1;
- }
- if(d < 1) {
- incorrect = true;
- d = 1;
- } else if (d > daysPerMonth(m, y)){
- d = daysPerMonth(m, y);
- incorrect = true;
- } else if (m == 2) {
- if(isLeap(y)) {
- if(d != 29) {
- incorrect = true;
- d = 29;
- }
- } else {
- if(d != 28) {
- incorrect = true;
- d = 28;
- }
- }
- }
- if(incorrect) {
- cout << "Invalid date values: Date corrected to " << m
- << "/" << d << "/" << y << "." << endl;
- }
- month = m;
- day = d;
- monthName = name(m);
- year = y;
- }
- Date::Date(const string &mn, unsigned d, unsigned y) {
- bool incorrect = false;
- string x = mn;
- x[0] = toupper(x[0]);
- int m = number(x);
- if(m == -1) {
- day = 1;
- month = 1;
- monthName = "January";
- year = 2000;
- cout << "Invalid month name: the Date was set to 1/1/2000." << endl;
- } else {
- if(d < 1) {
- incorrect = true;
- d = 1;
- } else if (d > daysPerMonth(m, y)){
- d = daysPerMonth(m, y);
- incorrect = true;
- } else if (m == 2) {
- if(isLeap(y)) {
- if(d != 29) {
- incorrect = true;
- d = 29;
- }
- } else {
- if(d != 28) {
- incorrect = true;
- d = 28;
- }
- }
- }
- month = m;
- day = d;
- monthName = x;
- year = y;
- if(incorrect) {
- cout << "Invalid date values: Date corrected to " << m
- << "/" << d << "/" << y << "." << endl;
- }
- }
- }
- void Date::printNumeric() const {
- cout << month << "/" << day << "/" << year;
- }
- void Date::printAlpha() const {
- cout << monthName << " " << day << ", " << year;
- }
- // Don't change the code below this line.
- // You may comment them out if you want to build your own test harness
- // while in develope mode, but you will need these to pass tets in submit mode.
- Date getDate();
- int main() {
- Date testDate;
- testDate = getDate();
- cout << endl;
- cout << "Numeric: ";
- testDate.printNumeric();
- cout << endl;
- cout << "Alpha: ";
- testDate.printAlpha();
- cout << endl;
- return 0;
- }
- Date getDate() {
- int choice;
- unsigned monthNumber, day, year;
- string monthName;
- cout << "Which Date constructor? (Enter 1, 2, or 3)" << endl
- << "1 - Month Number" << endl
- << "2 - Month Name" << endl
- << "3 - default" << endl;
- cin >> choice;
- cout << endl;
- if (choice == 1) {
- cout << "month number? ";
- cin >> monthNumber;
- cout << endl;
- cout << "day? ";
- cin >> day;
- cout << endl;
- cout << "year? ";
- cin >> year;
- cout << endl;
- return Date(monthNumber, day, year);
- } else if (choice == 2) {
- cout << "month name? ";
- cin >> monthName;
- cout << endl;
- cout << "day? ";
- cin >> day;
- cout << endl;
- cout << "year? ";
- cin >> year;
- cout << endl;
- return Date(monthName, day, year);
- } else {
- return Date();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement