天天看點

日期類之運算符重載

①date.h
#pragma once 

#include <iostream>
#include <assert.h>

using namespace std;

class Date
{
public:
    Date(int year = , int month = , int day = )
        :_year(year)
        , _month(month)
        , _day(day)
    {
        if (!IsInvalid())
        {
            assert(false);
        }
    }

    Date(const Date& d)     //拷貝構造
    {
        _year = d._year;
        _month = d._month;
        _day = d._day;
    }

    ~Date(){}

    bool IsInvalid();
    void Show();

    Date& operator=(const Date& d);   //指派運算符重載

    bool operator==(const Date& d); //==重載
    bool operator!=(const Date& d); //!=重載

    bool operator>=(const Date& d); //>=重載
    bool operator<=(const Date& d); //<=重載

    // d1 < d2
    bool operator>(const Date& d);      //>重載
    // d1 > d2 
    bool operator<(const Date& d);      //<重載

    // d1 + 10 
    Date operator+(int day);            //+重載
    Date& operator+=(int day);      //+=重載

    Date operator-(int day);            //-重載
    Date& operator-=(int day);      //-=重載

    //++d1 
    Date& operator++();             //前置++ 
    //d1++ 
    Date operator++(int);               //後置++

    Date& operator--();             //前置-- 
    Date operator--(int);               //後置--

    int operator-(const Date& d);       //計算兩個日期相差天數

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

           
②date.cpp
#include "date.h"

void Date::Show()
{
    cout << _year << "-" << _month << "-" << _day << endl;
}

bool IsLeapYear(int year)
{
    if (year %  ==  && year %  !=  || year %  == )
    {
        return true;
    }
    return false;
}

int GetMonthDay(int year, int month)
{
    int array[] = { -, , , , , , , , , , , ,  };
    int day = array[month];
    if (month ==  && IsLeapYear(year))
    {
        day = ;
    }
    return day;
}

bool Date::IsInvalid()
{
    return _year >= 
        && _month >=  && _month <= 
        && _day >  && _day <= GetMonthDay(_year, _month);
}

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


bool Date::operator==(const Date& d)        //隐含的this指針
{
    return this->_year == d._year
        && this->_month == d._month
        && this->_day == d._day;
}

bool Date::operator!=(const Date& d)
{
    return this->_year != d._year
        || this->_month != d._month
        || this->_day != d._day;
}

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

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

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

bool Date::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;
}


Date Date::operator+(int day)       //本身并沒有改變,不能傳回引用
{
    Date ret(*this);
    ret._day += day;
    while (ret._day > GetMonthDay(ret._year, ret._month))
    {
        int monthday = GetMonthDay(ret._year, ret._month);
        ret._day -= monthday;
        ret._month++;
        if (ret._month > )
        {
            ret._month = ;
            ret._year++;
        }
    }
    return ret;
}

Date& Date::operator+=(int day)
{
    *this = *this + day;
    return *this;
}

Date Date::operator-(int day)
{
    if (day < )
    {
        return *this + (-day);
    }
    Date ret(*this);
    ret._day -= day;
    while (ret._day < )
    {
        if (ret._month == )
        {
            ret._month = ;
            ret._year--;
        }
        else
        {
            ret._month--;
        }
        int monthday = GetMonthDay(ret._year, ret._month);
        ret._day += monthday;
    }
    return ret;
}

Date& Date::operator-=(int day)
{
    *this = *this - day;
    return *this;
}

Date& Date::operator++() // 前置(效率高)
{
    *this += ;
    return *this;
}

Date Date::operator++(int) // 後置 
{
    Date tmp(*this);
    tmp = *this + ;
    return tmp;
}

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


Date Date::operator--(int)
{
    Date tmp(*this);
    tmp = *this - ;
    return tmp;
}

int Date::operator-(const Date& d)      //計算兩個日期相差天數
{
    //先确定哪個日期小,然後将小的往大的加,知道兩個日期相等,就得到相差天數
    int flag = ;
    Date max(*this);
    Date min(d);
    if (*this < d)
    {
        min = *this;
        max = d;
        flag = -;
    }
    int days = ;
    while (min < max)
    {
        min++;
        days++;
    }
    return days * flag;
}



int main()
{
    Date d1(, , );
    d1.Show();
    Date d2(, , );
    d2 = d1;
    d2.Show();

    cout << "測試重載==:" << d1.operator==(d2) << endl;

    cout << "測試重載!=:" << d1.operator!=(d2) << endl;

    cout << "測試重載>:" << d1.operator>(d2) << endl;

    cout << "測試重載<:" << d1.operator<(d2) << endl;

    cout << "測試重載>=:" << d1.operator>=(d2) << endl;

    cout << "測試重載<=:" << d1.operator<=(d2) << endl;

    Date d3 = d2.operator+();
    d3.Show();

    d2.operator+=();
    d2.Show();

    Date d4 = d2.operator-(-);
    d4.Show();

    d2.operator-=();
    d2.Show();

    Date d5 = d2++;
    d5.Show();

    ++d2;
    d2.Show();

    --d2;
    d2.Show();

    Date d6 = d2--;
    d6.Show();

    Date d(, , );
    Date d7(, , );
    d.Show();
    d7.Show();
    int days = d7 - d;
    cout << " 相差天數:" << days << endl;

    system("pause");
    return ;
}
           

繼續閱讀