运算符重载的应用:日期类

#define _CRT_SECURE_NO_WARNINGS 1

#include<iostream>
using namespace std;

class Date
{
public:
        Date(int year = 1900, int month = 1, int day = 1)
    {
            if (year > 0 && (month > 0 && month < 13) &&
                (day > 0 && day < Getcount(year, month)))
            {
                _year = year;
                _month = month;
                _day = day;
            }
            else
                cout << "the day is illegal" << endl;
    }

        Date(const Date& d)
            :_year(d._year)
            , _month(d._month)
            , _day(d._day)
        {}

    //合法性判断
    bool leap_year(int year)
    {
        if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0))
        {
            return true;
        }
        else
            return false;
    }

    int Getcount(int year,int month)
    {
        int arr[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
        if (leap_year(year) && (2 == month))
        {
            arr[month] += 1;
        }
        return arr[month];
    }

    bool 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;
        }
        else
            return false;
    }

    bool 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;
        }
        else
            false;
    }

    bool operator == (const Date &d)
    {
        return ((_day == d._day)
            && (_month == d._month)
            && (_year == d._year));
    }

    bool operator != (const Date &d)
    {
        return((_year != d._year)
            && (_month != d._month)
            && (_day != d._day));
    }


    friend ostream& operator<<(ostream &_cout, const Date& d);

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

    Date operator+(int days)
    {
        Date temp(*this);
        if (days < 0)
        {
            days = -days;
            return temp - days;
        }
        temp._day += days;
        while (temp._day > Getcount(temp._year, temp._month))
        {
            temp._day -= Getcount(temp._year, temp._month);
            if (temp._month == 12)
            {
                temp._year ++;
                temp._month = 1;
            }
            else
            {
                temp._month++;
            }
        }
        return temp;
    }

    Date operator-(int days)
    {
        Date temp(*this);
        if (days < 0)
        {
            days = -days;
            return temp + days;
        }
        temp._day -= days;
        while (temp._day <= 0)
        {
            temp._day += Getcount(temp._year, temp._month);
            if (temp._month == 1)
            {
                temp._year -= 1;
                temp._month = 12;
            }
            else
            temp._month--;
        }
        return temp;
    }

    int operator-(const Date& d)
    {
        Date maxdate(*this);
        Date mindate(d);
        if (maxdate < mindate)
        {
            maxdate = d;
            mindate = *this;
        }
        int days = 0;
        while (mindate != maxdate)
        {
            days++;
        }
        return days;
    }


    Date& operator--()//前置--
    {
        return (*this = *this - 1);
    }

    Date operator--(int)//后置--
    {
        Date temp = (*this);
        _day -= 1;
        return temp;
    }

    Date& operator++()//前置++
    {
        _day += 1;
        return (*this);
    }

    Date operator++(int)//后置++
    {

            Date tmp = (*this);
            _day += 1;
            return tmp;
    }

    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;
}



int main()
{
    /*Date d;

    system("pause\n");*/
    Date d(2017, 3, 15);
    d =d + 20;
    d.display();
    system("pause\n");
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值