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 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
|
#include<iostream>
#include<string>
#include <cstdlib>
using namespace std;
class DayOfYear
{
private:
static string month;
public:
int day;
DayOfYear();
DayOfYear(int d);
DayOfYear(string m, int d);
void print();
DayOfYear& operator++();
DayOfYear operator++(int);
DayOfYear& operator--();
DayOfYear operator--(int);
};
string DayOfYear::month;
//Overloaded Operators
DayOfYear& DayOfYear::operator++()
{
if (this->day == 365)
this->day = 1;
else
this->day++;
return *this;
}
DayOfYear DayOfYear::operator++(int)
{
DayOfYear tmp(*this);
operator++();
return tmp;
}
DayOfYear& DayOfYear::operator--()
{
if (this->day == 1)
this->day = 365;
else
this->day--;
return *this;
}
DayOfYear DayOfYear::operator--(int)
{
DayOfYear tmp(*this);
operator--();
return tmp;
}
DayOfYear::DayOfYear(string m, int d) {
month = m;
day = d;
if (m == "January" && d > 31) {
cout << "Invalid input, terminating program." << endl;
exit(0);
} else if (m == "February" && d > 28) {
cout << "Invalid input, terminating program." << endl;
exit(0);
} else if (m == "March" && d > 31) {
cout << "Invalid input, terminating program." << endl;
exit(0);
} else if (m == "April" && d > 30) {
cout << "Invalid input, terminating program." << endl;
exit(0);
} else if (m == "May" && d > 31) {
cout << "Invalid input, terminating program." << endl;
exit(0);
} else if (m == "June" && d > 30) {
cout << "Invalid input, terminating program." << endl;
exit(0);
} else if (m == "July" && d > 31) {
cout << "Invalid input, terminating program." << endl;
exit(0);
} else if (m == "August" && d > 31) {
cout << "Invalid input, terminating program." << endl;
exit(0);
} else if (m == "September" && d > 30) {
cout << "Invalid input, terminating program." << endl;
exit(0);
} else if (m == "October" && d > 31) {
cout << "Invalid input, terminating program." << endl;
exit(0);
} else if (m == "November" && d > 30) {
cout << "Invalid input, terminating program." << endl;
exit(0);
} else if (m == "December" && d > 31) {
cout << "Invalid input, terminating program." << endl;
exit(0);
}
cout << month << " " << day << endl;
}
DayOfYear::DayOfYear()
{
cout << "Enter the a day of the year (1-365), to print out the month and day\n"
"or enter a number <=0 to end:" << endl;
cin >> day;
print();
}
DayOfYear::DayOfYear(int d)
{
day = d;
while (d > 365 || d < 1) {
cout << "Day cannot be > 365 or < 1. " << endl;
cin >> d;
day = d;
}
print();
}
void DayOfYear::print() {
if( day > 365){
cout << "Invalid input, terminating program. Day is greater than 365." << endl;
exit(0);
}
else if( day < 1){
cout << "Invalid input, terminating program. Day is less than 1." << endl;
exit(0);
}
else if (day > 0 && day < 32)
{
month = "January";
cout << month << " " << 31 + day - 31 << endl;
}
else if (day > 31 && day < 60)
{
month = "February";
cout << month << " " << day - 31 << endl;
}
else if (day > 59 && day < 91)
{
month = "March";
cout << month << " " << day - 59 << endl;
}
else if (day > 90 && day < 121)
{
month = "April";
cout << month << " " << day - 90 << endl;
}
else if (day > 120 && day < 152)
{
month = "May";
cout << month << " " << day - 120 << endl;
}
else if (day > 151 && day < 182)
{
month = "June";
cout << month << " " << day - 151 << endl;
}
else if (day > 181 && day < 213)
{
month = "July";
cout << month << " " << day - 181 << endl;
}
else if (day > 212 && day < 244)
{
month = "August";
cout << month << " " << day - 212 << endl;
}
else if (day > 243 && day < 274)
{
month = "September";
cout << month << " " << day - 243 << endl;
}
else if (day > 273 && day < 305)
{
month = "October";
cout << month << " " << day - 273 << endl;
}
else if (day > 304 && day < 335)
{
month = "November";
cout << month << " " << day - 304 << endl;
}
else if (day > 334 && day < 366)
{
month = "December";
cout << month << " " << day - 334 << endl;
}
}
int main()
{
DayOfYear day;
//DayOfYear *dayOfYear = new DayOfYear(day);
//DayOfYear *dayOfYear = new DayOfYear(month, day);
//DayOfYear::DayOfYear(string m, int d)
system("PAUSE");
return 0;
}
|