日期類

#include <iostream>
using namespace std;
static  int arry[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
class Date
{
public:
static int GetMonDay(int year, int month)
{

if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
{
arry[2] = 29;
}
else
{
arry[2] = 28;

}
return arry[month];
}
void Display()
{
cout << _year << "-" << _month << "-" << _day << endl;
}
Date(int year = 1900, int month = 1, int day = 1)
:_year(year)
,_month(month)
,_day(day)
{
if (year<1900 || month>12 || month < 0 || day<0 || day>GetMonDay(year, month))
{
cout << "您輸入的是一個不合法的日期" << endl;
_year = 1900;
_month = 1;
_day = 1;
}
}
Date(const Date& d)
:_year(d._year)
, _month(d._month)
, _day(d._day)
{
}
Date& operator= (const Date& d)
{
if (this!=&d)
{
this->_year = d._year;
this->_month = d._month;
this->_day = d._day;

return *this;
}
friend ostream& operator<<(ostream& os, const Date& d);
friend istream& operator>>(istream& is,  Date& d);
public:

bool operator == (const Date& d)
{
return _year == d._year
&& _month == d._month 
     && _day == d._day;
}
bool operator != (const Date& d)
{
return !(*this == d);
}
bool operator > (const Date& d)
{
if (_year > d._year)
{
return true;
}
if (_year == d._year)
{
if (_month > d._month)
{
return true;
}
if (_month == d._month)
{
if (_day > d._day)
{
return true;
}
}
}
return false;
}
bool operator >= (const Date& d)
{
return  (*this >d)||(*this==d);
}
bool operator < (const Date& d)
{
return !(*this>=d);
}
bool operator <= (const Date& d)
{
return !(*this>d);
}
Date operator+(int day)
{
if (day<0)
{
day = -day;
return *this - day;
}
Date tmp(*this);
tmp._day += day;
while (tmp._day > GetMonDay(tmp._year, tmp._month))
{
tmp._day -= GetMonDay(tmp._year, tmp._month);
if (tmp._month == 12)
{
tmp._year++;
tmp._month = 1;
}
else
tmp._month++;
}
return tmp;
}
Date operator-(int day)
{
if (day < 0)
{
day = -day;
return *this + day;
}
Date tmp(*this);
tmp._day -= day;
while (tmp._day<0)
{
if (tmp._month ==1)
{
tmp._year--;
tmp._month=12;
}
else
{
tmp._month--;
}
tmp._day += GetMonDay(tmp._year,tmp._month);
}
return tmp;
}
Date& operator-=(int day)
{
Date tmp= *this- day;
*this = tmp;
return *this;
}
Date& operator+=(int day)
{
Date tmp = *this - day;
*this = tmp;
return *this;
/*_day += day;
return *this;*/
}

const Date& operator++()//前置++
{
*this += 1;
return *this;
}
Date operator++(int)//後置++
{
Date tmp(*this);
*this+=1;
return tmp;
}
const Date& operator--()
{
*this -= 1;
return *this;
}
Date operator--(int)
{
Date tmp(*this);
*this-=1;
return  tmp;
}
//計算兩個日期相減以後的差的天數
int operator-(const Date& d)
{
Date a=*this, b=d;
int flag = 1;
if (a > b)
{
a = d;
b = *this;
}
else
{
flag = -1;
}
int day = 0;//定義一個計數器
while (a != b)
{
++a;
++day;
}
return  day*flag;
}
private:
int _year;
int _month;
int _day;

/*const int _testConst;
int& _testRef;
Time _t;*/
};
istream& operator>>(istream& is, Date& d)
{
cout << "請依次輸入年-月-日:" << endl;
cin >> d._year;
cin >> d._month;
cin >> d._day;
return  is;
}
ostream& operator<<(ostream& os, const Date& d)
{
cout << d._year << "-" << d._month << "-" << d._day << endl;
return os;
}
/*oid Print()
{
cout << "**********歡迎使用萬年曆******** "<< endl;

}*/

void TestDate()
{
Date d1(2015, 8, 1),d3;
Date d2(2015, 2, 13);
d1 + (31);
d1.Display();
/*Date d2(d1);
d2.Display();
d3 = d1;
d3.Display();*/
cout << "d1==d2?:" << (d1 == d2) << endl;
cout << "d1>d2?:" << (d1 > d2) << endl; 
cout << "d1>=d2?:" << (d1 >= d2) << endl;
cout << "d1<d2?:" << (d1 < d2) << endl;
cout << "d1<=d2?:" << (d1 <= d2) << endl;
cout << (d1 + 13) << endl;
cout << (d1 + 53) << endl;
cout << (d1 -13) << endl;
cout << (d1 -1) << endl;
cout << (d1 -33) << endl;
//cout << (d1 +=13) << endl;
d1 -= 13;
cout << d1<< endl;
cout << (d3=d1 --) << endl;
cout << (d3=d1 ++) << endl;
cout << (++d1) << endl;
//cout << (--d1) << endl;

//cout << (d1 -d2) << endl;

}
int main()
{
// Print();
TestDate();
getchar();
return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章