1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
|
#include <iostream>
#include <stdio.h>
using namespace std;
class Date
{
private:
int day, month, year;
public:
//constructor
Date(int _day=1, int _month=1, int _year=1970)
{
if (_day<1 || _day>30)
{
cout<<"Error day\n";
day=1;
}
else
day=_day;
if (_month<1 || _month>12)
{
cout<<"Error month\n";
month=1;
}
else
month=_month;
if (_year<1970 || _year>2099)
{
cout<<"Error year\n";
year=1970;
}
else
year=_year;
}
//copy constructor
Date(Date& _date)
{
day=_date.get_day();
month=_date.get_month();
year=_date.get_year();
}
//get set functions
int get_day() { return day; }
int get_month() { return month; }
int get_year() { return year; }
void set_day(int _day) { day=_day; }
void set_month(int _month) { _month=_month; }
void set_year(int _year) { year=_year; }
void setDate(int _day, int _month, int _year)
{
if ((_day>=1 && _day<=30) && (_month>=1 && _month<=12) && (_year>=1970 && _year<=2099))
{
day=_day;
month=_month;
year=_year;
}
}
Date operator++()
{
Date temp = *this;
if (day<30)
day++;
else if (day==30 && month<12)
{
month++;
day=1;
}
else if (day==30 && month==12)
{
month=1;
day=1;
year++;
}
return *this;
}
//suffix ++ operator
Date operator++(int u)
{
Date temp = *this;
if (day<30)
day++;
else if (day==30 && month<12)
{
month++;
day=1;
}
else if (day==30 && month==12)
{
month=1;
day=1;
year++;
}
return temp;
}
Date& operator+=(const Date &_date)
{
int tmpDay, tmpMonth, tmpYear;
tmpDay=tmpMonth=tmpYear=0;
day+=_date.day;
// month+=_date.month;
//year+=_date.year;
tmpDay=day % 30;
tmpMonth=(month + day / 30) % 12;
tmpYear=(year+month/12);
day=tmpDay;
month=tmpMonth;
year=tmpYear;
return *this;
}
bool operator>(const Date _date) const
{
int date1_days=day+month*12+year*12*30;
int date2_days=_date.day+_date.month*12+_date.year*12*30;
if (date1_days>date2_days)
return true;
else
return false;
}
bool operator<(const Date _date) const
{
int date1_days=day+month*12+year*12*30;
int date2_days=_date.day+_date.month*12+_date.year*12*30;
if (date1_days<date2_days)
return true;
else
return false;
}
bool operator==(const Date &_date) const
{
if (day==_date.day && month==_date.month && year==_date.year)
return true;
else
return false;
}
friend ostream& operator << (ostream& os, const Date &_date);
};
ostream& operator << (ostream& os, const Date &_date)
{
os<<_date.day<<"/"<<_date.month<<"/"<<_date.year<<endl;
return os;
}
void buildDate(char *date, int &dd, int &mm, int &yy)
{
int z=0, pos;
char temp[15]={'\0'};
while(date[z]!=0 && date[z]!='/')
z++;
strncpy(temp, date, z);
dd=atoi(temp);
for(int x=0; x<15;x++)
temp[x]='\0';
z++;
pos=z;
while(date[z]!=0 && date[z]!='/')
z++;
strncpy(temp, date+pos, z);
mm=atoi(temp);
for(int x=0; x<15;x++)
temp[x]='\0';
z++;
pos=z;
while(date[z]!=0 && date[z]!='/')
z++;
strncpy(temp, date+pos, z);
yy=atoi(temp);
}
int main()
{
char date[15];
int dd, mm, yy;
cout<<"Enter a date\n";
cin>>date;
buildDate(date, dd,mm,yy);
Date d1(dd,mm,yy);
cout<<d1;
d1.setDate(30,1,2012);
cout<<d1;
cout<<++d1;
cout<<d1++;
cout<<"Enter a date\n";
cin>>date;
buildDate(date, dd,mm,yy);
d1.setDate(dd,mm,yy);
cout<<d1;
d1++;
cout<<d1;
Date d2(1,1,2010);
if (d1<d2)
cout<<"<:true\n";
else
cout<<"<:false\n";
d2+=20;
cout<<"+=:"<<d2;
if (d1==d2)
cout<<"==:true\n";
else
cout<<"==:false\n";
Date d3(d2);
cout<<d3;
if (d2==d3)
cout<<"==:true\n";
else
cout<<"==:false\n";
return 0;
}
|