日期类的简单实现

1.要考虑到日期的合法性,如果不合法,置成1990.1.1
2.由于在日期类,没有用到资源的开辟,所以我们可以使用编译器自动合成的拷贝构造,赋值运算符重载等。
3.实现一个日期加上N天后,日期为多少
4.实现一个日期减去N天后,日期为多少
5.求两个日期之间相隔多少天

代码如下:

#include<iostream>
using namespace std;

class Date
{
public:
    Date(int year, int month, int day)
        : _year(year)
        , _month(month)
        , _day(day)
    {
        if (!(year > 0 && month > 0 && month < 13 && day>0 && day <= GetDayOfMonth(year, month)))
        {
            _year = 1990;
            _month = 1;
            _day = 1;
        }
    }
    bool isleap(int year)  //判断是否是闰年
    {
        if ((0 == year % 4 && 0 != year % 100) || (year % 400 == 0))
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    int GetDayOfMonth(int year, int month)//每个月的天数不同,根据每个月来获取这个月的天数
    {
        int mon[] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
        if (2 == month)
        {
            if (isleap(year))
            {
                return mon[2] += 1;
            }
        }
        return mon[month];
    }
    // 求当前日期第days天后是哪一天? 
    Date operator+(int days)
    {
        Date tmp(*this);
        if (days < 0)  // 加上一个负数的天数,相当于减去一个正数的天数
        {
            days = -days;
            return tmp - days;
        }
        tmp._day += days;
        while (tmp._day > GetDayOfMonth(tmp._year,tmp._month))
        {
            tmp._day -= GetDayOfMonth(tmp._year, tmp._month);
            tmp._month++;
            if (tmp._month > 12)
            {
                tmp._year++;
                tmp._month = 1;
            }
        }
        return tmp;
    }

    // 求当前日期第days天前是哪一天? 
    Date operator-(int days)
    {
        Date tmp(*this);
        if (days < 0)   //减去一个负数的天数,相当于加上一个正数的天数
        {
            days = -days;
            return tmp + days;
        }

        tmp._day -= days;
        while (tmp._day < 0)
        {
            tmp._month--;
            if (tmp._month < 1)
            {
                tmp._year--;
                tmp._month = 12;
            }
            tmp._day += GetDayOfMonth(tmp._year, tmp._month);

        }
        return tmp;
    }

    // 求两个日期之间的差值 
    int operator-(const Date& d)
    {
        int count = 0;
        Date Mindate(*this);    //假设最小
        Date Maxdate(d);        //假设最大
        if (Mindate > Maxdate)  //不满足,就交换两个对象,致使满足
        {
            Maxdate = Mindate;
            Mindate = d;
        }
        while (Maxdate != Mindate)
        {
            Mindate++;
            count++;
        }
        return count;
    }

    // 前置++ 
    Date& operator++()
    {
        *this = *this + 1;
        return *this;
    }

    // 后置++ 
    Date operator++(int)
    {
        Date tmp(*this);
        *this = *this + 1;
        return tmp;
    }
    Date& operator--()
    {
        *this = *this - 1;
        return *this;
    }
    Date operator--(int)
    {
        Date tmp(*this);
        *this = *this - 1;
        return tmp;
    }


    // 判断两个日期是否相等 
    bool operator==(const Date& d)
    {
        if (_year == d._year &&
            _month == d._month &&
            _day == d._day)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    // 判断两个日期是否不等 
    bool operator!=(const Date& d)
    {
        if (*this == d)
        {
            return false;
        }
        else
        {
            return true;
        }
    }
    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)
    {
        if (*this > d || *this == d)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    bool operator<(const Date& d)
    {
        if (*this > d)
        {
            return false;
        }
        else
        {
            return true;
        }
    }
    bool operator<=(const Date& d)
    {
        if (*this >= d)
        {
            return false;
        }
        else
        {
            return true;
        }
    }

private:
    int _year;
    int _month;
    int _day;
};

int main()
{
    Date d1(2018, 5, 6);
    Date d2(2026, 10, 25);
    cout << d2 - d1 << endl;
    d1++;
    d1--;
    --d1;
    ++d1;
    //d1 - 999;
    //d1 + 100;
    //d1 - (-999);
    return 0;
}

以上就是一个简单的日期类实现,如有建议,望得到你的建议,如有错误,望得到指正。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值