C++完成日期类的实现--Date

实现日期类的一些功能:
操作符重载
bool operator==(const Date& d); //==
bool operator!=(const Date& d); //!=
bool operator>(const Date& d); // >
bool operator>=(const Date& d); // >=
bool operator<(const Date& d); // <
bool operator<=(const Date& d); //<=
Date operator+(int days); // 加天数
Date& operator+=(int days);// +=
Date operator-(int days); //减天数
int operator-(const Date& d); // 两个日期相隔天数
Date& operator-=(int days); //-=
Date& operator++( ); //前置++
Date operator++(int); //后置++
Date& operator–( ); //前置–
Date operator–(int); //后置 –
bool IsLeapYear(int year);//判断闰年?
int GetMonthDays( ); //一个月有多少天 ?

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

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 <= GetMonthDays()))  
        {  
            _year = year;  
            _month = month;  
            _day = day;  
        }  
        else  
        {  
            cout << "非法日期" << endl;  
            _year = 1900;  
            _month = 1;  
            _day = 1;  
        }  
    } 

//Date(const Date& d)//拷贝构造函数
//{
//  _year = d._year ;
//  _month = d._month ;
//  _day = d._day ;
//}//不用写,系统会自动生成

//~Date()//析构函数
//{
//  cout<<"~Date()"<<endl;
//}

void Print() const
{
    cout<<_year<<"-"<<_month<<"-"<<_day<<endl;
}
//d1 > d2 
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 _year == d._year
        && _month == d._month 
        && _day == d._day ;
}

bool operator != (const Date& d)
{
    //复用->可维护性
    return !(*this==d);
}

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);
}
//d1 + 100 
Date operator+(int day)
{
    if(day < 0)
    { 
        return *this-(-day);
    }//如果day为负数,则调用减法操作符函数
    Date tmp(*this);
    tmp._day += day;
    while(tmp._day > GetMonthDays())
    {
        tmp._day -= GetMonthDays();
        tmp._month++;
        if(tmp._month > 12)
        {
            tmp._year++;
            tmp._month = 1;
        }
    }
    return tmp;
}

Date operator+=(int day)
{
    *this = *this + day;
    return *this;
}
//day-10
Date operator-(int day)
{
    if(day < 0)
    {
        return *this+(-day);
    }
    Date tmp(*this);
    tmp._day = day;
    while(tmp._day <= 0)
    {
        tmp._month--;
        if(tmp._month == 0)
        {
            tmp._year--;
            tmp._month = 12;
        }
        else
        {
            --tmp._month ;
        }
        tmp._day += GetMonthDays();
    }
    return tmp;
}

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

Date operator++()//前置++
{
    /*-day++;
    if(_day > GetMonthDays())
    {
        _day -= GetMonthDays();
        _month++;
        if(_month > 12)
        {
            _year++;
            _month = 1;
        }
    }
    return *this;*/
//++也相当于加一天 复用->可维护性
    *this += 1;
    return *this;
}

Date operator++(int)//后置++ int用来区分前置和后置
{
    Date tmp(*this);
    *this += 1;
    return tmp;
}

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

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

int operator-(const Date& d)//两个日期相隔天数 
{
    Date Max(*this);  
    Date Min(d);  
    int flag = 1;  
    if ( *this < d)  
    {  
        Max = d;  
        Min= *this;  
        flag = -1;  
    }  
    int days = 0;  
    while (Max > Min)  
    {  
        --Max;  
        ++days;  
    }  
    return days*flag;  
}

bool IsLeapYear(int year)//判断是不是闰年
{
    if(((_year%4 == 0)&&(_year%100 != 0)) || (_year%400 == 0))
    {
        return true;
    }
    else
        return false;
}

int GetMonthDays()
{
    static int MonthDays[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
    int flag = IsLeapYear(_year);
    /*if(_month = 2 && (((_year%4 == 0)&&(_year%100 != 0)) || (_year%400 == 0)))*/
    if(_month = 2 && flag == 1)
    {
        return 29;
    }
    return MonthDays[_month];
}
private: 
    int _year; 
    int _month; 
    int _day; 
};

void test1()
{
    Date d1(2015,10,14);
    Date d2(2017,10,15);
    d1.Print();
    d2.Print();
    cout<<(d1 > d2)<<endl;
}

int main()
{
    test1();
    return 0;
}
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值