使用C++完成一个【日期类】的编写。可以实现:

   1>日期+-天数-----输出日期 

   2>日期-日期------输出天数

#include <iostream>


using namespace std;

class Date

{

friend ostream& operator<<(ostream& os,const Date& d);

friend istream& operator>>(istream& is,const Date& d);

public:

Date(int year=1900,int month=1,int day=1)//构造函数

:_year(year)

,_month(month)

,_day(day)

{

if(IsLegal())

{

_year = 1900;

   _month = 1;

   _day = 1;

}

        

}

~Date()//析构函数

{


}

Date& operator=(const Date& d)//赋值函数

{

if(this != &d)

{

_year = d._year;

_month = d._month;

_day = d._day;

}

return *this;

}

public:

bool IsLegal()//年月日是否合法

{

if((_year<1900)||((_month<1)||(_month>12))||((_day>DayInMonth())||(_day<1)))

{

return true;

}

return false;

}

int DayInMonth()//每个月的天数

{

int arr[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};

if(IsLeap())

{

arr[2]+=1;

}

return arr[_month];

}

bool IsLeap()//是否为闰年

{

if(((_year%4==0)&&(_year%100!=0))||(_year%400==0))

{

return true;

}

else

{

return false;

}

}

void ToCrrectDate()

{

while(_day < 0)

{

if(_month==1)

{

_year -= 1;

_month = 12;

}

else

{

_month -= 1;

}

_day += DayInMonth();

}

while(_day > DayInMonth())

{

if(_month == 12)

{

_year += 1;

_month = 1;

}

_day -= DayInMonth();

_month += 1;

}

}

//public:

// void Print()

// {

// cout<<_year<<"-"<<_month<<"-"<<_day<<endl;

// }

public:

bool operator==(Date& d)

{

return (this->_year == d._year)&&(this->_month == d._month)&&(this->_day == d._day);

}

bool operator!=(Date &d)

{

return !(*this == d);

}

bool operator>(Date& d)

{

if(this->_year>d._year)

{

return true;

}

else if(this->_year==d._year)

{

if(this->_month>d._month)

{

return true;

}

else if(this->_month==d._month)

{

if(this->_day>d._day)

{

return true;

}

}

return false;

}

else

{

return false;

}

}

bool operator<(Date& d)

{

return !(*this >= d);

}

bool operator>=(Date& d)

{

return !(*this < d);

}

bool operator<=(Date& d)

{

return !(*this > d);

}

public://日期-+天数=日期

Date operator+(int day)

{

Date tmp(*this);

tmp._day += day;

tmp.ToCrrectDate();

return tmp;

}

Date operator-(int day)

{

Date tmp(*this);

tmp._day -= day;

tmp.ToCrrectDate();

return tmp;

}

Date& operator-=(int day)

{

this->_day -= day;

this->ToCrrectDate();

return *this;

}

Date& operator+=(int day)

{

this->_day += day;

this->ToCrrectDate();

return *this;

}

int operator-(Date& c)//日期-日期=天数

{

Date d1(*this);

Date d2(c);

int days = 0;

if(d1 > d2)

{  

d1 = c;

   d2 = *this;

   }

while(d1 != d2)

{

days++;

d1 += 1;

}

return days;

}

private:

int _year;

int _month;

int _day;

};

ostream& operator<<(ostream& os,const Date& d)

{

os<<d._year<<"-"<<d._month<<"-"<<d._day<<endl;

return os;

}

istream& operator>>(istream& is,const Date& d)

{

return is;

}

int main()

{

//Date d1(2016,1,27);

//Date d2(2016,2,3);

Date d(2016,3,2);

//Date d = d + 23;

d += 56;

cout<<d<<endl;

//d.Print();

/*int ret = d1 - d2;

cout << ret <<endl;*/

return 0;

}