Advertisement
Guest User

Untitled

a guest
Jan 27th, 2020
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.75 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. class Date {
  7. private:
  8. unsigned day;
  9. unsigned month;
  10. string monthName;
  11. unsigned year;
  12.  
  13. public:
  14. // creates the date January 1st, 2000.
  15. Date();
  16.  
  17.  
  18. /* parameterized constructor: month number, day, year
  19. - e.g. (3, 1, 2010) will construct the date March 1st, 2010
  20.  
  21. If any of the arguments are invalid (e.g. 15 for month or 32 for day)
  22. then the constructor will construct instead a valid Date as close
  23. as possible to the arguments provided - e.g. in above example,
  24. Date(15, 32, 2010), the Date would be corrected to Dec 31st, 2010.
  25. In case of such invalid input, the constructor will issue a console error message:
  26.  
  27. Invalid date values: Date corrected to 12/31/2010.
  28. (with a newline at the end).
  29. */
  30. Date(unsigned m, unsigned d, unsigned y);
  31.  
  32.  
  33. /* parameterized constructor: month name, day, year
  34. - e.g. (December, 15, 2012) will construct the date December 15th, 2012
  35.  
  36. If the constructor is unable to recognize the string argument as a valid month name,
  37. then it will issue a console error message:
  38.  
  39. Invalid month name: the Date was set to 1/1/2000.
  40. (with a newline at the end).
  41.  
  42. If the day argument is invalid for the given month (but the month name was valid),
  43. then the constructor will handle this error in the same manner as the other
  44. parameterized constructor.
  45.  
  46. This constructor will recognize both "december" and "December"
  47. as month name.
  48. */
  49. Date(const string &mn, unsigned d, unsigned y);
  50.  
  51.  
  52. /* Outputs to the console (cout) a Date exactly in the format "3/15/2012".
  53. Does not output a newline at the end.
  54. */
  55. void printNumeric() const;
  56.  
  57.  
  58. /* Outputs to the console (cout) a Date exactly in the format "March 15, 2012".
  59. The first letter of the month name is upper case, and the month name is
  60. printed in full - January, not Jan, jan, or january.
  61. Does not output a newline at the end.
  62. */
  63. void printAlpha() const;
  64.  
  65. private:
  66.  
  67. /* Returns true if the year passed in is a leap year, otherwise returns false.
  68. */
  69. bool isLeap(unsigned y) const;
  70.  
  71.  
  72. /* Returns number of days allowed in a given month
  73. - e.g. daysPerMonth(9, 2000) returns 30.
  74. Calculates February's days for leap and non-leap years,
  75. thus, the reason year is also a parameter.
  76. */
  77. unsigned daysPerMonth(unsigned m, unsigned y) const;
  78.  
  79. /* Returns the name of a given month
  80. - e.g. name(12) returns the string "December"
  81. */
  82. string name(unsigned m) const;
  83.  
  84. /* Returns the number of a given named month
  85. - e.g. number("March") returns 3
  86. */
  87. unsigned number(const string &mn) const;
  88. };
  89.  
  90.  
  91. // Implement the Date member functions here
  92.  
  93. //Private Helper functions
  94.  
  95. bool Date::isLeap(unsigned y) const {
  96. bool leap = false;
  97. if(y % 4 == 0) {
  98. if(y % 100 == 0) {
  99. if(y % 400 == 0) {
  100. leap = true;
  101. }
  102. } else {
  103. leap = true;
  104. }
  105. }
  106. return leap;
  107. }
  108.  
  109. unsigned Date::daysPerMonth(unsigned m, unsigned y) const{
  110. unsigned int dayNum = 0;
  111. if(m == 1 || m == 3 || m == 5 ||
  112. m == 7 || m == 8 || m == 10 ||
  113. m == 12) {
  114. dayNum = 31;
  115. } else if (m == 2) {
  116. if(isLeap(y)) {
  117. dayNum = 29;
  118. } else {
  119. dayNum = 28;
  120. }
  121. } else {
  122. dayNum = 30;
  123. }
  124. return dayNum;
  125. }
  126.  
  127. string Date::name(unsigned m) const {
  128. string name;
  129. if(m == 1) {
  130. name = "January";
  131. } else if(m == 2) {
  132. name = "February";
  133. } else if(m == 3) {
  134. name = "March";
  135. } else if(m == 4) {
  136. name = "April";
  137. } else if(m == 5) {
  138. name = "May";
  139. } else if(m == 6) {
  140. name = "June";
  141. } else if(m == 7) {
  142. name = "July";
  143. } else if(m == 8) {
  144. name = "August";
  145. } else if(m == 9) {
  146. name = "September";
  147. } else if(m == 10) {
  148. name = "October";
  149. } else if(m == 11) {
  150. name = "November";
  151. } else if(m == 12) {
  152. name = "December";
  153. }
  154. return name;
  155. }
  156.  
  157. unsigned Date::number(const string &mn) const {
  158. unsigned number;
  159. if(mn == "January") {
  160. number = 1;
  161. } else if (mn == "February") {
  162. number = 2;
  163. } else if (mn == "March") {
  164. number = 3;
  165. } else if (mn == "April") {
  166. number = 4;
  167. } else if (mn == "May") {
  168. number = 5;
  169. } else if (mn == "June") {
  170. number = 6;
  171. } else if (mn == "July") {
  172. number = 7;
  173. } else if (mn == "August") {
  174. number = 8;
  175. } else if (mn == "September") {
  176. number = 9;
  177. } else if (mn == "October") {
  178. number = 10;
  179. } else if (mn == "November") {
  180. number = 11;
  181. } else if (mn == "December") {
  182. number = 12;
  183. } else {
  184. number = -1;
  185. }
  186. return number;
  187. }
  188.  
  189. // Constructors
  190.  
  191. Date::Date() {
  192. day = 1;
  193. month = 1;
  194. monthName = "January";
  195. year = 2000;
  196. }
  197.  
  198. Date::Date(unsigned m, unsigned d, unsigned y) {
  199. bool incorrect = false;
  200.  
  201. if(m > 12) {
  202. incorrect = true;
  203. m = 12;
  204. } else if (m < 1) {
  205. incorrect = true;
  206. m = 1;
  207. }
  208.  
  209. if(d < 1) {
  210. incorrect = true;
  211. d = 1;
  212. } else if (d > daysPerMonth(m, y)){
  213. d = daysPerMonth(m, y);
  214. incorrect = true;
  215. } else if (m == 2) {
  216. if(isLeap(y)) {
  217. if(d != 29) {
  218. incorrect = true;
  219. d = 29;
  220. }
  221. } else {
  222. if(d != 28) {
  223. incorrect = true;
  224. d = 28;
  225. }
  226. }
  227. }
  228.  
  229.  
  230. if(incorrect) {
  231. cout << "Invalid date values: Date corrected to " << m
  232. << "/" << d << "/" << y << "." << endl;
  233. }
  234.  
  235. month = m;
  236. day = d;
  237. monthName = name(m);
  238. year = y;
  239.  
  240. }
  241.  
  242. Date::Date(const string &mn, unsigned d, unsigned y) {
  243. bool incorrect = false;
  244.  
  245. string x = mn;
  246.  
  247. x[0] = toupper(x[0]);
  248. int m = number(x);
  249.  
  250. if(m == -1) {
  251. day = 1;
  252. month = 1;
  253. monthName = "January";
  254. year = 2000;
  255. cout << "Invalid month name: the Date was set to 1/1/2000." << endl;
  256. } else {
  257. if(d < 1) {
  258. incorrect = true;
  259. d = 1;
  260. } else if (d > daysPerMonth(m, y)){
  261. d = daysPerMonth(m, y);
  262. incorrect = true;
  263. } else if (m == 2) {
  264. if(isLeap(y)) {
  265. if(d != 29) {
  266. incorrect = true;
  267. d = 29;
  268. }
  269. } else {
  270. if(d != 28) {
  271. incorrect = true;
  272. d = 28;
  273. }
  274. }
  275. }
  276.  
  277. month = m;
  278. day = d;
  279. monthName = x;
  280. year = y;
  281.  
  282. if(incorrect) {
  283. cout << "Invalid date values: Date corrected to " << m
  284. << "/" << d << "/" << y << "." << endl;
  285. }
  286. }
  287. }
  288.  
  289. void Date::printNumeric() const {
  290. cout << month << "/" << day << "/" << year;
  291. }
  292.  
  293. void Date::printAlpha() const {
  294. cout << monthName << " " << day << ", " << year;
  295. }
  296.  
  297.  
  298.  
  299. // Don't change the code below this line.
  300. // You may comment them out if you want to build your own test harness
  301. // while in develope mode, but you will need these to pass tets in submit mode.
  302.  
  303. Date getDate();
  304.  
  305. int main() {
  306.  
  307. Date testDate;
  308. testDate = getDate();
  309. cout << endl;
  310. cout << "Numeric: ";
  311. testDate.printNumeric();
  312. cout << endl;
  313. cout << "Alpha: ";
  314. testDate.printAlpha();
  315. cout << endl;
  316.  
  317. return 0;
  318. }
  319.  
  320. Date getDate() {
  321. int choice;
  322. unsigned monthNumber, day, year;
  323. string monthName;
  324.  
  325. cout << "Which Date constructor? (Enter 1, 2, or 3)" << endl
  326. << "1 - Month Number" << endl
  327. << "2 - Month Name" << endl
  328. << "3 - default" << endl;
  329. cin >> choice;
  330. cout << endl;
  331.  
  332. if (choice == 1) {
  333. cout << "month number? ";
  334. cin >> monthNumber;
  335. cout << endl;
  336. cout << "day? ";
  337. cin >> day;
  338. cout << endl;
  339. cout << "year? ";
  340. cin >> year;
  341. cout << endl;
  342. return Date(monthNumber, day, year);
  343. } else if (choice == 2) {
  344. cout << "month name? ";
  345. cin >> monthName;
  346. cout << endl;
  347. cout << "day? ";
  348. cin >> day;
  349. cout << endl;
  350. cout << "year? ";
  351. cin >> year;
  352. cout << endl;
  353. return Date(monthName, day, year);
  354. } else {
  355. return Date();
  356. }
  357. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement