我的第一万天是多少?(日期计算器)

当我们回想往事时会不会感叹时间流逝,往事如烟,暮然回首,物是人非……

额……不扯了,现在进入主题,当别人问你多大的时候,你总是会说:我22了!然后别人会说,都奔三了,结婚了没有啊,难道你会在回一句,我连女朋友都没有……过。真是悲催呀!额……又扯远了。

好了,为了避免尴尬,别人问你多大的时候,你可以跟他说,我已经七千多天了,哈哈哈。那么这个换算是如何进行的呢?

接下来我来告诉你:

#include<iostream>
using namespace std;

class Date
{
public:
    Date(int year = 1990, int month = 1, int day = 1)
        :_year(year)
        , _month(month)
        , _day(day)
    {
        //如果日期规格不正确,那么定义一个日期
        if (!(_year > 0 && _month > 0 && _month<13
            && _day>0 && _day < GetMonthDays(_year, _month)))
        {
            _year = 1990;
            _month = 1;
            _day = 1;
        }
    }

    Date(const Date&d)
        :_year(d._year)
        , _month(d._month)
        , _day(d._day)
    {}

    Date& operator=(const Date&d)
    {
        if (this != &d)
        {
            _year = d._year;
            _month = d._month; 
            _day = d._day;
        }
        return *this;
    }

    //日期加上天数,返回日期
    Date operator+(const int days)
    {
        if (days<0)//如果天数为负数,直接转为减法
        {
            return (*this) - (0 - days);
        }

        Date temp(*this);//定义一个临时变量,
        temp._day += days;//将天数加到临时变量的天上面

        int _daysinmonth = 0;
        while (temp._day>(_daysinmonth=GetMonthDays(temp._year, temp._month)))
        {
            temp._day -= _daysinmonth;
            if (12 == temp._month)//如果当前月份为12,那么年加1,月置于1
            {
                temp._year += 1;
                temp._month = 1;
            }
            else
            {
                temp._month += 1;
            }
        }
        return temp;//处理完成之后,返回这个日期
    }

    //日期减去天数,返回日期
    Date operator-(const int days)
    {
        if (days < 0)//如果天数是负数,直接转为加法
        {
            return (*this) + days;
        }

        Date temp(*this);
        temp._day -= days;

        while (temp._day < 0)
        {
            if (1 == temp._day)//如果当前月份为1月,那么年减1,月份置于12
            {
                temp._year -= 1;
                temp._month = 12;
            }
            else
            {
                temp._month -= 1;
            }

            temp._day += GetMonthDays(temp._year, temp._month);
        }
        return temp;
    }

    //日期之间的减法,返回相差的天数
    int operator-(const Date& d)
    {
        Date _mindate(*this);
        Date _maxdate(d);

        if (_mindate > _maxdate)
        {
            _mindate = _maxdate;
            _maxdate = *this;
        }

        int count = 0;
        while (_mindate < _maxdate)
        {
            ++_mindate;//让小的日期加1的同时将计数器加1
            ++count;
        }
        return count;
    }

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

    Date&operator++(int)
    {
        Date temp = *this;
        *this = *this + 1;
        return temp;
    }

    Date&operator--()
    {
        *this = *this - 1;
        return *this;
    }

    Date&operator--(int)
    {
        Date temp = *this;
        *this = *this - 1;
        return temp;
    }

    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;
        }
        return false;
    }

    bool operator==(const Date& d)
    {
        if (_year == d._year && _month == d._month && _day == d._day)
        {
            return true;
        }
        return false;
    }

    bool operator<(const Date& d)
    {
        return !(*this>d || *this == d);
    }

    bool operator!=(const Date& d)
    {
        return !(*this == d);
    }

private:
    //获得每月的天数
    int GetMonthDays(int year,int month)
    {
        int DaysInMonth[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
        if (IsLeap(year) && month == 2)
        {
            DaysInMonth[month] += 1;
        }
        return DaysInMonth[month];
    }

    //判断是不是瑞年
    bool IsLeap(int year)
    {
        if (((0 == year % 4) && (0 != year % 100)) || (0 == year % 400))
        {
            return true;
        }
        return false;
    }

    //重载输出操作符
    friend ostream& operator << (ostream&_cout, const Date&d)
    {
        _cout << d._year << "-" << d._month << "-" << d._day;
        return _cout;
    }

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

int main()
{
    //Date d;
    //Date d1(2017, 3, 22);
    //Date d2(2017, 2, 30);

    Date d3(1995, 7, 11);
    cout << d3 + 10000<< endl;//我的第一万天是多少

    cout << d3 - 100 << endl;//出生前百天日期

    Date d4(2017, 3, 23);
    cout << d4 - d3 << endl;//我活了多少天

    return 0;
}

一万天也是一个具有有重要意义的日子,快来计算你的一万天吧!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值