I need some help with prefix and postfix.
I had a program which basically takes in a number 1 - 365 and converts it into month and day in which i had no problem. Now this is a second program which takes in a month and a day and convert it to a number 1 - 365 (opposite of the other program). The task says to use prefix++ and postfix++ (which i dont know anything about) to solve it. Please give me at least some idea how solve this problem
Here is the program that i have previously written ( What should be added to retrieve the number according to the entered month and day?? ):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
|
//Main.cpp
#include <iostream>
#include <cstdlib>
#include "DayOfYear.h"
using namespace std;
int main()
{
int _day;
// choose a day to print
cout << "Please enter a number from 1 to 365" << endl;
cin >> _day;
DayOfYear d(_day);
// print day 78 in Month and Day format
d.print();
system("pause");
return 0;
}
//DayOfYear.h
#include <iostream>
#ifndef _MYCLASS_
#define _MYCLASS_
using namespace std;
class DayOfYear{
public:
int day;
string Month;
DayOfYear(int dayEntered);
void print();
};
#endif
//DayOfYear.cpp
#include "DayOfYear.h"
DayOfYear::DayOfYear(int dayEntered){
day = dayEntered;
};
void DayOfYear::print()
{
if(day >= 1 && day <= 31)
{
cout << "January " << day << endl;
}
if(day >= 32 && day <= 59)
{
cout << "February " << (day - 31) << endl;
}
if(day >= 60 && day <= 90)
{
cout << "March " << (day - 59) << endl;
}
if(day >= 91 && day <= 120)
{
cout << "April " << (day - 90) << endl;
}
if(day >= 121 && day <= 151)
{
cout << "May " << (day - 120) << endl;
}
if(day >= 152 && day <= 181)
{
cout << "June " << (day - 151) << endl;
}
if(day >= 182 && day <= 212)
{
cout << "July " << (day - 181) << endl;
}
if(day >= 213 && day <= 243)
{
cout << "August " << (day - 212) << endl;
}
if(day >= 244 && day <= 273)
{
cout << "September " << (day - 243) << endl;
}
if(day >= 274 && day <= 304)
{
cout << "October " << (day - 273) << endl;
}
if(day >= 305 && day <= 334)
{
cout << "November " << (day - 304) << endl;
}
if(day >= 335 && day <= 365)
{
cout << "December " << (day - 334) << endl;
}
}
|
Task:
Modify the DayOfYear class, written in Programming Challenge 2, to add a constructor
that takes two parameters: a string representing a month and an integer in the range 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 constructor should terminate the program with an appropriate error
message if the number entered for a day is outside the range of days for the month given.
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 will represent the last day of the year.