日期類的實現

#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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章