Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2020
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.73 KB | None | 0 0
  1.  
  2.  
  3. // Date.h
  4. #include <iostream>
  5. #include <iomanip>
  6. #include <fstream>
  7. #include <string.h>
  8. using namespace std;
  9.  
  10. class Date
  11. {
  12. private:
  13. unsigned day;
  14. unsigned month;
  15. string monthName;
  16. unsigned year;
  17.  
  18. public:
  19. Date();
  20. Date(unsigned m, unsigned d, unsigned y);
  21. Date(const string &mn, unsigned d, unsigned y);
  22. void printNumeric() const;
  23. void printAlpha() const;
  24.  
  25. private:
  26. bool isLeap(unsigned y) const;
  27. unsigned daysPerMonth(unsigned m, unsigned y) const;
  28. string name(unsigned m) const;
  29. unsigned number(const string &mn) const;
  30. };
  31.  
  32. //Date.cpp
  33. #include <iostream>
  34. #include <iomanip>
  35. #include <fstream>
  36. #include <string.h>
  37. using namespace std;
  38. #include "Date.h"
  39.  
  40. // creates the date January 1st, 2000.
  41. Date::Date()
  42. {
  43. day = 1;
  44. month = 1;
  45. monthName = "January";
  46. year = 2000;
  47. }
  48.  
  49. /* parameterized constructor: month number, day, year
  50. - e.g. (3, 1, 2010) will construct the date March 1st, 2010
  51.  
  52. If any of the arguments are invalid (e.g. 15 for month or 32 for day)
  53. then the constructor will construct instead a valid Date as close
  54. as possible to the arguments provided - e.g. in above example,
  55. Date(15, 32, 2010), the Date would be corrected to Dec 31st, 2010.
  56. In case of such invalid input, the constructor will issue a console error message:
  57.  
  58. Invalid date values: Date corrected to 12/31/2010.
  59. (with a newline at the end).
  60. */
  61. Date::Date (unsigned m, unsigned d, unsigned y)
  62. {
  63. bool invalid = false;
  64.  
  65. //if invalid month input, change to closest month
  66. if (m < 1)
  67. {
  68. m = 1;
  69. invalid = true;
  70. }
  71. else if (m > 12)
  72. {
  73. m = 12;
  74. invalid = true;
  75. }
  76.  
  77. //invalid day
  78. if (d > daysPerMonth(m, y))
  79. {
  80. d = daysPerMonth(m,y);
  81. invalid = true;
  82. }
  83.  
  84. day = d;
  85. month = m;
  86. monthName = name(m);
  87. year = y;
  88.  
  89. if (invalid)
  90. {
  91. cout << "Invalid date values: Date corrected to ";
  92. cout << month << "/" << day << "/" << year << "." << endl;
  93. }
  94. }
  95.  
  96. /* parameterized constructor: month name, day, year
  97. ­ - e.g. (December, 15, 2012) will construct the date December 15th, 2012
  98.  
  99. If the constructor is unable to recognize the string argument as a valid month name,
  100. then it will issue a console error message:
  101.  
  102. Invalid month name: the Date was set to 1/1/2000.
  103. (with a newline at the end).
  104.  
  105. If the day argument is invalid for the given month (but the month name was valid),
  106. then the constructor will handle this error in the same manner as the other
  107. parameterized constructor.
  108.  
  109. This constructor will recognize both "december" and "December"
  110. as month name.
  111. */
  112. Date::Date (const string &mName, unsigned d, unsigned y)
  113. {
  114. //will change to true if invalid day
  115. bool invalidDay = false;
  116. //will change to true if invalid month
  117. bool invalidMonth = false;
  118.  
  119. if (mName == "January" || mName == "january")
  120. {
  121. month = number("January");
  122. monthName = "January";
  123. }
  124. else if (mName == "February" || mName == "february")
  125. {
  126. month = number("February");
  127. monthName = "February";
  128. }
  129. else if (mName == "March" || mName == "march")
  130. {
  131. month = number("March");
  132. monthName = "March";
  133. }
  134. else if (mName == "April" || mName == "april")
  135. {
  136. month = number("April");
  137. monthName = "April";
  138. }
  139. else if (mName == "May" || mName == "may")
  140. {
  141. month = number("May");
  142. monthName = "May";
  143. }
  144. else if (mName == "June" || mName == "june")
  145. {
  146. month = number("June");
  147. monthName = "June";
  148. }
  149. else if (mName == "July" || mName == "july")
  150. {
  151. month = number("July");
  152. monthName = "July";
  153. }
  154. else if (mName == "August" || mName == "august")
  155. {
  156. month = number("August");
  157. monthName = "August";
  158. }
  159. else if (mName == "September" || mName == "september")
  160. {
  161. month = number("September");
  162. monthName = "September";
  163. }
  164. else if (mName == "October" || mName == "october")
  165. {
  166. month = number("October");
  167. monthName = "October";
  168. }
  169. else if (mName == "November" || mName == "november")
  170. {
  171. month = number("November");
  172. monthName = "November";
  173. }
  174. else if (mName == "December" || mName == "december")
  175. {
  176. month = number("December");
  177. monthName = "December";
  178. }
  179. //If invalid month name, change date to 1/1/2000
  180. else
  181. {
  182. day = 1;
  183. month = number("January");
  184. monthName = "January";
  185. year = 2000;
  186.  
  187. invalidMonth = true;
  188. }
  189.  
  190. //if monthName is correct but day number is invalid, change to closest day
  191. if ((!invalidMonth) && (d > daysPerMonth(month, y)))
  192. {
  193. day = daysPerMonth(month,y);
  194. invalidDay = true;
  195. }
  196.  
  197. //outputs message if input was invalid
  198. if (invalidDay || invalidMonth)
  199. {
  200. cout << "Invalid date values: Date corrected to ";
  201. if (invalidDay)
  202. {
  203. year = y;
  204. }
  205. cout << month << "/" << day << "/" << year << "." << endl;
  206. }
  207.  
  208. else
  209. {
  210. day = d;
  211. year = y;
  212. }
  213. }
  214.  
  215.  
  216. /* Outputs to the console (cout) a Date exactly in the format "3/1/2012".
  217. Does not output a newline at the end.
  218. */
  219. void Date::printNumeric () const
  220. {
  221. cout << month << "/" << day << "/" << year;
  222. }
  223.  
  224. /* Outputs to the console (cout) a Date exactly in the format "March 1, 2012".
  225. The first letter of the month name is upper case, and the month name is
  226. printed in full - January, not Jan, jan, or january.
  227. Does not output a newline at the end.
  228. */
  229. void Date::printAlpha () const
  230. {
  231. cout << monthName << " " << day << ", " << year;
  232. }
  233.  
  234. /* Returns true if the year passed in is a leap year, otherwise returns false.
  235. */
  236. bool Date::isLeap(unsigned y) const
  237. {
  238. //implies leap year
  239. if (y % 4 == 0)
  240. {
  241. //does not imply leap year
  242. if (y % 100 == 0)
  243. {
  244. //unless its a multiple of 400
  245. if (y % 400 == 0)
  246. {
  247. return true;
  248. }
  249. return false;
  250. }
  251. return true;
  252. }
  253. return false;
  254. }
  255.  
  256. /* Returns number of days allowed in a given month
  257. - e.g. daysPerMonth(9, 2000) returns 30.
  258. Calculates February's days for leap and non-­leap years,
  259. thus, the reason year is also a parameter.
  260. */
  261. unsigned Date::daysPerMonth(unsigned m, unsigned y) const
  262. {
  263. if (m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12)
  264. {
  265. return 31;
  266. }
  267. else if (m == 4 || m == 6 || m == 9 || m == 11)
  268. {
  269. return 30;
  270. }
  271. else if (isLeap(y))
  272. {
  273. return 29;
  274. }
  275. return 28;
  276. }
  277.  
  278. /* Returns the name of a given month
  279. - e.g. name(12) returns the string "December"
  280. */
  281. string Date::name(unsigned m) const
  282. {
  283. if (m <= 1)
  284. {
  285. return "January";
  286. }
  287. else if (m >= 12)
  288. {
  289. return "December";
  290. }
  291.  
  292. if (m == 2)
  293. {
  294. return "February";
  295. }
  296. else if (m == 3)
  297. {
  298. return "March";
  299. }
  300. else if (m == 4)
  301. {
  302. return "April";
  303. }
  304. else if (m == 5)
  305. {
  306. return "May";
  307. }
  308. else if (m == 6)
  309. {
  310. return "June";
  311. }
  312. else if (m == 7)
  313. {
  314. return "July";
  315. }
  316. else if (m == 8)
  317. {
  318. return "August";
  319. }
  320. else if (m == 9)
  321. {
  322. return "September";
  323. }
  324. else if (m == 10)
  325. {
  326. return "October";
  327. }
  328. else if (m == 11)
  329. {
  330. return "November";
  331. }
  332. }
  333.  
  334. /* Returns the number of a given named month
  335. - e.g. number("March") returns 3
  336. */
  337. unsigned Date::number(const string &mName) const
  338. {
  339. if (mName == "January")
  340. {
  341. return 1;
  342. }
  343. else if (mName == "February")
  344. {
  345. return 2;
  346. }
  347. else if (mName == "March")
  348. {
  349. return 3;
  350. }
  351. else if (mName == "April")
  352. {
  353. return 4;
  354. }
  355. else if (mName == "May")
  356. {
  357. return 5;
  358. }
  359. else if (mName == "June")
  360. {
  361. return 6;
  362. }
  363. else if (mName == "July")
  364. {
  365. return 7;
  366. }
  367. else if (mName == "August")
  368. {
  369. return 8;
  370. }
  371. else if (mName == "September")
  372. {
  373. return 9;
  374. }
  375. else if (mName == "October")
  376. {
  377. return 10;
  378. }
  379. else if (mName == "November")
  380. {
  381. return 11;
  382. }
  383. else if (mName == "December")
  384. {
  385. return 12;
  386. }
  387. }
  388.  
  389.  
  390. // main.cpp
  391. #include <iostream>
  392. #include <iomanip>
  393. #include <fstream>
  394. #include <string.h>
  395. #include "Date.h"
  396.  
  397. using namespace std;
  398.  
  399. Date getDate();
  400.  
  401.  
  402. int main() {
  403.  
  404. Date testDate;
  405. testDate = getDate();
  406. cout << endl;
  407. cout << "Numeric: ";
  408. testDate.printNumeric();
  409. cout << endl;
  410. cout << "Alpha: ";
  411. testDate.printAlpha();
  412. cout << endl;
  413.  
  414. return 0;
  415. }
  416.  
  417. Date getDate() {
  418. int choice;
  419. unsigned monthNumber, day, year;
  420. string monthName;
  421.  
  422. cout << "Which Date constructor? (Enter 1, 2, or 3)" << endl
  423. << "1 - Month Number" << endl
  424. << "2 - Month Name" << endl
  425. << "3 - default" << endl;
  426. cin >> choice;
  427. cout << endl;
  428.  
  429. if (choice == 1) {
  430. cout << "month number? ";
  431. cin >> monthNumber;
  432. cout << endl;
  433. cout << "day? ";
  434. cin >> day;
  435. cout << endl;
  436. cout << "year? ";
  437. cin >> year;
  438. cout << endl;
  439. return Date(monthNumber, day, year);
  440. } else if (choice == 2) {
  441. cout << "month name? ";
  442. cin >> monthName;
  443. cout << endl;
  444. cout << "day? ";
  445. cin >> day;
  446. cout << endl;
  447. cout << "year? ";
  448. cin >> year;
  449. cout << endl;
  450. return Date(monthName, day, year);
  451. } else {
  452. return Date();
  453. }
  454. }
  455.  
  456.  
  457. /*
  458. output:
  459.  
  460. Which Date constructor? (Enter 1, 2, or 3)
  461. 1 - Month Number
  462. 2 - Month Name
  463. 3 - default
  464. 3
  465.  
  466. Numeric: 1/1/2000
  467. Alpha: January 1, 2000
  468.  
  469.  
  470. Which Date constructor? (Enter 1, 2, or 3)
  471. 1 - Month Number
  472. 2 - Month Name
  473. 3 - default
  474. 2
  475.  
  476. month name? march
  477.  
  478. day? 32
  479.  
  480. year? 1994
  481.  
  482. Invalid date values: Date corrected to 3/31/1994.
  483.  
  484. Numeric: 3/31/1994
  485. Alpha: March 31, 1994
  486.  
  487.  
  488. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement