Date类
判断 大于 小于 等于 等等运算符重载函数
我们先实现一个 > 的运算符重载,然后再实现一个 == 的运算符重载:
bool Date::operator>(const Date& d)
{
if (_year > d._year)
{
return true;
}
else if (_year == d._year && _month > d._month)
{
return true;
}
else if (_year == d._year && _month == d._month && _day > d._day)
{
return true;
}
return false;
}
bool Date::operator==(const Date& d)
{
return _year == d._year
&& _month == d._month
&& _day == d._day;
}
那接下来我们就可以调用这个两个 重载函数,来实现其他的 函数:
例如:< 就是 !> && !== :
bool Date::operator< (const Date& d)
{
return !(*this == d) && !(*this > d);
}
利用这样的逻辑,我们就可以这样写:
bool Date::operator<=(const Date& d)
{
return !(*this > d);
}
bool Date::operator>=(const Date& d)
{
return !(*this < d);
}
bool Date::operator!=(const Date& d)
{
return !(*this == d);
}
需要注意的是:上述后面 运算符重载函数 的实现技巧不仅仅可以用在 Date 简单简单类当中,因为后序的技巧只是在调用 对应的重载函数。所以这种技巧在如何类当中都可以使用。
其他功能函数实现
日期 + 天数 计算日期函数
首先确定的是每一个月的 天数不统一,而且有平年和闰年的区分2月的天数,所以我们先写一个函数来获取某一个月的 天数:
创建一个数组,下标 代表 月份,元素的值代表 这个月的天数:
static int array[] = {0,31,28,31,30,31,31,30,31,31,30,31,30,31};
直接把这个数组写在静态区,因为这个函数会被调用很多次,每次都需要创建这个数组的话很麻烦,所以我们直接创建静态的。
然后是判断闰年:
if(month == 2 && ((year % 4 == 0) || (year % 100 != 0) || (year % 400 == 0)))
{
return 29;
}
else
{
return array[month];
}
当我们拿到 一个月的天数的时候,我们就可以直接进行操作了,我们先直接把 需要加的天数直接加载 日期 的 _day 成员中,然后判断此时是否超出当月的天数,如果超出,_day 就减掉 当月的天数,_month++,然后再次判断,如果再超,就再减,如果不超说明就计算计算完成。如果上述月超了13,那么_year++。
下述代码的实现是有问题的:
Date Date::operator+(int day)
{
_day += day;
while (_day > GetMonthDate(_year, _month))
{
_day -= GetMonthDate(_year, _month);
_month++;
if (_month >= 13)
{
_month = 1;
_year++;
}
}
return *this;
}
这样写有一个问题,如下图:
我们只写了 d1 + 100 ,但是我们发现 d1 被修改了,其实是我们上述直接 对 _day 进行修改,我们应该把计算好的值作为函数的返回值。
所以,我们上诉实现的其实不是 + ,其实是 += :
Date Date::operator+=(int day)
{
_day += day;
while (_day > GetMonthDate(_year, _month))
{
_day -= GetMonthDate(_year, _month);
_month++;
if (_month >= 13)
{
_month = 1;
_year++;
}
}
return *this;
}
接下来我们对这个函数进行修改,实现 + 的重载:
Date Date::operator+(int day)
{
Date tmp(*this);
tmp->_day += day;
while (tmp->_day > GetMonthDate(_year, _month))
{
tmp->_day -= GetMonthDate(_year, _month);
tmp->_month++;
if (tmp->_month >= 13)
{
tmp->_month = 1;
tmp->_year++;
}
}
return tmp;
}
我们在 + 重载函数中创建一个 Date对象-tmp,然后把 当前对象拷贝给 这个tmp 对象,我们修改就修改这个 tmp 对象当中的 值,最后返回这个 tmp 就行了。
输出:
我们来看这个场景,当我们调用如下函数的时候,d3 是调用拷贝构造函数在创建,还是通过赋值来创建呢?
Date d3 = d1 + 100;
其实是 调用构造函数 来创建,因为此时编译器会做处理,把这个代码变成 如下来进行执行:
Date d3(d1 + 100);
+ 重载还可以这样写:
Date Date::operator+(int day)
{
Date tmp(*this);
tmp += day;
return tmp;
}
可以服用 += 重载函数。
前置++ 和后置++ 的实现
// 前置++
Date& Date::operator++()
{
*this += 1;
return *this;
}
// 后置++
Date Date::operator++(int)
{
Date tmp(*this);
*this += 1;
return tmp;
}
需要注意的是,前置后置 是 通过参数来构成重载,如 d1++;
- ++d1 会转换为 d1.operator++()
- d1++ 会转换成 d1.operator(0)
上述增加的int 不是为了接收具体的值,仅仅是为了占位,跟前置结点构成函数重载。
我们之前实现过+= 和 + 两个重载运算符,那么 -= 和 - 和+= ,+ 是类似的,只是-= 是向上一位借位,如果我们用 day - 天数,这个的结果是负数,就要像上一位借位,然后再让 day += 上这个月的 天数,直到day 为正数。
Date& Date::operator-=(int day)
{
_day -= day;
while (_day < 0)
{
_day += GetMonthDate(_year, _month);
_month--;
if (_month <= 0)
{
_month = 12;
_year--;
}
}
return *this;
}
Date Date::operator-(int day)
{
Date tmp = *this;
tmp -= day;
return tmp;
}
我们上述实现了 += 和 -= ,如果我们 日期 += 负数,或者是 日期 -= 上 负数,会出现问题:
我们发现, += 上 -100 之后,_day 变成了 负数,这是因为,我们调用是 += 这个重载函数,而 += -100 的正确逻辑应该是 -= 100 ,但是我们此时调用但是 += 重载函数,他的执行逻辑 是 day 为正数的时候才能实现,所以我们要在 += 重载函数中判断一下,如果 传入的 day 是负数,就去调用 -= 这个重载函数:
Date& Date::operator+=(int day)
{
if (day < 0)
{
return *this -= abs(day);
}
_day += day;
while (_day > GetMonthDate(_year, _month))
{
_day -= GetMonthDate(_year, _month);
_month++;
if (_month >= 13)
{
_month = 1;
_year++;
}
}
return *this;
}
注意,上述当中的 day 在-= 当中也是规定为 正数的,所以我们 += 当中调用 -= 函数的时候要把 day绝对值化,或者是添加一个 负号。那么同样, -= 重载函数也需要判断,当传入的 day 为负数的时候,就去调用 += 重载函数:
日期 - 日期
如果直接拿 日期 - 日期的话有点麻烦,还需要判断 闰年 和 每月天数不相同的情况,所以我们直接找出小的日期,让小的日期 ++ ,一直加到 大的日期,然后把每一次 ++ 的天数用一个计数器来保存起来:
int Date::operator-(const Date& d)
{
Date max = *this;
Date min = d;
int flag = 1; // 如果第一个大,第二个小,那么计算出来的天数就是正的
if (max < min)
{
max = d;
min = *this;
flag = -1; // 如果第一个小,第二个大,那么就是负的
}
int n = 0;
while (max != min)
{
++min;
++n;
}
return n * flag;
}
需要注意的是,两个日期,谁大水小是没有规定的,所以我们传入的日期就有大有小,那么相减的日期就有正有负,我们就默认 第一个大,第二个小,此时用 flag = 1 来表示 ,然后再判断,如果是第二个大,第一个小,那么就 flag = -1来表示,最后,我们用 n 来表示计数器,是正是负,乘以flag就行。
int Date::operator-(const Date& d)
{
Date max = *this;
Date min = d;
int flag = 1; // 如果第一个大,第二个小,那么计算出来的天数就是正的
if (*this < d)
{
max = d;
min = *this;
flag = -1; // 如果第一个小,第二个大,那么就是负的
}
int n = 0;
while (max != min)
{
++min;
++n;
}
return n * flag;
}
全部代码实现
class Date
{
public:
// 获取某年某月的天数
int GetMonthDay(int year, int month)
{
static int days[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int day = days[month];
if (month == 2
&&((year % 4 == 0 && year % 100 != 0) || (year%400 == 0)))
{
day += 1;
}
return day;
}
// 全缺省的构造函数
Date(int year = 1900, int month = 1, int day = 1)
{
if(year < 1900
|| month < 1 || month > 12
|| day < 1 || day > GetMonthDay(year ,month) )
{
cout<<"非法日期"<<endl;
}
_year = year;
_month = month;
_day = day;
}
// 拷贝构造函数
// d2(d1)
Date(const Date& d)
{
this->_year = d._year;
_month = d._month;
_day = d._day;
}
// 赋值运算符重载
// d2 = d3 -> d2.operator=(&d2, d3)
Date& operator=(const Date& d)
{
if (this != &d)
{
this->_year = d._year;
this->_month = d._month;
this->_day = d._day;
}
return *this;
}
// 析构函数
~Date()
{
// 清理工作
}
void Print()
{
cout<<_year<<"-"<<_month<<"-"<<_day<<endl;
}
// 日期+=天数
// d1 += 10
// d1 += -10
Date& operator+=(int day)
{
if (day < 0)
{
return *this -= -day;
}
_day += day;
while (_day > GetMonthDay(_year, _month))
{
_day -= GetMonthDay(_year, _month);
_month++;
if (_month == 13)
{
_year++;
_month = 1;
}
}
return *this;
}
// 日期+天数
// d + 10
Date operator+(int day)
{
Date ret(*this);
ret += day;
return ret;
}
// 日期-天数
Date operator-(int day)
{
Date ret(*this);
ret -= day;
return ret;
}
// 日期-=天数
// d -= 100
// d -= -100
Date& operator-=(int day)
{
if (day < 0)
{
return *this += -day;
}
_day -= day;
while (_day <= 0)
{
--_month;
if (_month == 0)
{
--_year;
_month = 12;
}
_day += GetMonthDay(_year, _month);
}
return *this;
}
// 前置++
// ++d -> d.operator++(&d)
Date& operator++()
{
*this += 1;
return *this;
}
// 后置++
// d++ -> d.operator++(&d, 0)
Date operator++(int)
{
Date ret(*this);
*this += 1;
return ret;
}
// // 后置--
Date operator--(int)
{
Date ret(*this);
*this -= 1;
return ret;
}
// 前置--
Date& operator--()
{
*this -= 1;
return *this;
}
// 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;
}
}
}
return false;
}
// ==运算符重载
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);
}
// <=运算符重载
bool operator <= (const Date& d)
{
return !(*this > d);
}
// !=运算符重载
bool operator != (const Date& d)
{
return !(*this == d);
}
// d1 - d2
// 日期-日期 返回天数
int operator-(const Date& d)
{
int flag = 1;
Date max = *this;
Date min = d;
if (*this < d)
{
max = d;
min = *this;
flag = -1;
}
int day = 0;
while (min < max)
{
++(min);
++day;
}
return day*flag;
}
private:
int _year;
int _month;
int _day;
};