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

来源:互联网 发布:去日本带mac好吗 编辑:程序博客网 时间:2024/04/30 05:27

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

额……不扯了,现在进入主题,当别人问你多大的时候,你总是会说:我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;}

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

1 0