简单实现日期计算器的基本功能

来源:互联网 发布:株洲中车 知乎 编辑:程序博客网 时间:2024/06/05 15:48
#include<iostream>using namespace std;class Date{    friend ostream& operator<<(ostream& out, const Date& d);    friend istream& operator>>(istream& cin,  Date& d);public:    //构造函数    Date(int year = 1900, int month = 1, int day = 1)        :_year(year)        , _month(month)        , _day(day)    {        // 如何检查一个日期是否合法         int days = GetMonthDays(year, month);        if (days == -1 || day < 1 || day > days)        {            cout << "日期不合法" << endl;            exit(-1);        }    }    //拷贝构造    Date(const Date& d)        :_year(d._year)        , _month(d._month)        , _day(d._day)    {        ;    }    //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)    {        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 !(*this < d);    }    bool operator<= (const Date& d)    {        return (*this < d) || (*this == d);        //return !(*this > d);    }    bool operator!= (const Date& d)    {        return !(*this == d);    }    //d1 + 100     Date operator+(int day)//+并没有改变这个值,因此要一个临时变量    {        Date tmp = *this;        tmp._day += day;        while (tmp._day > GetMonthDays(tmp._year, tmp._month))        {            tmp._day = tmp._day - GetMonthDays(tmp._year, tmp._month);            tmp._month++;            if (tmp._month > 12)            {                tmp._month = tmp._month - 12;                tmp._year++;            }        }        return tmp;    }    Date& operator+=(int day)//+=改变了这个值    {        _day += day;        while (_day > GetMonthDays(_year, _month))        {            _day = _day - GetMonthDays(_year, _month);            _month++;            if (_month > 12)            {                _month = _month - 12;                _year++;            }        }        return *this;    }    Date operator-(int day)//-没有改变原来的值    {        Date tmp = *this;        tmp._day = tmp._day - day;        while (tmp._day < 1)        {            tmp._month--;            if (tmp._month < 1)            {                tmp._month = 12;                tmp._year--;            }            tmp._day += GetMonthDays(tmp._year, tmp._month);        }        return tmp;    }    Date operator-=(int day)    {        _day = _day - day;        while (_day < 1)        {            _month--;            if (_month < 1)            {                _month = 12;                _year--;            }            _day += GetMonthDays(_year, _month);        }        return *this;    }    /*Date operator++();    Date operator++(int);    Date operator--();    Date operator--(int);*/    int operator- (Date& d)     {        int count = 0;        int flg = -1;        Date min(*this);        Date max(d);        if (d < *this)        {            min = d;            max = *this;            flg = 1;        }        while (min != max)        {            count++;            max._day -= 1;        }        return count*flg;    }    //判断该月天数     //静态成员函数只能调静态函数,不能访问私有成员,因为没this指针     static int GetMonthDays(int year, int month)    {        ////年月不合法返回-1,注:不能用IsInvalid判断,否则就会递归GetMonthDays          if ((year < 1900) && (month < 1) && (month > 13))        {            return -1;        }        int month_day[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };        //月份判断开销小,放前面        //判断月份为2的话,就可以省一段开销        if ((month == 2)&&((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0))))        {            month_day[2] = 28;        }        return month_day[month];    }    //打印    void Display()    {        cout << _year << "-" << _month << "-" << _day << endl;    }private:    int _year;    int _month;    int _day;};ostream& operator<<(ostream& cout, const Date& d){    cout << d._year << "-" << d._month << "-" << d._day << endl;    return cout;}istream& operator>>(istream& cin, Date& d){    cin >> d._year >> d._month >> d._day;    return cin;}int main(){    Date d1(2017,7,7);    Date d2(2017, 7, 9);    Date d3(2017, 7, 5);    cout << d1-d2 << endl;    cout << d1-d3 << endl;}
原创粉丝点击