Guest User

Untitled

a guest
Feb 24th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.24 KB | None | 0 0
  1. #pragma once
  2. #ifndef _DATE_H_
  3. #define _DATE_H_
  4.  
  5. #include <iostream>
  6. #include <string>
  7.  
  8. class Date {
  9. public:
  10. struct BadDate;
  11. enum Month {
  12. jan = 1, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec
  13. };
  14.  
  15. private:
  16. static std::string monthNames [12];
  17. static bool defaultSet;
  18. static Date defaultDate;
  19.  
  20. int _day, _month, _year;
  21. bool leapYear(const int) const;
  22. void fillDate(int d, Month m, int y);
  23. void checkIllFormed();
  24. bool isIllFormed() const;
  25. void setVal(int&, const int, const int);
  26.  
  27. public:
  28. static void setDefault(const int d = 1, Month = Month(1), const int y = 2000);
  29. static void showDefault(std::ostream& os);
  30. static const std::string monthNameByNumber(const int);
  31.  
  32. Date(
  33. int d = (Date::defaultSet)?Date::defaultDate.day():1,
  34. Month m = (Date::defaultSet)?Date::defaultDate.month():Month(1),
  35. int y = (Date::defaultSet)?Date::defaultDate.year():2000
  36. );
  37. Date(int d, int m, int y);
  38. Date(const Date&);
  39. Date& operator=(const Date&);
  40. ~Date();
  41.  
  42. int day() const;
  43. Month month() const;
  44. int year() const;
  45.  
  46. const std::string getMonthName() const;
  47.  
  48. void setDay(const int);
  49. void setMonth(const int);
  50. void setYear(const int);
  51.  
  52. const Date& operator++();
  53. const Date operator++(int);
  54. const Date& operator--();
  55. const Date operator--(int);
  56. };
  57.  
  58. struct Date::BadDate {
  59. int _day, _month, _year;
  60. BadDate(int d, int m, int y);
  61. };
  62.  
  63. std::ostream& operator<<(std::ostream&, const Date&);
  64. std::ostream& operator<<(std::ostream&, const Date::BadDate&);
  65.  
  66. #endif
  67.  
  68. #include "Date.h"
  69.  
  70. Date::BadDate::BadDate(int d, int m, int y)
  71. : _day(d), _month(m), _year(y) {
  72.  
  73. };
  74.  
  75. std::string Date::monthNames[12] = {
  76. "January", "February", "March",
  77. "April", "May", "June",
  78. "July", "August", "September",
  79. "October", "November", "December"
  80. };
  81. bool Date::defaultSet = true;
  82. Date Date::defaultDate = Date(1, 1, 2000);
  83.  
  84. void Date::setDefault(const int d, Month m, const int y) {
  85. Date::defaultDate.setDay(d);
  86. Date::defaultDate.setMonth(m);
  87. Date::defaultDate.setYear(y);
  88. Date::defaultSet = true;
  89. }
  90. void Date::showDefault(std::ostream& os) {
  91. os << Date::defaultDate;
  92. }
  93. const std::string Date::monthNameByNumber(const int n) {
  94. return Date::monthNames[n-1];
  95. }
  96.  
  97. Date::Date(int d, Month m, int y)
  98. : _day(d), _month(m), _year(y) {
  99. checkIllFormed();
  100. }
  101. Date::Date(int d, int m, int y)
  102. : _day(d), _month(Month(m)), _year(y) {
  103. checkIllFormed();
  104. }
  105. Date::Date(const Date& that)
  106. : _day(that.day()), _month(that.month()), _year(that.year()) {
  107. checkIllFormed();
  108. }
  109. void Date::checkIllFormed() {
  110. if(isIllFormed()) {
  111. BadDate bd = BadDate(day(), month(), year());
  112. *this = Date::defaultDate;
  113. throw bd;
  114. }
  115. }
  116. Date& Date::operator=(const Date& that) {
  117. fillDate(that.day(), that.month(), that.year());
  118. return *this;
  119. }
  120. Date::~Date(void) {
  121.  
  122. }
  123.  
  124. int Date::day() const {
  125. return _day;
  126. }
  127. Date::Month Date::month() const {
  128. return Month(_month);
  129. }
  130. int Date::year() const {
  131. return _year;
  132. }
  133.  
  134. const std::string Date::getMonthName() const {
  135. return Date::monthNameByNumber(month());
  136. }
  137.  
  138. void Date::setDay(const int d) {
  139. setVal(_day, d, day());
  140. }
  141. void Date::setMonth(const int m) {
  142. setVal(_month, m, month());
  143. }
  144. void Date::setYear(const int y) {
  145. setVal(_year, y, year());
  146. }
  147. void Date::setVal(int& val, const int newVal, const int prevVal) {
  148. val = newVal;
  149. if(isIllFormed()) {
  150. BadDate bd = BadDate(day(), month(), year());
  151. val = prevVal;
  152. throw bd;
  153. }
  154. }
  155.  
  156. const Date& Date::operator++() {
  157. setDay(day() + 1);
  158. return *this;
  159. }
  160. const Date Date::operator++(int) {
  161. setDay(day() + 1);
  162. return *this;
  163. }
  164. const Date& Date::operator--() {
  165. setDay(day() - 1);
  166. return *this;
  167. }
  168. const Date Date::operator--(int) {
  169. setDay(day() - 1);
  170. return *this;
  171. }
  172.  
  173. std::ostream& operator<<(std::ostream& os, const Date& d) {
  174. os << d.day() << '.' << d.getMonthName() << '.' << d.year();
  175. return os;
  176. }
  177. std::ostream& operator<<(std::ostream& os, const Date::BadDate& bd) {
  178. os << bd._day << '.' << Date::monthNameByNumber(bd._month) << '.' << bd._year;
  179. return os;
  180. }
  181.  
  182. bool Date::leapYear(const int y) const {
  183. /*
  184. 1.If the year is evenly divisible by 4, go to step 2. Otherwise, go to step 5.
  185. 2.If the year is evenly divisible by 100, go to step 3. Otherwise, go to step 4.
  186. 3.If the year is evenly divisible by 400, go to step 4. Otherwise, go to step 5.
  187. 4.The year is a leap year (it has 366 days).
  188. 5.The year is not a leap year (it has 365 days).
  189. */
  190. if(y%4) {
  191. return false;
  192. }
  193. if(y%100) {
  194. return true;
  195. }
  196. if(y%400) {
  197. return false;
  198. }
  199. return true;
  200. }
  201. void Date::fillDate(int d, Month m, int y) {
  202. setDay(d);
  203. setMonth(m);
  204. setYear(y);
  205. }
  206.  
  207. bool Date::isIllFormed() const {
  208. const int d(day()), m(month()), y(year());
  209.  
  210. //check year
  211. if(y < 0) {
  212. return true;
  213. }
  214.  
  215. //check month
  216. if(m < 1 || 12 < m) {
  217. return true;
  218. }
  219.  
  220. //check day
  221. int maxDay((7<m)?31-m%2:30+m%2);
  222. if(m == 2) {
  223. if(leapYear(y)) {
  224. maxDay = 29;
  225. } else {
  226. maxDay = 28;
  227. }
  228. }
  229. if(d<1 || maxDay<d) {
  230. return true;
  231. }
  232.  
  233. return false;
  234. }
Add Comment
Please, Sign In to add comment