日期类的实现

来源:互联网 发布:熊片数据库手机版 2.1 编辑:程序博客网 时间:2024/05/26 02:55
#include <iostream>using namespace std;class Date{    friend ostream& operator<<(ostream& _cout, const Date& date);    friend istream& operator>>(istream& _cin, Date& date);public:    Date(int year, int month, int day)        : _year(year)        , _month(month)        , _day(day)    {}    Date(const Date& d)        : _year(d._year)        , _month(d._month)        , _day(d._day)    {}    Date& operator=(const Date& d);        // 计算当前日期day天之后日期     Date& operator-(int day);    // 计算当前日期day天之前日期     Date& operator+(int day);    // 计算两个日期之间差距     int operator-(const Date& d);    //前置++     Date& operator++();        // 后置++     Date operator++(int);    Date& operator--();    Date operator--(int);    bool operator>(const Date& d);    bool operator<(const Date& d);    bool operator==(const Date& d);    bool operator!=(const Date& d);    // 重载输出运算符 private:    bool IsLeapYear(int year)    {        return year % 400 == 0 || (year % 4 == 0 && year % 100 != 0);    }    int GetDayInMonth(int year, int month)    {        int months[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30,31 };        if(IsLeapYear(year))        {            months[2] = 29;        }        return months[month];    }private:    int _year;    int _month;    int _day;};ostream& operator<<(ostream& _cout, const Date& date){    _cout << date._year << "/" << date._month << "/" << date._day;    return _cout;}istream& operator>>(istream& _cin, Date& date){    _cin >> date._year >> date._month >> date._day;    return _cin;}Date& Date::operator=(const Date& date){    if (this != &date)    {        _year = date._year;        _month = date._month;        _day = date._day;    }    return *this;}Date& Date::operator+(int day){    Date& tmp = *this;    tmp._day += day;    while (tmp._day > GetDayInMonth(tmp._year, tmp._month))    {        tmp._day -= GetDayInMonth(tmp._year, tmp._month);        if (tmp._month == 12)        {            tmp._year++;            tmp._month = 1;        }        else            tmp._month++;    }    return tmp;}Date& Date::operator-(int day){    Date& tmp = *this;    tmp._day -= day;    while (tmp._day < 0)    {        if (tmp._month == 1)        {            tmp._year--;            tmp._month = 12;            }        else        {            --tmp._month;        }        tmp._day += GetDayInMonth(tmp._year, tmp._month);    }    return tmp;}int Date::operator-(const Date& date){    Date datemax = *this;    Date datemin = date;    int days = 0;    if (datemin > datemax)    {        datemax = date;        datemin = *this;    }    while (1)    {        if (datemin + days == datemax)            break;        days++;    }    return days;}//前置Date& Date::operator++(){    Date& tmp = *this;    tmp._day++;    if (tmp._day > GetDayInMonth(tmp._year, tmp._month))    {        if (tmp._month == 12)        {            tmp._year++;            tmp._month = 1;            tmp._day = 1;        }        tmp._month++;        tmp._day = 1;    }    return tmp;    //return (*this = *this +1);}Date& Date::operator--(){    return (*this = *this - 1);}//后置Date Date::operator++(int day){    Date tmp = *this;    *this = *this + 1;    return tmp;}Date Date::operator--(int day){    Date tmp(*this);    *this = *this - 1;    return tmp;}bool Date::operator>(const Date& d){    if (this->_year > d._year || (_year == d._year && _month > d._month) || (_year == d._year && _month == d._month && _day > d._day))        return true;    else        return false;}bool Date::operator<(const Date& d){    if (this->_year < d._year || (_year == d._year && _month < d._month) || (_year == d._year && _month == d._month && _day < d._day))        return true;    else        return false;}bool Date::operator==(const Date& d){    if (_year == d._year && _month == d._month && _day == d._day)        return true;    else        return false;}bool Date::operator!=(const Date& d){    if (_year == d._year && _month == d._month && _day == d._day)        return false;    else        return true;}void FunTest(){    Date date(2016, 11, 17);    cout << ++date << endl;    cout << --date << endl;    cout << date << endl;    cout << date + 9 << endl;    cout << date - 9 << endl;    cout << date-- << endl;    cout << date++ << endl;    cout << date << endl;    date = Date(2016, 10, 17);    cout << date + 100 << endl;    Date date2(1996, 3, 1);    cout << date2 - 100 << endl;    cout << (date - date2) << endl;}int main(){    FunTest();    getchar();    return 0;}
原创粉丝点击