实现关于日期的常用计算,具体代码如下:
#include<iostream>
#include<assert.h>
using namespace std;
class Date
{
public:
Date(int year = 1900, int month = 1, int day = 1) //构造函数
:_year(year)
,_month(month)
,_day(day)
{
if (!IsInvalid()) // this->IsInvalid(this)
{
assert(false);
//cout<<"非法日期"< //exit(-1);
}
}
Date(const Date& d)//拷贝构造函数
{
_year=d._year;
_month=d._month;
_day=d._day;
}
Date& operator=(const Date& d)
{
//assert(d);
if(this!=&d)
{
(*this)._day=d._day;
(*this)._month=d._month;
(*this)._year=d._year;
}
return *this;
}
~Date()//析构函数
{}
bool IsInvalid()
{
if(_year>=0)
{
if(_month>=1&&_month<=12)
{
if(_day<=(GetMonthDay(_year, _month)))
{
return true;
}
else
return false;
}
return false;
}
return false;
}
bool isLeapYear(int year)
{
if(year%4==0&&year%100!=0||year%400==0)
{
return true;
}
else
return false;
}
int GetMonthDay(int year, int month)
{
int day;
int DayNum[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
if(isLeapYear(year))
{
if(2==month)
{
day=DayNum[2]+1;
}
else
day=DayNum[month];
}
else
day=DayNum[month];
return day;
}
void Show()
{
cout<<_year<<"-"<<_month<<"-"<<_day<<endl;
}
bool operator==(const Date& d)//相等
{
return ((_year==d._year)&&(_month==d._month)&&(_day==d._day));
}
bool operator!=(const Date& d)
{
return ((_year!=d._year)||(_month!=d._month)||(_day!=d._day));
}
bool operator>=(const Date& d)
{
return ((*this>d)||(*this==d));
}
bool operator<=(const Date& d)
{
return ((*this<d)||(*this==d));
}
bool operator>(const Date& d)
{
if(_year>d._year)
{
return true;
}
else if(_year==d._year)
{
if(_month>d._month)
{
return true;
}
else if(_month==d._month)
{
if(_day>d._day)
{
return true;
}
else if(_day==d._day)
{
return false;
}
else
return false;
}
else
return false;
}
else
return false;
}
// d1 < d2
bool operator<(const Date& d)
{
if(_year<d._year)
{
return true;
}
else if(_year==d._year)
{
if(_month<d._month)
{
return true;
}
else if(_month==d._month)
{
if(_day<d._day)
{
return true;
}
else if(_day==d._day)
{
return false;
}
else
return false;
}
else
return false;
}
else
return false;
}
// d1 + 10
Date operator+(int day) //当前日期向前加day后的日期
{
if(day<0)
{
return *this-(-day);
}
Date tmp=*this;
tmp._day+=day;
while(tmp._day>GetMonthDay(tmp._year,tmp._month))
{
if(tmp._month!=12)
{
tmp._day-=GetMonthDay(tmp._year,tmp._month);
tmp._month++;
}
else
{
tmp._day-=GetMonthDay(tmp._year,tmp._month);
tmp._month=1;
tmp._year++;
}
}
return tmp;
}
Date& operator+=(int day)//日期向后加day天
{
*this=*this+day;
return *this;
}
Date operator-(int day)//日期向前减day天
{
if(day<0)
{
return *this+(-day);
}
Date tmp=*this;
tmp._day-=day;
while(tmp._day>GetMonthDay(tmp._year,tmp._month))
{
if(tmp._month!=1)
{
tmp._day-=GetMonthDay(tmp._year,tmp._month);
tmp._month--;
}
else
{
tmp._day-=GetMonthDay(tmp._year,tmp._month);
tmp._month=12;
tmp._year--;
}
}
return tmp;
}
Date& operator-=(int day)//日期向前减day天
{
*this= *this-day;
return *this;
}
int operator-(const Date& d)//两日期相减
{
int flag=1;
Date max=*this;
Date min=d;
if(max<min)
{
Date tmp=max;
max=min;
min=tmp;
flag=-1;
}
int numday=0;
while(max!=min)
{
numday++;
min._day++;
}
return numday;
}
//++d1
Date& operator++() // 前置
//d1++
{
return *this+=1;
}
Date operator++(int) // 后置
{
Date tmp=*this;
*this+=1;
return tmp;
}
Date& operator--()//前置
{
return *this-=1;
}
Date operator--(int)//后置
{
Date tmp=*this;
*this-=1;
return tmp;
}
private:
int _year;
int _month;
int _day;
};
int main()
{
Date d1;
d1.Show();
Date d2(1900, 10, 1);
d2.Show();
Date i= d1+300;
d1+=300;
d1.Show();
d1--;
d1.Show();
--d1;
d1.Show();
int s=d1-d2;
cout<<s<<endl;
int j;
j=d1.GetMonthDay(2000, 2);
cout<<j<<endl;
return 0;
}