SHOW:
|
|
- or go back to the newest paste.
| 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()); |
| 43 | + | return (year == date.getYear() && month==date.getMonth() && day==date.getDay() && theTime==date.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()); |
| 48 | + | return (year != date.getYear() || month!=date.getMonth() || day!=date.getDay() || theTime!=date.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()) |
| 64 | + | else if (theTime>date.dateTime()) |
| 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 | else if (theTime>date.getTime()) | |
| 85 | return false; | |
| 86 | else | |
| 87 | return true; | |
| 88 | } | |
| 89 | ||
| 90 | bool myDate::operator>=(const myDate &date) const | |
| 91 | {
| |
| 92 | if (*this>date ||*this ==date) | |
| 93 | return true; | |
| 94 | else | |
| 95 | return false; | |
| 96 | } | |
| 97 | ||
| 98 | bool myDate::operator<=(const myDate &date)const | |
| 99 | {
| |
| 100 | if(*this<date ||*this==date) | |
| 101 | return true; | |
| 102 | else | |
| 103 | return false; | |
| 104 | } | |
| 105 | //Name: compareDates | |
| 106 | int myDate::compareDates(const myDate &secondDate) const | |
| 107 | {
| |
| 108 | if(*this>secondDate) | |
| 109 | {
| |
| 110 | return -1; | |
| 111 | } | |
| 112 | else if(*this==secondDate) | |
| 113 | {
| |
| 114 | return 0; | |
| 115 | } | |
| 116 | else | |
| 117 | return 1; | |
| 118 | } | |
| 119 | //Adds days to the date | |
| 120 | myDate &myDate::operator+=(int dy) | |
| 121 | {
| |
| 122 | day+=dy; | |
| 123 | return *this; | |
| 124 | } | |
| 125 | ||
| 126 | string myDate::toLongDate() | |
| 127 | {
| |
| 128 | string wordDate; | |
| 129 | if(month==1) | |
| 130 | {
| |
| 131 | wordDate="January"; | |
| 132 | } | |
| 133 | else if (month==2) | |
| 134 | {
| |
| 135 | wordDate="February"; | |
| 136 | } | |
| 137 | else if(month==3) | |
| 138 | {
| |
| 139 | wordDate="March"; | |
| 140 | } | |
| 141 | ||
| 142 | else if (month==4) | |
| 143 | {
| |
| 144 | wordDate="April"; | |
| 145 | } | |
| 146 | ||
| 147 | else if (month==5) | |
| 148 | {
| |
| 149 | wordDate="May"; | |
| 150 | } | |
| 151 | ||
| 152 | else if (month==6) | |
| 153 | {
| |
| 154 | wordDate="June"; | |
| 155 | } | |
| 156 | ||
| 157 | else if (month==7) | |
| 158 | {
| |
| 159 | wordDate="July"; | |
| 160 | } | |
| 161 | ||
| 162 | else if (month==8) | |
| 163 | {
| |
| 164 | wordDate="August"; | |
| 165 | } | |
| 166 | ||
| 167 | else if (month==9) | |
| 168 | {
| |
| 169 | wordDate="September"; | |
| 170 | } | |
| 171 | ||
| 172 | else if (month==10) | |
| 173 | {
| |
| 174 | wordDate="October"; | |
| 175 | } | |
| 176 | ||
| 177 | else if (month==11) | |
| 178 | {
| |
| 179 | wordDate="November"; | |
| 180 | } | |
| 181 | ||
| 182 | else | |
| 183 | wordDate="December"; | |
| 184 | return wordDate; | |
| 185 | } | |
| 186 | ||
| 187 | bool myDate::isLeapYear()const | |
| 188 | {
| |
| 189 | if ((year%4)!=0) | |
| 190 | {
| |
| 191 | return false; | |
| 192 | } | |
| 193 | else if((year%4)==0 && (year%100)!=0) | |
| 194 | {
| |
| 195 | return true; | |
| 196 | } | |
| 197 | else if((year%4)==0 &&(year%100)==0) | |
| 198 | {
| |
| 199 | if((year%400)==0) | |
| 200 | {
| |
| 201 | return true; | |
| 202 | } | |
| 203 | else | |
| 204 | return false; | |
| 205 | } | |
| 206 | else ; | |
| 207 | } | |
| 208 | ||
| 209 | int myDate::daysInMonth()const | |
| 210 | {
| |
| 211 | int daysinmonth; | |
| 212 | if(month==1) | |
| 213 | {
| |
| 214 | daysinmonth=31; | |
| 215 | } | |
| 216 | else if (month==2) | |
| 217 | {
| |
| 218 | if(isLeapYear()==true) | |
| 219 | {
| |
| 220 | daysinmonth=29; | |
| 221 | } | |
| 222 | else | |
| 223 | daysinmonth=28; | |
| 224 | } | |
| 225 | else if (month==3) | |
| 226 | {
| |
| 227 | daysinmonth=31; | |
| 228 | } | |
| 229 | else if (month==4) | |
| 230 | {
| |
| 231 | daysinmonth=30; | |
| 232 | } | |
| 233 | else if (month==5) | |
| 234 | {
| |
| 235 | daysinmonth=31; | |
| 236 | } | |
| 237 | ||
| 238 | else if (month==6) | |
| 239 | {
| |
| 240 | daysinmonth=30; | |
| 241 | } | |
| 242 | ||
| 243 | else if (month==7) | |
| 244 | {
| |
| 245 | daysinmonth=31; | |
| 246 | } | |
| 247 | ||
| 248 | else if (month==8) | |
| 249 | {
| |
| 250 | daysinmonth=31; | |
| 251 | } | |
| 252 | ||
| 253 | else if (month==9) | |
| 254 | {
| |
| 255 | daysinmonth=30; | |
| 256 | } | |
| 257 | ||
| 258 | else if (month==10) | |
| 259 | {
| |
| 260 | daysinmonth=31; | |
| 261 | } | |
| 262 | else if (month==11) | |
| 263 | {
| |
| 264 | daysinmonth=30; | |
| 265 | } | |
| 266 | else | |
| 267 | {
| |
| 268 | daysinmonth=31; | |
| 269 | } | |
| 270 | } | |
| 271 | ||
| 272 | ||
| 273 | ostream &operator<<(ostream &output, const myDate &date) | |
| 274 | {
| |
| 275 | output<< date.getMonth() <<"/"<<date.getDay()<<"/"<<date.getYear(); | |
| 276 | return output; | |
| 277 | } | |
| 278 | ||
| 279 | istream &operator>>(istream &input, myDate &date) | |
| 280 | {
| |
| 281 | int dy, mnth,yr; | |
| 282 | char slash; | |
| 283 | input>>mnth>>slash>>dy>>slash>>yr; | |
| 284 | date.setMonth(mnth); | |
| 285 | date.setDay(dy); | |
| 286 | date.setYear(yr); | |
| 287 | return input; | |
| 288 | } |