简单的日期类实现

主要功能:

  1. 求当前日期第days天后是哪一天?

  2. 求当前日期第days天前是哪一天?

  3. 求两个日期之间的差值

  4. 前置++ 和 后置++

  5. 前置– 和 后置–

  6. 其他运算符重载


代码实现:

date.h文件

#pragma once

#include <iostream>
using namespace std;

class Date
{
public:
    Date(int year = 2018, int month = 4, int day = 16);

    Date(const Date& d);
    // 赋值运算符重载 
    Date& operator=(const Date& d);

    // 求当前日期第days天后是哪一天? 
    Date operator+(int days);

    // 求当前日期第days天前是哪一天? 
    Date operator-(int days);

    // 求两个日期之间的差值 
    int operator-(const Date& d);

    // 前置++ 
    Date& operator++();

    // 后置++ 
    Date operator++(int);
    Date& operator--();
    Date operator--(int);


    // 判断两个日期是否相等 
    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);


    //输出友元函数
    friend std::ostream& operator<<(ostream& out, const Date &d)
    {
        out << d._year << "-" << d._month << "-" << d._day;
        return out;
    }

    //计算该月最大天数
    int MaxDay(int year, int month);
    //是否为瑞年
    bool isRyear(int year);
    //得到平年的前month(不包括month当前月)的总天数
    int  GetCount(int month);


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



date.cpp文件


#include "date.h"
//构造函数
Date::Date(int year , int month, int day)
    :_year(year),
     _month(month),
     _day(day)
{}
//拷贝构造函数
Date::Date(const Date& d):
    _year(d._year),
    _month(d._month),
    _day(d._day)
{

}
// 赋值运算符重载 
Date& Date::operator=(const Date& d)
{
    _year = d._year;
    _month = d._month;
    _day = d._day;
    return *this;
}

// 求当前日期第days天后是哪一天? 
Date Date::operator+(int days)
{
    Date tem(*this);
    tem._day += days;

    int maxday = MaxDay(tem._year, tem._month);

    while (tem._day > maxday)
    {
        if (++tem._month > 12)
        {
            tem._month = 1;
            tem._year++;
        }
        tem._day -= maxday;
        maxday = MaxDay(tem._year, tem._month);
    }
    return tem;
}

// 求当前日期第days天前是哪一天? 
Date Date::operator-(int days)
{
    Date tem(*this);
    tem._day -= days;

    int maxday;

    while (tem._day < 1)
    {
        if (--tem._month < 1)
        {
            tem._month = 12;
            tem._year--;
        }
        maxday = MaxDay(tem._year, tem._month);
        tem._day += maxday;
    }
    return tem;
}

// 求两个日期之间的差值 
int Date::operator-(const Date& d)
{
    int countRyear = 0;
    int count = 0;
    int min_year, max_year;

    if (_year > d._year){
        min_year = d._year;
        max_year = _year;
    }
    else {
        min_year = _year;
        max_year = d._year;
    }

    count += (d._year - _year) * 365;
    count -= GetCount(_month);
    count += GetCount(d._month);
    count -= _day;
    count += d._day;
    if (count > 0)
    {
        if (isRyear(_year) && _month <= 2)
            count++;
        if (isRyear(d._year) && d._month > 2)
            count++;
    }
    else if(count < 0)
    {
        if (isRyear(_year) && _month > 2)
            count--;
        if (isRyear(d._year) && d._month <= 2)
            count--;
    }


    if (count < 0)
        count = -count;

    for (int i = min_year + 1; i < max_year; ++i)
        if (isRyear(i))
            countRyear++;

    count += countRyear;
    return count;
}

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

// 后置++ 
Date Date::operator++(int)
{
    Date temp(*this);

    *this = *this + 1;

    return temp;

}
Date& Date::operator--()
{

    *this = *this - 1;

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

    *this = *this - 1;

    return temp;
}


// 判断两个日期是否相等 
bool Date::operator==(const Date& d)
{
    return (_year == d._year) && (_month == d._month) && (_day == d._day);
}

// 判断两个日期是否不等 
bool Date::operator!=(const Date& d)
{
    return !(*this == d);
}
bool Date::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 Date::operator>=(const Date& d)
{
    if (*this == d)
        return true;

    else 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 Date::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 Date::operator<=(const Date& d)
{
    if (*this == d)
        return true;

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




//#################################################################
int  Date::MaxDay(int year, int month)
{
    int d[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
    if (isRyear(year))
        d[2] = 29;
    return d[month];
}

bool  Date::isRyear(int year)
{
    return (year % 4 == 0 && year % 100 != 0 || year % 400 == 0);
}

int  Date::GetCount(int month)
{
    int cd[13] = { 0,0,31,59,90,120,151,181,212,243,273,304,334 };
    return cd[month];
}

main.cpp文件

#include "date.h"


int main()
{
    //构造函数
    Date d1(2018, 4, 16);
    Date d2(d1);//拷贝构造函数
    Date d3;

    // 求当前日期第days天后是哪一天? 
    d3 = d1 + 15;
    cout << d3 << endl;

    // 求当前日期第days天前是哪一天? 
    d3 = d1 - 31;
    cout << d3 << endl;


    // 求两个日期之间的差值 
    Date d4(2000, 2 ,3);
    int sub = d1 - d4;
    cout << d4 << "  -   "<<  d1 << " = " << sub << endl;

    //自加自减
    Date d5(2018, 4, 1);
    cout << "d5--: " << d5-- << endl;//2018-4-1
    cout << "d5++: " << d5++ << endl;//2018-3-31
    cout << "--d5: " << --d5 << endl;//2018-3-31
    cout << "++d5: " << ++d5 << endl;//2018-4-1

    //等于和不等于
    Date d6(2018, 4, 16);
    cout << d6 << " == " << d1 << " : " << (d6 == d1) << endl;//1
    cout << d6 << " != " << d1 << " : " << (d6 != d1) << endl;//0
    Date d7(2020, 4, 16);
    cout << d7 << " == " << d1 << " : " << (d7 == d1) << endl;//0
    cout << d7 << " != " << d1 << " : " << (d7 != d1) << endl;//1

    //比较
    Date d8(2000, 1, 1);
    cout << d8 << " < " << d1 << " : " << (d8 < d1) << endl;//1
    cout << d8 << " > " << d1 << " : " << (d8 > d1) << endl;//0
    cout << d8 << " <= " << d1 << " : " << (d8 <= d1) << endl;//1
    cout << d8 << " >= " << d1 << " : " << (d8 >= d1) << endl;//0
    Date d9(2222, 1, 1);
    cout << d9 << " < " << d1 << " : " << (d9 < d1) << endl;//0
    cout << d9 << " > " << d1 << " : " << (d9 > d1) << endl;//1
    cout << d9 << " <= " << d1 << " : " << (d9 <= d1) << endl;//0
    cout << d9 << " >= " << d1 << " : " << (d9 >= d1) << endl;//1
    Date d10(2018, 4, 16);
    cout << d10 << " < " << d1 << " : " << (d10 < d1) << endl;//0
    cout << d10 << " > " << d1 << " : " << (d10 > d1) << endl;//0
    cout << d10 << " <= " << d1 << " : " << (d10 <= d1) << endl;//1
    cout << d10 << " >= " << d1 << " : " << (d10 >= d1) << endl;//1

    system("pause");
    return 0;

}

猜你喜欢

转载自blog.csdn.net/xiaozuo666/article/details/79959428
今日推荐