Previous code #include <iostream> #include <string> using namespace std; //Day o
ID: 3702498 • Letter: P
Question
Previous code
#include <iostream>
#include <string>
using namespace std;
//Day of the year class declaration
class DayOfYear
{
private:
int day;
static int MonthDays[];
static string MonthName[];
public:
int getDay()
{
return day;
}
void operator++(int);
DayOfYear operator--(int);
DayOfYear(){}
DayOfYear(int day)
{
this->day = day;
}
DayOfYear(string month,int date)
{
int i;
for( i = 0 ; i < 12 ; i++)
{
if(MonthName[i] == month)
break;
}
day = MonthDays[i] + date;
}
void print();
};
int DayOfYear::MonthDays[] = {31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365};
//Set the name of each month into an array
string DayOfYear::MonthName[] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
//**************************************************
// This function displays the month and day using *
// the number entered. *
//**************************************************
void DayOfYear::operator++(int)
{
this->day = (this->day%365 + 1);
}
DayOfYear DayOfYear::operator--(int)
{
if(day == 1)
day = 365;
else
day = day - 1;
return DayOfYear(day);
}
void DayOfYear::print()
{
int month = 0;
while (MonthDays[month] < day)
month = (month + 1) %12;
//Display month and day
cout << MonthName[month] << " ";
if(month == 0)
cout<<day<<endl;
else
cout<< day - MonthDays[month-1]<<endl;
}
int main()
{
//Set days of each month into an array
char c;
do{
cout<<"1. Day to Date 2. Date to Day ";
int n;
cin>> n;
switch(n)
{
case 1 :
{int day;
//Ask user the total day number
cout << " Enter a number you would like to convert into a month and day";
cin >> day;
DayOfYear dYear(day);
//Error check for negative numbers and numbers higher than one year
if(day <= 0 || day > 365)
{
cout << "You must enter a valid number (1 thru 365)" << endl;
}
else
dYear.print();
dYear++;
cout<<"After ++"<<endl;
dYear.print();
cout<<"After --"<<endl;
dYear--;
dYear.print();
}
break;
case 2 :
{string month; int date;
cout<<" Enter month:"<<endl;
cin>>month;
cout<<" Enter date:"<<endl;
cin>>date;
DayOfYear dYear1(month,date);
cout<<"Day:"<<dYear1.getDay()<<endl;}
break;
}
cout<<" Do you wish to continue(y/n)?"<<endl;
cin>>c;
}while(c== 'y' || c == 'Y');
system("pause");
return 0;
}
Section 12.7 (pages 805-810) and Section 11.2 (pages 681- 688). Run your code, submit the output and code too. Submit code in a separate, compliable file, do NOT include it in your pdf or text file IMPORTANT: Write a main function that tests your code, and provide data in your main function. Do NOT get the input from the user even if the question says so. 1. Programming challenges 11.3, Day of the Year Modification, page 768. [2 points 2. Programming challenges 11.7, Corporate Sales, page 769. [2 points] 3. Day of the Year Modification Modify the DayofYear class, written in an earlier Programming Challenge, to add a con- structor that takes two parameters: 0 through 31 representing the day of the month. The constructor should then initialize the integer member of the class to represent the day specified by the month and day of month parameters. The sage if the number entered for a day is outside the range of days for the month given. a string representing a month and an integer in the range constructor should terminate the program with an appropriate error mes- Add the following overloaded operators: ++ prefix and postfix increment operators. These operators should modify the DayofYear object so that it represents the next day. If the day is already the end of the year, the new value of the object will represent the first day of the year prefix and postfix decrement operators. These operators should modify the DayofYear object so that it represents the previous day. If the day is already the first day of the year, the new value of the object wi Il represent the last day of the yearExplanation / Answer
/*The program has been modified as per the requirement as no input should be taken from the user a test data has been taken to show the out put of the program and using the test data the output whcih is obtained is shown after the program*/
#include <iostream>
#include <string>
using namespace std;
//Day of the year class declaration
class DayOfYear
{
private:
int day;
static int MonthDays[];
static string MonthName[];
public:
int getDay()
{
return day;
}
void operator++(int);
DayOfYear operator--(int);
DayOfYear(){}
DayOfYear(int day)
{
this->day = day;
}
DayOfYear(string month,int date)
{
int i;
for( i = 0 ; i < 12 ; i++)
{
if(MonthName[i] == month)
break;
}
day = MonthDays[i] + date;
}
void print();
};
int DayOfYear::MonthDays[] = {31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365};
//Set the name of each month into an array
string DayOfYear::MonthName[] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
//**************************************************
// This function displays the month and day using *
// the number entered. *
//**************************************************
void DayOfYear::operator++(int)
{
this->day = (this->day%365 + 1);
}
DayOfYear DayOfYear::operator--(int)
{
if(day == 1)
day = 365;
else
day = day - 1;
return DayOfYear(day);
}
void DayOfYear::print()
{
int month = 0;
while (MonthDays[month] < day)
month = (month + 1) %12;
//Display month and day
cout << MonthName[month] << " ";
if(month == 0)
cout<<day<<endl;
else
cout<< day - MonthDays[month-1]<<endl;
}
int main()
{
//Set days of each month into an array
char c;
int n;
n = 2;
int day;
day = 365;
DayOfYear dYear(day);
//Error check for negative numbers and numbers higher than one year
if(day <= 0 || day > 365)
{
cout << "You must enter a valid number (1 thru 365)" << endl;
}
else
dYear.print();
dYear++;
cout<<"After ++"<<endl;
dYear.print();
cout<<"After --"<<endl;
dYear--;
dYear.print();
string month; int date;
month = 5;
date = 140;
DayOfYear dYear1(month,date);
cout<<"Day:"<<dYear1.getDay()<<endl;
return 0;
}
Output:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.