Guest User

halp

a guest
Mar 1st, 2015
286
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.84 KB | None | 0 0
  1. #include "myDate.h"
  2. #include <iostream>
  3. #include <iomanip>
  4. using namespace std;
  5.  
  6. //Constructor
  7. //Description Sets month to 1, day to 1, year to 2015
  8. //Parameters: none
  9. myDate::myDate()
  10. {
  11.   month=1;
  12.   day =1;
  13.   year=2015;
  14. }
  15.  
  16. myDate::myDate(int dy, int yr, int mnth)
  17. {
  18.   month=mnth;
  19.   day=dy;
  20.   year=yr;
  21. }
  22. //gets and sets
  23. //sets month,day,year,time
  24. void myDate::setMonth(int mnth) {month=mnth;}
  25.  
  26. void myDate::setDay(int dy) {day=dy;}
  27.  
  28. void myDate::setYear(int yr) {year=yr;}
  29.  
  30. void myDate::setTime (const myTime &tm) {theTime=tm;}
  31.  
  32. //gets month,day,year,time
  33. int myDate::getMonth()const {return month;}
  34. int myDate::getDay()const {return day;}
  35. int myDate::getYear()const {return year;}
  36. myTime myDate::getTime()const {return theTime;}
  37.  
  38. //relation operators: compare full date and time
  39. //A later date/time is "greater than an earlier one
  40.  
  41. bool myDate::operator==(const myDate &date) const
  42. {
  43.   return (year == date.getYear() && month==date.getMonth() && day==date.getDay() && theTime==tm.getTime());
  44. }
  45.  
  46. bool myDate::operator!=(const myDate &date) const
  47. {
  48.   return (year != date.getYear() || month!=date.getMonth() || day!=date.getDay() || theTime!=tm.getTime());
  49. }
  50. bool myDate::operator>(const myDate &date)const
  51. {
  52.   if (year > date.getYear())
  53.     return true;
  54.  else if (year<date.getYear())
  55.     return false;
  56.   else if (month>date.getMonth())
  57.     return true;
  58.   else if (month<date.getMonth())
  59.     return false;
  60.   else if (day>date.getDay())
  61.     return true;
  62.   else if (day<date.getDay())
  63.     return false;
  64.   else if (theTime>tm.getTime())
  65.     return true;
  66.   else
  67.     return false;
  68. }
  69.  
  70. bool myDate::operator<(const myDate &date) const
  71. {
  72.   if (year>date.getYear())
  73.     return false;
  74.   else if (year<date.getYear())
  75.     return true;
  76.   else if (month>date.getMonth())
  77.     return false;
  78.   else if (month<date.getMonth())
  79.     return true;
  80.   else if (day>date.getDay())
  81.     return false;
  82.   else if (day<date.getDay())
  83.     return true;
  84. bool myDate::operator>=(const myDate &date) const
  85. {
  86.   if (*this>date ||*this ==date)
  87.     return true;
  88.   else
  89.     return false;
  90. }
  91.  
  92. bool myDate::operator<=(const myDate &date)const
  93. {
  94.   if(*this<date ||*this==date)
  95.     return true;
  96.   else
  97.     return false;
  98. }
  99. //Name: compareDates
  100. int myDate::compareDates(const myDate &secondDate) const
  101. {
  102.   if(*this>secondDate)
  103.     {
  104.       return -1;
  105.     }
  106.   else if(*this==secondDate)
  107.     {
  108.       return 0;
  109.     }
  110.   else
  111.     return 1;
  112. }
  113. //Adds days to the date
  114. myDate &myDate::operator+=(int dy)
  115. {
  116.   day+=dy;
  117.   return *this;
  118. }
  119.  
  120. string myDate::toLongDate()
  121. {
  122.   string wordDate;
  123.   if(month==1)
  124.     {
  125.       wordDate="January";
  126.     }
  127.   else if (month==2)
  128.     {
  129.       wordDate="February";
  130.     }
  131.   else if(month==3)
  132.     {
  133.       wordDate="March";
  134.     }
  135.  
  136.   else if (month==4)
  137.     {
  138. wordDate="April";
  139.     }
  140.  
  141.   else if (month==5)
  142.     {
  143.       wordDate="May";
  144.     }
  145.  
  146.   else if (month==6)
  147.     {
  148.       wordDate="June";
  149.     }
  150.  
  151.   else if (month==7)
  152.     {
  153.       wordDate="July";
  154.     }
  155.  
  156.   else if (month==8)
  157.     {
  158.       wordDate="August";
  159.     }
  160.  
  161.   else if (month==9)
  162.     {
  163.       wordDate="September";
  164.     }
  165.  
  166.   else if (month==10)
  167.     {
  168.  wordDate="October";
  169.     }
  170.  
  171.   else if (month==11)
  172.     {
  173.       wordDate="November";
  174.     }
  175.  
  176.   else
  177.     wordDate="December";
  178.   return wordDate;
  179. }
  180.  
  181. bool myDate::isLeapYear()const
  182. {
  183.   if ((year%4)!=0)
  184.     {
  185.       return false;
  186.     }
  187.   else if((year%4)==0 && (year%100)!=0)
  188.     {
  189.       return true;
  190.     }
  191.   else if((year%4)==0 &&(year%100)==0)
  192.     {
  193.       if((year%400)==0)
  194.         {
  195.           return true;
  196.         }
  197.       else
  198. return false;
  199.     }
  200.   else ;
  201. }
  202.  
  203. int myDate::daysInMonth()const
  204. {
  205.   int daysinmonth;
  206.   if(month==1)
  207.     {
  208.       daysinmonth=31;
  209.     }
  210.   else if (month==2)
  211.     {
  212.       if(isLeapYear()==true)
  213.         {
  214.           daysinmonth=29;
  215.         }
  216.       else
  217.         daysinmonth=28;
  218.     }
  219.   else if (month==3)
  220.     {
  221.       daysinmonth=31;
  222.     }
  223.   else if (month==4)
  224.     {
  225.       daysinmonth=30;
  226.     }
  227.  else if (month==5)
  228.     {
  229.       daysinmonth=31;
  230.     }
  231.  
  232.   else if (month==6)
  233.     {
  234.       daysinmonth=30;
  235.     }
  236.  
  237.   else if (month==7)
  238.     {
  239.       daysinmonth=31;
  240.     }
  241.  
  242.   else if (month==8)
  243.     {
  244.       daysinmonth=31;
  245.     }
  246.  
  247.   else if (month==9)
  248.     {
  249.       daysinmonth=30;
  250.     }
  251.  
  252.   else if (month==10)
  253.     {
  254.       daysinmonth=31;
  255.     }
  256.   else if (month==11)
  257.     {
  258.       daysinmonth=30;
  259.     }
  260.   else
  261.     {
  262.       daysinmonth=31;
  263.     }
  264. }
  265.  
  266.  
  267. ostream &operator<<(ostream &output, const myDate &date)
  268. {
  269.   output<< date.getMonth() <<"/"<<date.getDay()<<"/"<<date.getYear();
  270.   return output;
  271. }
  272.  
  273. istream &operator>>(istream &input, myDate &date)
  274. {
  275.   int dy, mnth,yr;
  276.   char slash;
  277.   input>>mnth>>slash>>dy>>slash>>yr;
  278.   date.setMonth(mnth);
  279.   date.setDay(dy);
  280.   date.setYear(yr);
  281.   return input;
  282. }
Advertisement
Add Comment
Please, Sign In to add comment