#include<iostream>
using namespace std;
class Date
{
friend ostream& operator<<(ostream& _cout, const Date &d);
public:
bool check_is_true(int year,int month,int day)//判断日期合法性。
{
if (_year<0 || (_month<1 && _month>12) || (_day>_month_day(_year, _month)))
return false;
else
return true;
}
Date(int year = 1990, int month = 1, int day = 1)
:_year(year)
, _month(month)
, _day(day)
{
_day = d._day;
_month = d._month;
_year = d._year;
}
Date& operator=(const Date&d)
{
if (&d != this)
_year = d._year;
_month = d._month;
_day = d._day;
return *this;
}
Date& operator++()
{
(*this)++;
return *this ;
}
Date operator++(int)
{
Date temp(*this);
*this = *this + 1;
return temp;
}
Date operator+(int days)
{
if (days<0)
{
return *this - (-days);
}
else
{
Date temp(*this);
temp._day += days;
while (temp._day >temp._month_day(temp._year, temp._month))
{
temp._day -= _month_day(temp._year, temp._month);
if (temp._month == 12)
{
temp._year++;
temp._month = 1;
}
else
{
temp._month++;
}
}
return temp;
}
}
Date operator-(int days)
{
if (days < 0)
return *this + (-days);
else
{
Date temp(*this);
temp._day -= days;
while (temp._day>0)
{
--_month;
if (_month == 0)
{
temp._year--;
temp._year = 12;
}
temp._day += _month_day(temp._year, temp._month);//这里调用计算月份天数函数时,最好将临时变量的值传过去如果直接调用可能函数内使用的还是this指针给的值。
}
return temp;
}
}
int _month_day(int year, int month)//计算每个月份的天数,特别的要注意平年闰年的变化。
{
int _month_day[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
if (year_is(year))
{
_month_day[2] = 29;
}
return _month_day[month];
}
static bool year_is(int year)// 为了避免与其他类成员或全局对象名字冲突给它加上static修饰符,作为日期类的内部函数。
{
if (year % 4 == 0 && year % 400 != 0 || year % 400 == 0)
return true;
else
return false;
}
int operator-(const Date& d)
{
int flag = 1;
Date max = *this;
Date min = d;
if (*this<d)
{
min = *this;
max = d;
flag = -1;
}
int count = 0;
while (min != max)
{
++min;
count++;
}
return count*flag;
}
bool operator!=(const Date&d)
{
return!(*this == d);
}
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 || (_year = d._year&&_month > d._month) || ((_year = d._year) && (_month = d._month) && (_day > d._day))))
return true;
else
return false;
}
bool operator<(const Date &d)
{
return !((*this>d) || (*this == d));
}
private:
int _year;
int _month;
int _day;
};
ostream& operator<<(ostream& _cout, const Date &c1)//输出运算符重载的第一个参数一定是输出流的引用,返回值类型也是输出流的引用。
{
_cout << "(" << c1._year << "-" << c1._month << "-" << c1._day << ")";
return _cout;
}
system("pause");
return 0;
}
using namespace std;
class Date
{
friend ostream& operator<<(ostream& _cout, const Date &d);
public:
bool check_is_true(int year,int month,int day)//判断日期合法性。
{
if (_year<0 || (_month<1 && _month>12) || (_day>_month_day(_year, _month)))
return false;
else
return true;
}
Date(int year = 1990, int month = 1, int day = 1)
:_year(year)
, _month(month)
, _day(day)
{
if(!check_is_true(_year,_month,_day))
{
_year=1990;
_month=1;
_day=1
}
}
Date(const Date& d){
_day = d._day;
_month = d._month;
_year = d._year;
}
Date& operator=(const Date&d)
{
if (&d != this)
_year = d._year;
_month = d._month;
_day = d._day;
return *this;
}
Date& operator++()
{
(*this)++;
return *this ;
}
Date operator++(int)
{
Date temp(*this);
*this = *this + 1;
return temp;
}
Date operator+(int days)
{
if (days<0)
{
return *this - (-days);
}
else
{
Date temp(*this);
temp._day += days;
while (temp._day >temp._month_day(temp._year, temp._month))
{
temp._day -= _month_day(temp._year, temp._month);
if (temp._month == 12)
{
temp._year++;
temp._month = 1;
}
else
{
temp._month++;
}
}
return temp;
}
}
Date operator-(int days)
{
if (days < 0)
return *this + (-days);
else
{
Date temp(*this);
temp._day -= days;
while (temp._day>0)
{
--_month;
if (_month == 0)
{
temp._year--;
temp._year = 12;
}
temp._day += _month_day(temp._year, temp._month);//这里调用计算月份天数函数时,最好将临时变量的值传过去如果直接调用可能函数内使用的还是this指针给的值。
}
return temp;
}
}
int _month_day(int year, int month)//计算每个月份的天数,特别的要注意平年闰年的变化。
{
int _month_day[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
if (year_is(year))
{
_month_day[2] = 29;
}
return _month_day[month];
}
static bool year_is(int year)// 为了避免与其他类成员或全局对象名字冲突给它加上static修饰符,作为日期类的内部函数。
{
if (year % 4 == 0 && year % 400 != 0 || year % 400 == 0)
return true;
else
return false;
}
int operator-(const Date& d)
{
int flag = 1;
Date max = *this;
Date min = d;
if (*this<d)
{
min = *this;
max = d;
flag = -1;
}
int count = 0;
while (min != max)
{
++min;
count++;
}
return count*flag;
}
bool operator!=(const Date&d)
{
return!(*this == d);
}
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 || (_year = d._year&&_month > d._month) || ((_year = d._year) && (_month = d._month) && (_day > d._day))))
return true;
else
return false;
}
bool operator<(const Date &d)
{
return !((*this>d) || (*this == d));
}
private:
int _year;
int _month;
int _day;
};
ostream& operator<<(ostream& _cout, const Date &c1)//输出运算符重载的第一个参数一定是输出流的引用,返回值类型也是输出流的引用。
{
_cout << "(" << c1._year << "-" << c1._month << "-" << c1._day << ")";
return _cout;
}
system("pause");
return 0;
}