Modify the Date class in Programming Challenge 1 of Chapter 13. The new version
ID: 3684120 • Letter: M
Question
Modify the Date class in Programming Challenge 1 of Chapter 13. The new version should have the following overloaded operators: ++ Prefix and postfix increment operators. These operators should increment the object's day member. Prefix and postfix decrement operators. These operators should decrement the object's day member. Subtraction operator. If one Date object is subtracted from another, the operator should give the number of days between the two dates. For example, if April 10, 2014 is subtracted from April 18, 2014, the result will be 8. << cout’s stream insertion operator. This operator should cause the date to be dis- played in the form April 18, 2014 >> cin’s stream extraction operator. This operator should prompt the user for a date to be stored in a Date object. The class should detect the following conditions and handle them accordingly: • When a date is set to the last day of the month and incremented, it should become the first day of the following month. • When a date is set to December 31 and incremented, it should become January 1 of the following year. • When a day is set to the first day of the month and decremented, it should become the last day of the previous month. • When a date is set to January 1 and decremented, it should become December 31 of the previous year. Demonstrate the class’s capabilities in a simple program. Input Validation: The overloaded >> operator should not accept invalid dates. For example, the date 13/45/2014 should not be accepted.
question of chapter 13 Q.1 is below
Design a class called Date. The class should store a date in three integers: month, day, and year. There should be member functions to print the date in the following forms:
12/25/2014 December 25, 2014 25 December 2014
Demonstrate the class by writing a complete program implementing it.
Input Validation: Do not accept values for the day greater than 31 or less than 1. Do not accept values for the month greater than 12 or less than 1.
Explanation / Answer
#include "Date.h"
#include <iostream>
int numOfDays(bool leap, int day, int month);
string getNameOfMonth(int i);
string getDay(int tday, int month, int year);
Date Date::operator++()
{
bool leap;
++day;
if ( (year % 4 == 0 && year % 100 != 0) || ( year % 400 == 0))
leap = true;
else
leap = false;
if(month == 2)
{
if(leap == true)
{
if(day > 29)
{
//day = 1;
++month;
}
}
else
{
if(day > 28){
//day = 1;
++month;
if(month > 12){
month = 1;
}
}
}
}
else if(month == 9 || month == 4 || month == 6 || month == 11)
{
if(day > 30){
day = 1;
++month;
}
}
else
{
if(day > 31){
day = 1;
++month;
if(month > 12){
month = 1;
++year;
}
}
}
return *this;
}
Date Date::operator++(int)
{
bool isLeap;
Date temp(month,day,year);
day++;
if ( (year % 4 == 0 && year % 100 != 0) || ( year % 400 == 0))
isLeap = true;
else
isLeap = false;
if(month == 2)
{
if(isLeap == true)
{
if(day > 29)
{
month++;
}
}
else{
if(day > 28)
{
month++;
if(month > 12)
{
month = 1;
}
}
}
}
else if( month == 4 || month == 6 || month == 9 || month == 11)
{
if(day > 30)
{
month++;
}
}
else{
if(day > 31)
{
month++;
if(month > 12)
{
year++;
}
}
}
return temp;
}
Date Date::operator--()
{
bool isLeap;
--day;
if ( (year % 4 == 0 && year % 100 != 0) || ( year % 400 == 0))
isLeap = true;
else
isLeap = false;
if(month == 3)
{
if(isLeap == true)
{
if(day < 1)
{
day = 29;
--month;
}
}
else
{
if(day < 1)
{
day = 28;
--month;
}
}
}
else if (month == 1)
{
if(day < 1)
{
day = 31;
month = 12;
--year;
}
}
else if(month == 4 || month == 6 || month == 9 || month == 11 || month == 8)
{
if(day < 1)
{
day = 31;
--month;
}
}
else if(month == 10 || month == 5 || month == 7 || month == 12)
{
if(day < 1)
{
day = 30;
--month;
}
}
return *this;
}
Date Date::operator--(int)
{
bool isLeap;
Date temp(month,day,year);
day--;
if ( (year % 4 == 0 && year % 100 != 0) || ( year % 400 == 0))
isLeap = true;
else
isLeap = false;
if(month == 3)
{
if(isLeap == true)
{
if(day < 1)
{
day = 29;
--month;
}
}
else
{
if(day < 1)
{
day = 28;
--month;
}
}
}
else if (month == 1)
{
if(day < 1)
{
day = 31;
month = 12;
--year;
}
}
else if(month == 4 || month == 6 || month == 9 || month == 11 || month == 8)
{
if(day < 1)
{
day = 31;
--month;
}
}
else if(month == 10 || month == 5 || month == 7 || month == 12)
{
if(day < 1)
{
day = 30;
--month;
}
}
return temp;
}
int Date::operator-(const Date &right)
{
bool isLeap1,isLeap2;
int tempCount = 0,rightCount=0;
int year2 = right.year;
if ( (year % 4 == 0 && year % 100 != 0) || ( year % 400 == 0))
isLeap1 = true;
else
isLeap1 = false;
if ( (year2 % 4 == 0 && year2 % 100 != 0) || ( year2 % 400 == 0))
isLeap2 = true;
else
isLeap2 = false;
if ( month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
{
tempCount = year*365 + month*31 + day;
rightCount = right.year*365 + right.month*31 + right.day;
}
else if(month == 4 || month == 6 || month == 9 || month == 11)
{
tempCount = year*365 + month*30 + day;
rightCount = right.year*365 + right.month*30 + right.day;
}
else if(month == 2)
{
if(isLeap1 == true || isLeap2 == true)
{
tempCount = year*366 + month*29 + day;
rightCount = right.year*366 + right.month*29 + right.day;
}
else if(isLeap1 == false || isLeap2 == false)
{
tempCount = year*365 + month*28 + day;
rightCount = right.year*365 + right.month*28 + right.day;
}
}
tempCount -= rightCount;
return tempCount;
}
bool Date::operator>(const Date &right)
{
bool isGreater = true;
if(year > right.year)
{
isGreater;
}
else if( year == right.year)
{
if(month > right.month)
{
isGreater;
}
else if(month == right.month)
{
if(day > right.day)
{
isGreater;
}
}
}
else
{
isGreater = false;
}
return isGreater;
}
bool Date::operator<(const Date &right)
{
bool isGreater = true;
if(year < right.year)
{
isGreater;
}
else if( year == right.year)
{
if(month < right.month)
{
isGreater;
}
else if(month == right.month)
{
if(day < right.day)
{
isGreater;
}
}
}
else
{
isGreater = false;
}
return isGreater;
}
ostream &operator << (ostream &strm, const Date &obj)
{
string month, year, day;
int mNum = obj.month;
int yNum = obj.year;
int dNum = obj.day;
month = getNameOfMonth(mNum); //Get name of month
day = getDay(dNum, mNum, yNum);
strm << day << ", " << month << " " << obj.getDay() << ", " << obj.getYear();
return strm;
}
istream &operator >> (istream &strm, Date &obj)
{
bool isLeap;
cout << "Enter a year: ";
strm >> obj.year;
while (obj.year < 1582 || obj.year > 2999)
{
cout << "Invalid year! " << endl;
cout << "Enter a year between 1582 and 2999: ";
strm >> obj.year;
}
int year = obj.year;
if ( (year % 4 == 0 && year % 100 != 0) || ( year % 400 == 0))
isLeap = true;
else
isLeap = false;
cout << "Enter a month: ";
strm >> obj.month;
while( obj.month < 1 || obj.month > 12 )
{
cout << "Invalid month!" << endl;
cout << "Enter a month between 1 and 12: ";
strm >> obj.month;
}
int month = obj.month;
cout << "Enter a day: ";
strm >> obj.day;
if (obj.month == 2)
{
if (isLeap == true)
{
while( obj.day < 1 || obj.day > 29){
cout << "This is a leap year.Enter a day (1 - 29) :" << endl;
strm >> obj.day;
}
}
else{
while (obj.day < 1 || obj.day > 28){
cout << "Enter a day (1-28)" << endl;
cout << "Please enter a day: ";
strm >> obj.day;
}
}
}
else if ( obj.month == 1 || obj.month == 3 || obj.month == 5 || obj.month == 7 || obj.month == 8 || obj.month == 10 || obj.month == 12)
{
while (obj.day > 31 || obj.day < 1){
cout << "The day should be between 1 and 31" << endl;
cout << "Enter a day: ";
strm >> obj.day;
}
}
else if (obj.month == 4 || obj.month == 6 || obj.month == 9 || obj.month == 11)
{
cout << "Enter a day: ";
strm >> obj.day;
while (obj.day > 30 || obj.day < 1){
cout << "The day should be between 1 and 30" << endl;
cout << "Please enter a day: ";
strm >> obj.day;
}
}
return strm;
}
int numOfDays(bool leap, int day, int month)
{
int tempCount = day, tempMonth = month;
while(tempMonth > 1)
{
if(tempMonth == 2)
{
if(leap == true){
tempCount += 29;
tempMonth--;
}
else
{
tempCount += 28;
tempMonth--;
}
}
else if(tempMonth == 4 || tempMonth == 6 || tempMonth == 9 || tempMonth == 11){
tempCount += 30;
tempMonth--;
}
else if(tempMonth == 1)
{
}
else
{
tempCount += 31;
tempMonth--;
}
}
return tempCount;
}
string getNameOfMonth(int i){
string month;
switch(i)
{
case 1:
month = "January";
break;
case 2:
month = "February";
break;
case 3:
month = "March";
break;
case 4:
month = "April";
break;
case 5:
month = "May";
break;
case 6:
month = "June";
break;
case 7:
month = "July";
break;
case 8:
month = "August";
break;
case 9:
month = "September";
break;
case 10:
month = "October";
break;
case 11:
month = "November";
break;
case 12:
month = "December";
break;
default:
cout << "value unknown";
}
return month;
}
string getDay(int d, int month, int year){
int mCommonYear[12] = { 3, 28, 7, 4, 9, 6, 11, 8, 5, 10, 7, 12 };
int monthsLeap[12] = { 4, 29, 7, 4, 9, 6, 11, 8, 5, 10, 7, 12 };
string daysOfWeek[7] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };
int lastTwoDigits =year%100;
int a = lastTwoDigits/12;
int b = lastTwoDigits %12;
int c = b/4;
int i = a+ b +c;
int anchor = 0;
int day;
int y = year %400;
if (y < 100){
anchor = 3;
}
else if (y < 200){
anchor = 1;
}
else if (y < 300){
anchor = 6;
}
else{
anchor = 4;
}
int doomsday = i + anchor;
int x = 0;
while (doomsday > 7){
doomsday = doomsday - 7;
}
if (month > 2){
x = (d + 14) - mCommonYear[month - 1];
day = doomsday + x;
while (day > 7){
day = day - 7;
}
}
else{
if (((year % 400) != 0) && ((year % 4) != 0))
{
x = (d + 35) - mCommonYear[month - 1];
day = doomsday + x;
while (day > 7)
{
day = day - 7;
}
}
else{
x = (d + 35) - monthsLeap[month - 1];
day = doomsday + x;
while (day > 7)
{
day = day - 7;
}
}
}
return daysOfWeek[day - 1];
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.