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
|
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
const int BUFFER_SIZE = 256;
const int daysinMonth[13] =
{
-1, 31, 28, 31, 30, 31, 30,
31, 31, 30, 31, 30, 31
};
const string nameOfMonth[13] =
{
"", "January", "February", "March",
"April", "May", "June", "July",
"August", "September", "October",
"November", "Decemeber"
};
class Date
{
private:
int month;
int day;
int year;
void CheckInput(int m, int d, int y);
bool isLeapYear(int y);
public:
Date(int m = 1, int d = 1, int y = 2001); // constructor
void getInfo(); // accessor
void printOne(void);
void printTwo(void);
void printThree(void);
};
Date::Date(int m, int d, int y)
{
CheckInput(m, d, y);
month = m;
day = d;
year = y;
}
void Date::CheckInput(int m, int d, int y)
{
ostringstream daysinMonthErrorMsg;
if((m < 1) || (m > 12))
throw range_error("A month must be between 1 and 12");
isLeapYear(y);
daysinMonthErrorMsg.str("");
daysinMonthErrorMsg << "A day must between 1 and ";
daysinMonthErrorMsg << daysinMonth[m]
<< " for the month of ";
daysinMonthErrorMsg << nameOfMonth[m]
<< " in " << y << endl;
if(m == 2 && isLeapYear(y))
{
if((d < 1) || (d > 29))
throw range_error("A day must between 1 and 29 for the month of February in a leap year");
}
else if((d < 1) || (d > daysinMonth[m]))
{
throw range_error(daysinMonthErrorMsg.str());
}
if(y < 1)
throw range_error("A year cannot be negative");
}
bool Date::isLeapYear(int y)
{
if(!(y % 4))
{
return true;
}
else
return false;
}
void Date::getInfo()
{
cout << "Class Info: \n";
cout << month << endl;
cout << day << endl;
cout << year << endl;
}
void Date::printOne()
{
cout << month << "/" << day << "/" << year<< endl;
}
void Date::printTwo()
{
cout << nameOfMonth[month] << " " << day << ", " << year << endl;
}
void Date::printThree()
{
cout << day << " " << nameOfMonth[month] << " " << year << endl;
}
int main()
{
try
{
cout << "Leap year test: \n";
Date datetest(2, 29, 2016); // leap year, 29 days in February
datetest.printOne();
datetest.printTwo();
datetest.printThree();
cout << '\n';
cout << "3 parameters test: \n";
Date datetest2(8, 31, 2005); // testing regular
datetest2.printOne();
datetest2.printTwo();
datetest2.printThree();
cout << '\n';
cout << "No parameters test: \n";
Date datetest3; // testing no parameter
datetest3.printOne();
datetest3.printTwo();
datetest3.printThree();
cout << '\n';
cout << "getinfo() test - \n";
datetest.getInfo();
//error tests, can only do one at a time
/*cout << '\n';
// tests out of range month
Date datetest4(0, 31, 2005);
datetest4.printOne();
datetest4.printTwo();
datetest4.printThree();*/
/*cout << '\n';
// tests out of range day, non-leap year
Date datetest5(2, 29, 2014);
datetest5.printOne();
datetest5.printTwo();
datetest5.printThree();*/
/*cout << '\n';
// tests out of range year
Date datetest6(2, 3, -1);
datetest6.printOne();
datetest6.printTwo();
datetest6.printThree();*/
}
catch(exception e)
{
cerr << e.what() << endl;
cin.ignore(BUFFER_SIZE, '\n');
return EXIT_FAILURE;
}
cin.ignore(BUFFER_SIZE, '\n');
return EXIT_SUCCESS;
}
|