Advertisement
Guest User

d_date.h

a guest
Feb 2nd, 2013
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.78 KB | None | 0 0
  1. #ifndef DATE_CLASS
  2. #define DATE_CLASS
  3.  
  4. #include <iostream>
  5. #include <iomanip>
  6. #include <string>
  7.  
  8. #include "d_except.h"
  9.  
  10. using namespace std;
  11.  
  12. class date
  13. {
  14. public:
  15. date (int mm=1, int dd=1, int yyyy=1900);
  16. // supply date in format MM/DD/YYYY
  17. // preconditions: 1 <= mm <= 12,
  18. // 1 <= dd <= daysInMonth()
  19.  
  20. void writeShortDate () const;
  21. // output the date in the format "MM/DD/YYYY"
  22. void writeLongDate () const;
  23. // output the date in the format "month day, year"
  24.  
  25. void incrementDate(int ndays);
  26. // add ndays days to the date
  27. // precondition: 0 <= ndays <= 365
  28.  
  29. int numberOfDays() const;
  30. // return the number of days into the year
  31.  
  32. int getMonth() const;// done
  33. // return the month as integer value 1 to 12
  34. int getDay() const; //done
  35. // return day of the month
  36. int getYear() const; //done
  37. // return the year
  38.  
  39. void setMonth(int mm); //done
  40. // update the month
  41. // precondition: 1 <= mm <= 12
  42. void setDay(int dd); //done
  43. // update the day
  44. // precondition: 1 <= dd <= daysInMonth()
  45. void setYear(int yyyy); //done
  46. // update the year
  47. // precondition: if the date is February 29,
  48. // yyyy must be a leap year
  49. int daysInMonth() const;
  50. // return number of days in the month
  51.  
  52. bool isLeapYear() const;
  53. // is the current year a leap year (true/false)
  54.  
  55. //ADDED
  56. bool operator< (const date&) const;
  57. bool operator> (const date&) const;
  58. date operator++ ();
  59. friend ostream& operator<<(ostream&, const date&);
  60. friend istream& operator>>(istream&, date& day);
  61.  
  62. private:
  63. enum monthName {Jan = 1, Feb, Mar, Apr, May, Jun,
  64. Jul, Aug, Sep, Oct, Nov, Dec};
  65. // private type used by date
  66.  
  67. int month, day, year;
  68. // private members that specify the date
  69.  
  70. };
  71.  
  72.  
  73. // constructor. month, day, year given as integer
  74. // values mm dd yyyy
  75. date::date (int mm, int dd, int yyyy):
  76. month(mm), day(dd), year(yyyy)
  77. {
  78. if (month < 1 || month > 12)
  79. throw dateError("date constructor:", month, "invalid month");
  80.  
  81. if (day <= 0 || day > daysInMonth())
  82. throw dateError("date constructor:", day, "invalid day of month");
  83. }
  84.  
  85. // write date in the format "MM/DD/YYYY"
  86. void date::writeShortDate () const
  87. {
  88. // save current format flags and fill character
  89. ios_base::fmtflags currentFlags = cout.flags();
  90. char currentFill = cout.fill();
  91.  
  92. // enable right justification
  93. cout.setf(ios::right,ios::adjustfield);
  94.  
  95. // output the month right-justified in
  96. // two character positions, blank fill
  97. cout.fill(' ');
  98. cout << setw(2) << month;
  99.  
  100. // set fill char to '0'
  101. cout.fill('0');
  102. cout << '/' << setw(2) << day << '/' << year;
  103.  
  104. // restore the fill char and the format flags
  105. cout.fill(currentFill);
  106. cout.setf(currentFlags);
  107. }
  108.  
  109. // write date with full month name
  110. void date::writeLongDate() const
  111. {
  112. // array begins with empty string corresponding to month 0
  113. static string monthNames[] =
  114. {"", "January", "February", "March", "April", "May",
  115. "June", "July", "August", "September", "October",
  116. "November", "December"
  117. };
  118. // use month as index into monthNames
  119. cout << monthNames[month] << " " << day << ", " << year;
  120. }
  121.  
  122. // add ndays days to the date. ndays must be in the range
  123. // 0 <= ndays <= 365
  124. void date::incrementDate(int ndays)
  125. {
  126. int totalDays, daysInYear = 365;
  127. int addDay;
  128.  
  129. if (ndays < 0 || ndays > 365)
  130. throw dateError("date incrementDate():", ndays, "is out of range");
  131.  
  132. if(isLeapYear())
  133. {
  134. addDay = 1;
  135. daysInYear++;
  136. }
  137. else
  138. addDay = 0;
  139.  
  140. totalDays = numberOfDays() + ndays;
  141. if (totalDays/daysInYear == 1 && totalDays%daysInYear != 0)
  142. {
  143. year++;
  144. if(isLeapYear())
  145. addDay = 1;
  146. else
  147. addDay = 0;
  148. totalDays = totalDays % daysInYear;
  149. }
  150.  
  151. if (totalDays <= 31)
  152. {
  153. month = 1;
  154. day = totalDays;
  155. }
  156. else if (totalDays <= 59 + addDay)
  157. {
  158. month = 2;
  159. day = totalDays - 31;
  160. }
  161. else if (totalDays <= 90 + addDay)
  162. {
  163. month = 3;
  164. day = totalDays - (59 + addDay);
  165. }
  166. else if (totalDays <= 120 + addDay)
  167. {
  168. month = 4;
  169. day = totalDays - (90 + addDay);
  170. }
  171. else if (totalDays <= 151 + addDay)
  172. {
  173. month = 5;
  174. day = totalDays - (120 + addDay);
  175. }
  176. else if (totalDays <= 181 + addDay)
  177. {
  178. month = 6;
  179. day = totalDays - (151 + addDay);
  180. }
  181. else if (totalDays <= 212 + addDay)
  182. {
  183. month = 7;
  184. day = totalDays - (181 + addDay);
  185. }
  186. else if (totalDays <= 243 + addDay)
  187. {
  188. month = 8;
  189. day = totalDays - (212 + addDay);
  190. }
  191. else if (totalDays <= 273 + addDay)
  192. {
  193. month = 9;
  194. day = totalDays - (243 + addDay);
  195. }
  196. else if (totalDays <= 304 + addDay)
  197. {
  198. month = 10;
  199. day = totalDays - (273 + addDay);
  200. }
  201. else if (totalDays <= 334 + addDay)
  202. {
  203. month = 11;
  204. day = totalDays - (304 + addDay);
  205. }
  206. else if (totalDays <= 365 + addDay)
  207. {
  208. month = 12;
  209. day = totalDays - (334 + addDay);
  210. }
  211. }
  212.  
  213.  
  214. // determine the number of days into the year
  215. int date::numberOfDays() const
  216. {
  217. // assign a place holder value of 0 for monthLength[0]
  218. int monthLength[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
  219.  
  220. int daysToDate = 0, i;
  221.  
  222. // add up all the days in the preceding months
  223. for (i = 1; i < month; i++)
  224. daysToDate += monthLength[i];
  225.  
  226. // if year is a leap year and the month is March or later,
  227. // increment daysToDate. use member function isLeapYear()
  228. if (isLeapYear() && month > 2)
  229. daysToDate += day + 1;
  230. else
  231. daysToDate += day;
  232.  
  233. return daysToDate;
  234. }
  235.  
  236. // return the month as integer value 1 to 12
  237. int date::getMonth() const
  238. {
  239. return month;
  240. }
  241.  
  242. // return day of the month
  243. int date::getDay() const
  244. {
  245. return day;
  246. }
  247.  
  248. // return the year
  249. int date::getYear() const
  250. {
  251. return year;
  252. }
  253.  
  254. // assign a new month
  255. void date::setMonth(int mm)
  256. {
  257. // verify that mm is in the range 1 to 12
  258. if (mm < 1 || mm > 12)
  259. throw dateError("date setMonth():", mm, "invalid month");
  260.  
  261. month = mm;
  262. }
  263.  
  264. // assign a new day of current month
  265. void date::setDay(int dd)
  266. {
  267. // verify that dd is in correct range for
  268. // the current month
  269. if (dd <= 0 || dd > daysInMonth())
  270. throw dateError("date setDay():", dd, "invalid day of month");
  271.  
  272. day = dd;
  273. }
  274.  
  275. void date::setYear(int yyyy)
  276. {
  277. // assign the new year
  278. year = yyyy;
  279.  
  280. // if the date is February 29, yyyy must be a
  281. // leap year
  282. if (month == 2 && day == 29 && !isLeapYear())
  283. throw dateError("date setYear():",
  284. year, "Not a leap year. February 29 invalid");
  285. }
  286.  
  287. // return the number of days in the monthn
  288. int date::daysInMonth() const
  289. {
  290. int monthLength;
  291.  
  292. // monthName(month) converts integer month to the
  293. // equivalent monthName object
  294. switch (monthName(month))
  295. {
  296. case Jan:
  297. case Mar:
  298. case May:
  299. case Jul:
  300. case Aug:
  301. case Oct:
  302. case Dec: monthLength = 31; // months with 31 days
  303. break;
  304.  
  305. case Apr:
  306. case Jun:
  307. case Sep:
  308. case Nov: monthLength = 30; // months with 30 days
  309. break;
  310.  
  311. case Feb: if (isLeapYear()) // special case of Feb
  312. monthLength = 29;
  313. else
  314. monthLength = 28;
  315. break;
  316. }
  317. return monthLength;
  318. }
  319.  
  320. // is the current year a leap year (true/false)
  321. bool date::isLeapYear() const
  322. {
  323. if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
  324. return true;
  325. else
  326. return false;
  327. }
  328.  
  329. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement