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 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
|
#include <string>
#include <iostream>
#include <vector>
using namespace std;
class Month
{
public:
enum MonthEnum
{
JANUARY=1, FEBRUARY, MARCH, APRIL, MAY, JUNE,
JULY, AUGUST, SEPTEMBER, OCTOBER, NOVEMBER, DECEMBER
};
Month() : month_(MonthEnum::JANUARY)
{
}
Month(MonthEnum m) : month_(m)
{
}
Month(int m) : month_((MonthEnum)m)
{
}
bool operator==(MonthEnum m)
{
return month_ == m;
}
Month& operator++() // Pre
{
if (month_ == MonthEnum::DECEMBER)
month_ = MonthEnum::JANUARY;
else
month_ = (MonthEnum)((int)month_ + 1);
return *this;
}
Month operator++(int) // Post
{
Month tmp(*this);
operator++();
return tmp;
}
Month& operator--() // Pre
{
if (month_ == MonthEnum::JANUARY)
month_ = MonthEnum::DECEMBER;
else
month_ = (MonthEnum)((int)month_ - 1);
return *this;
}
Month operator--(int) // Post
{
Month tmp(*this);
operator--();
return tmp;
}
int Numeric() const
{
return (int)month_;
}
string str() const
{
return strings_[(int)month_-1];
}
private:
MonthEnum month_;
static const vector<string> strings_;
};
const vector<string> Month::strings_ = {"January", "February", "March", "April",
"May", "June", "July", "August", "September", "October", "November", "December"};
ostream& operator<<(ostream& os, const Month& m)
{
os << m.str();
return os;
}
class DayOfYear
{
public:
DayOfYear& operator++()
{
day_++;
if(day_ == 29 && month_ == Month::MonthEnum::FEBRUARY)
{
day_ = 1;
month_ == Month::MonthEnum::MARCH;
}
else if (day_ == 31)
{
if(month_ == Month::MonthEnum::APRIL ||
month_ == Month::MonthEnum::JUNE ||
month_ == Month::MonthEnum::SEPTEMBER ||
month_ == Month::MonthEnum::NOVEMBER)
{
day_ = 1;
month_++;
}
}
else if (day_ == 32)
{
day_ = 1;
month_++;
}
return *this;
}
DayOfYear operator++(int)
{
DayOfYear tmp(*this);
operator++();
return tmp;
}
DayOfYear& operator--()
{
day_--;
if(day_ == 0)
{
if (month_ == Month::MonthEnum::MARCH)
{
day_ = 28;
month_--;
}
else if(month_ == Month::MonthEnum::MAY ||
month_ == Month::MonthEnum::JULY ||
month_ == Month::MonthEnum::OCTOBER ||
month_ == Month::MonthEnum::DECEMBER)
{
day_ = 30;
month_--;
}
else
{
day_ = 31;
month_--;
}
}
return *this;
}
DayOfYear operator--(int)
{
DayOfYear tmp(*this);
operator--();
return tmp;
}
DayOfYear(Month month, int day) : month_(month), day_(day)
{
}
void Show()
{
cout << month_ << " " << day_ << endl;
}
private:
Month month_;
unsigned int day_;
};
int main()
{
DayOfYear doy(Month::MonthEnum::APRIL, 30);
doy.Show();
doy++;
cout << "\nHere is the post-incremented date: " << endl;
doy.Show();
cout << "\nAnother date:" << endl;
DayOfYear doy2(Month::MonthEnum::DECEMBER, 1);
doy2.Show();
--doy2;
cout << "Decremented:"<< endl;
doy2.Show();
cout << endl;
return 0;
}
|