实现简单的日期类:Date

Date.h

#pragma once 

#include<iostream>
using namespace std;

class Date
{
public:
    Date(int year = 2018,int month = 7,int day = 19)
    :_year(year)
    ,_month(month)
    ,_day(day)
    {}
    
    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=(const Date& d);
    int GetMonthDay(int year,int month); 
    Date operator+(int day);
    Date operator+=(int day);
    Date operator-(int day);
    Date operator-=(int day);
    Date& operator++();//前置
    Date& operator--();
    Date operator++(int);
    Date operator--(int);
    int operator-(const Date& d);
  void show()
   {
    cout<<_year<<"-"<<_month<<"-"<<_day<<endl;
   }
    ~Date(){};
private:
    int _year;
    int _month;
    int _day;

};



Date.cpp

#include"Date.h"
#include<iostream>
using namespace std;
bool Date::operator>(const Date& d)
{
   if(this->_year > d._year)
   {
       return true;
   }
    else if(this->_year == d._year)
    {
        if(this->_month > d._month)
        {
            return true;
        }
        else if(this->_month == d._month)
        {
            if(this->_day > d._day)
            {
                return true;
            }
        }
    }
    return false;
}

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


bool Date::operator>=(const Date& d)
{
    if(*this > d || *this == d)
    {
        return true;
    }
    return false;
}

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

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

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

int Date::GetMonthDay(int year,int month)
{
    int GetMonthDayArr[] = {0,31,29,31,30,31,30,31,31,30,31,30,31};
    int ret = GetMonthDayArr[month];
    if(((year % 4 == 0 && year % 100 != 0) \
                || year % 400 == 0)&&(month == 2))
    {
        ret--;
    }
    return ret;
}

Date Date::operator+(int day)
{
    Date tmp(*this);
    tmp._day += day;
    if(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 Date::operator-(int day)
{
    Date tmp(*this);
    tmp._day -= GetMonthDay(tmp._year,tmp._month);
    while(tmp._day <= 0)
    {
        tmp._month--;
        if(tmp._month == 0)
        {
            tmp._year--;
        }
        tmp._day += GetMonthDay(tmp._year,tmp._month);
    }
    return tmp;
}

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

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

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

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

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

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

int Date:: operator-(const Date& d)
{
    Date tmp = *this;
    Date ret = d;
    int day = 0;
    int flag = 0;
    if(tmp < ret)
    {
        tmp = d;
        ret = *this;
        flag = 1;
    }
    while( ret != tmp )
    {
        ret += 1;
        ++day;
    }
    if(flag)
    {
        day = -day;
    }
    return day;
}

猜你喜欢

转载自blog.csdn.net/DuckyLoser/article/details/81198970