日期类(Date)_运算符重载简单应用

面对日期类里面的加加减减涉及到的进位和借位,我更倾向使用一个保存了每个月天数的数组代替,虽然很占内存,但是方法简化了,而且不需要遍历数组,只需要拿到月份的数字,用月份的数字当下标就可以得到这个月的天数(哈希表的简单应用),这种方法很好用,尤其是在需要不断的遍历数组的时候,可以用这种方法代替遍历.
在写代码的时候,还需要注意代码的复用,这样写代码的效率会变高,不会把时间都浪费在写很多类似的逻辑上,比如在本文的重载>,<,==,!=,>=,<=的时候就体现了出来.

#include<iostream>
using namespace std;
class Date
{
public:
    Date(int year = 1900, int month = 1, int day = 1)
        :_year(year)
        , _month(month)
        , _day(day)
    {
        if (_year<1900
            || _month<0
            || _month>12
            || _day<0
            || _day>calenter[_month])
            exit(-1);
    }
    bool operator > (const Date &d) const
    {
        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;
        else
            return false;
    }
    bool operator ==(const Date&d)const
    {
        if (_year == d._year&&_month == d._month&&_day == d._day)
            return true;
        else
            return false;
    }
    bool operator >=(const Date&d)const
    {
        return (*this > d || *this == d);
    }
    bool operator <=(const Date&d)const
    {
        return !(*this > d);
    }
    bool operator >(const Date&d)const
    {
        return !(*this > d || *this == d);
    }
    bool operator !=(const Date&d)const
    {
        return!(*this == d);
    }
    bool IsLeapYear(Date d)
    {
        if ((d._year % 100 != 0 && d._year % 4 == 0) || (d._year % 400 == 0))
        {
            d.calenter[2] = 29;
            return true;
        }
        d.calenter[2] = 28;
        return false;
    }
    Date operator+(int day)const//+ 不是+=,只需要操作,不能给this改变值!!!
    {
        if (day<0)
        {
            *this - (-day);
        }
        else
        {
            Date tmp = *this;
            tmp._day += day;
            while (tmp._day > calenter[_month])
            {
                tmp._day -= calenter[_month];
                tmp._month++;
                if (tmp._month > 12)
                {
                    tmp._year++;
                    tmp._month = 1;
                    IsLeapYear(tmp);
                }
            }
            return tmp;
        }
    }
    Date operator+=(int day)
    {
        return *this = *this + day;
    }
    Date operator-(int day)const
    {
        if(day<0)
        {
            return *this + (-day);
        }
        Date tmp = *this;
        tmp._day -= day;
        while (tmp._day < 0)
        {
            if (tmp._month == 1)
            {
                tmp._year--;
                IsLeapYear(tmp);
                tmp._month = 12;
                tmp._day += 31;
            }
            else
            {
                tmp._day += calenter[_month - 1];
                tmp._month++;
            }
        }
        return tmp;
    }
    Date operator-=(int day)
    {
        return *this = *this - day;
    }
    Date operator++()//前置
    {
        return ((*this) + 1);
    }
    Date operator++(int)//后置
    {
        Date tmp = *this;
        *this=*this+1;
        return tmp;
    }
    Date operator--()
    {
        return *this = *this - 1;
    }
    Date operator--(int)
    {
        Date tmp = *this;
        *this = *this - 1;
        return tmp;
    }
    int operator-(const Date& d)
    {
        Date tmp1;
        Date tmp2;
        int count = 0;
        if (*this > d)
        {
            tmp1 = d;
            tmp2 = *this;
        }
        else
        {
            tmp1 = *this;
            tmp2 = d;
        }
        while (tmp1!=tmp2)
        {
            tmp1++;
            count++;
        }
        return count;
    }
    ostream operator <<(ostream&os,const Date &d)
    {
        out<<d._yeaar<<-<<d._month<<-<<d.day<<endl;
        return out;
    }
private:
    int _year;
    int _month;
    int calenter[13]{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    int _day;
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值