萬年曆

#include<iostream>
using namespace std;


class Date
{
public:
    // 初始化列表進行初始化。 
    Date(int year = 1900, int month = 1, int day = 1) //構造函數
        :_year(year)
        , _month(month)
        , _day(day)
    {
        if (_year < 1900
            || _month < 1 || _month > 12 || _day < 1 || _day > GetMonthDay(year,month))
        {
            cout << "Invalue Date" << endl; //如果傳入非法日期就恢復初始日期
            _year = 1900;
            _month = 1;
            _day = 1;
        }
    }
    Date(const Date& d)//拷貝構造
        :_year(d._year)
        , _month(d._month)
        , _day(d._day)
    {

    }
    Date& operator= (const Date& d)
    {
        _year = d._year;
        _month = d._month;
        _day = d._day;
        return *this;
    }
    void Display()
    {
        cout << _year << "-" << _month << "-" << _day << endl;
    }

    static int GetMonthDay(int year,int month)
    {
        int MonthDay[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
        if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) //判斷閏年
        {
            MonthDay[2] = 29;
        }
        else
        {
            MonthDay[2] = 28;
        }
        return MonthDay[month];

    }
public:
    //
    // 比較日期的運算符重載
    //
    bool operator == (const Date& d)
    {
        /*if ((_year == d._year) && (_month == d._month) && (_day == d._day))
        {
            return true;
        }
        else
        {
            return false;
        }*/
        return (_year == d._year) && (_month == d._month) && (_day == d._day);
    }
    bool operator != (const Date& d)
    {
        return !(*this == d);
    }
    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 || *this == d;
    }
    bool operator < (const Date& d)
    {
        return !(*this >= d);
    }
    bool operator <= (const Date& d)
    {
        return !(*this > d);
    }

    //
    // 計算一個日期加減一個天數以後的日期。
    //
    Date operator+(int day)
    {
        Date tmp(*this);
        tmp._day += day;
        while (tmp._day > GetMonthDay(tmp._year, tmp._month))
        {
            tmp._day -= GetMonthDay(tmp._year, tmp._month);
            tmp._month++;

            if (tmp._month > 12)
            {
                tmp._year++;
                tmp._month = 1;
            }
        }
        return tmp;
    }
    Date operator-(int day)
    {
        Date tmp(*this);
        tmp._day -= day;
        while (tmp._day <= 0)
        {
            tmp._month--;
            if (tmp._month < 1)
            {
                tmp._year--;
                tmp._month = 12;
            }
            tmp._day += GetMonthDay(tmp._year, tmp._month);

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

    const Date& operator++()    // 前置++
    {
        (*this) += 1;
        return (*this);
    }
    Date operator++(int)        // 後置++
    {
        Date tmp(*this);
        (*this) += 1;
        return tmp;
    }
    const Date& operator--()    // 前置-- 
    {
        *this -= 1;
        return *this;
    }
    Date operator--(int)        // 後置--
    {
        Date tmp(*this);
        (*this) -= 1;
        return tmp;
    }

    //
    // 兩個日期相加沒有意義
    // 計算兩個日期相減以後的差的天數
    //
    int operator-(const Date& d)
    {
        Date d1(*this), d2(d);
        if (*this < d)
        {
            Date tmp = d1;
            d1 = d2;
            d2 = tmp;
        }
        int count = 0;
        while (d1 > d2)
        {
            count++;
            d2++;
        }
        cout<<count<<endl;
        return count;
    }
private:
    int _year;
    int _month;
    int _day;


};

void TestDate()
{}
int main()
{
    Date d1(1995,4,19),d2(1995,4,19),d3,d4;
    d1.Display();
    cout << "d1 = d2 ?" << (d1 == d2) << endl;
    cout << "d1 != d2 ?" << (d1 != d2) << endl;
    cout << "d1 > d2 ?" << (d1 > d2) << endl;
    cout << "d1 >= d2 ?" << (d1 >= d2) << endl;
    cout << "d1 < d2 ?" << (d1 < d2) << endl;
    d3 = d1 + (-19);
    d3.Display();
    d4 = d2 - 365;
    d4.Display();
    /*d2 -= 30;
    d2.Display();
    d1 += 30;
    d1.Display();*/
    /*d1--;
    d1.Display();*/
    d1 - d2;

    getchar();
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章