#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
using namespace std;
class Date
{
public:
Date(int Year=2000,int Month=1,int Day=1)
:_Year(Year)
,_Month(Month)
,_Day(Day)
{
if(!IsInvalidDay(_Year,_Month,_Day))
{
_Year=2000;
_Month=1;
_Day=1;
cout<<"小同志时间有问题哟"<<endl;
}
}
Date operator+(int count)
{
Date tmp(*this);//?
tmp._Day+=count;
ToCorrect(tmp);
return tmp;
}
Date operator-(int count)
{
Date tmp(*this);
tmp._Day-=count;
ToCorrect(tmp);
return tmp;
}
Date& operator++()//前置++,++day
{
return (*this=*this+1);
}
Date operator++(int)//后置++,day++
{
Date tmp(*this);
*this=*this+1;
return tmp;
}
Date& operator--()//前置--,--day
{
(*this)--;
return *this;
}
Date operator--(int)//后置--,day--
{
Date tmp(*this);
*this=*this-1;
return tmp;
}
bool operator>(const Date& d)
{
if(this->_Year>d._Year
||(this->_Year==d._Year && this->_Month>d._Month)
||(this->_Year==d._Year && this->_Month==d._Month &&_Day>d._Day))
return true;
return false;
}
bool operator<(const Date& d)
{
if(this->_Year>d._Year
||(this->_Year==d._Year && this->_Month>d._Month)
||(this->_Year==d._Year && this->_Month==d._Month &&_Day>=d._Day))
return false;
return true;
}
bool operator==(const Date& d)
{
if(this->_Year==d._Year && this->_Month==d._Month && _Day==d._Day)
return true;
return false;
}
bool operator!=(const Date& d)
{
if(this->_Year==d._Year && this->_Month==d._Month && _Day==d._Day)
return false;
return true;
}
int operator-(const Date& d)//比较两个日期的差值
{
int flag=1;
Date max=*this;
Date min=d;
if(*this<d)
{
max=d;
min=*this;
flag=-1;
}
int count=0;
while(min!=max)
{
min++;
count++;
}
return count*flag;
}
Date& operator+=(int count)//count天后的日期
{
*this=*this+count;
return *this;
}
Date& operator-=(int count)//count天前的日期
{
*this=*this-count;
return *this;
}
virtual ~Date()
{}
public:
bool IsInvalidDay(int Year,int Month,int Day)
{
if(Year<1 || Month<1 || Month>13
|| Day>YearOfMonth(Year,Month))
return false;
return true;
}
int YearOfMonth(int Year,int Month)
{
int Day;
int Days[12]={31,28,31,30,31,30,31,31,30,31,30,31};
Day=Days[Month-1];
if(Month==2)
Day+=IsLeapYear(Year);
return Day;
}
//Date ToCorrect(Date &d)
//{
// if(d._Day<=d.YearOfMonth(d._Year,d._Month))
// {
// while(d._Day<=0)
// {
// if(d._Month==1)
// {
// d._Year--;
// d._Month=12;
// }
// else
// d._Month--;
// d._Day+=d.YearOfMonth(d._Year,d._Month);
// }
// }
// else
// {
// while(d._Day>d.YearOfMonth(d._Year,d._Month))
// {
// d._Day-=d.YearOfMonth(d._Year,d._Month);
// if(d._Month==12)
// {
// d._Year++;
// d._Month=1;
// }
// else
// d._Month++;
// //d._Day+=d.YearOfMonth(d._Year,d._Month);
// }
// }
// return d;
//}
Date ToCorrect(Date &d)
{
if (d._Day>YearOfMonth(d._Year, d._Month))
{
while (d._Day > YearOfMonth(d._Year, d._Month))
{
d._Day -= YearOfMonth(d._Year,d._Month);
if (d._Month == 12)
{
d._Year++;
d._Month = 1;
}
else
{
++d._Month;
}
}
return d;
}
else
{
while (d._Day <= 0)
{
if (d._Month == 1)
{
d._Year--;
d._Month = 12;
}
else
{
--d._Month;
}
d._Day += YearOfMonth(d._Year, d._Month);
}
return d;
}
}
bool IsLeapYear(int Year)
{
if(Year%4==0 && Year%100==0
||Year%400==0)
return true;
else
return false;
}
void Display()
{
cout<<"Time is:"<<_Year<<"-"<<_Month<<"-"<<_Day<<endl;
}
friend istream& operator>>(istream &is,Date &d);
friend ostream& operator<<(ostream &os,Date &d);
friend void Test();
private:
int _Year;
int _Month;
int _Day;
};
istream& operator>>(istream &is,Date &d)
{
cout<<"请输入一个日期"<<endl;
is>>d._Year>>d._Month>>d._Day;
return is;
}
ostream& operator<<(ostream &os,Date &d)
{
cout<<"请输出一个日期"<<endl;
os<<d._Year<<d._Month<<d._Day;
return os;
}
测试:
void Test()
{
Date d(2000,1,3);
//d.operator +(30);
//d.operator -(30);
//Date d1(2000,3,27);
//d.operator -(d1);
//d.operator +=(32);
//d.operator -=(21);
d.Display();
}
int main()
{
Test();
return 0;
}
下面是测试的截图: (黄色箭头是对应第一个跳入(调用)的函数)