一个比较完善的日期类(二)

日期类
class Date
{
public:
    //构造函数
    Date(int year = 1900, int month = 1, int day = 1);
    //拷贝构造,赋值,析构不需要实现,使用默认的就可以了
    //运算符重载
    Date operator+(int day); // 日期加上天数
    Date operator-(int day); // 日期减去天数
    Date& operator+=(int day); // 日期+=天数
    Date& operator-=(int day); // 日期-=天数
    Date& operator++(); //日期+1,前置++
    Date operator++(int); //日期+1,后置++
    Date& operator--(); // 日期-1,前置--
    Date operator--(int) // 日期-1,后置--
    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); // 日期比较(大于)
    int operator-(const Date& d); // 日期相差天数
private:
    bool IsInvalidDate(); //判断输入是否合法
    int getDay(int year, int month); // 获取某年某月的天数
    // << 和 >> 的重载,使用友元函数
    friend ostream& operator<<(ostream& _cout, const Date& d);
    friend istream& operator>>(istream& _cin, Date& d);
};
功能实现

函数:Date(int year = 1900, int month = 1, int day = 1);

思路:输入日期判断是否有效,若无效使用默认日期

Date(int year = 1900, int month = 1, int day = 1)
{
    //判断日期是否有效
    if (year <= 0
        || (month <= 0 || month > 12)
        || (day <= 0 || day > getDay(year, month))) {
        cout << "日期无效" << endl;
        cout << "日期重置为: 1900-1-1" << endl;
        _year = 1900;
        _month = 1;
        _day = 1;
    }
    else {
        _year = year;
        _month = month;
        _day = day;
    }
}

函数:Date operator+(int day);

功能:一个日期加上天数,最终得到的日期是多少

思路:当前日期的day加上输入的day,如果大于当前日期天数,就让相加的day减去当前月份的天数,判断月份是否加一,年份是否加一,循环直到day小于某个月的天数

Date operator+(int day)
{
    // 常规思路
    /*if (day < 0) { // 日期小于0,用日期减法(减去[0-当前天数])
        return ((*this) - (0 - day));
    }
    Date temp(*this); // 拷贝构造当前日期
    temp._day += day; // 加上天数
    int daysOfMonth = 0;
    while (temp._day > (daysOfMonth = getDay(temp._year, temp._month))) // 相加的天数比这个月天数多
    {
        temp._day -= daysOfMonth; // 减去当前月份的天数
        ++temp._month; //月份加一
        if (temp._month > 12) // 月份大于12,年份加一
        {
            temp._year += 1;
            temp._month = 1;
        }
    }*/
    // 思路二:采用+=操作
    Date tmp(*this); // 拷贝构造
    return tmp += day; // +=
}

函数:Date operator-(int day);

功能:一个日期减去天数,最终得到的日期是多少

思路:和加法的思路相同,比较便捷的方式采用-=

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

函数:Date operator+=(int day);

功能:日期+=天数

思路:判断日期正负,日期累加,判断日期是否溢出

Date& operator+=(int day)
{
    //0. 判断日期正负
    if (day < 0) {
        return *this -= -day;
    }
    //1. 日期累加
    _day += day;
    //2. 判断日期是否溢出
    while (_day > getDay(_year, _month)) {
        //月份进位,首先减去当前月的天数
        _day -= getDay(_year, _month);
        _month++;
        //判断月份是否溢出
        if (_month == 13) {
            _month = 1;
            ++_year;
        }
    }
    return *this;
}

在这里插入图片描述

函数:Date operator-=(int day);

功能:日期-=天数

思路:判断日期正负,天数相减,判断日期是否小于等于0

Date& operator-=(int day)
{
    //0. 判断是否为负数
    if (day < 0) {
        return *this += -day;
    }
    //1. 天数相减
    _day -= day;
    //2. 判断日期是否小于等于0
    while (_day <= 0) {
        //月份回退
        --_month;
        //判断月份是否为0
        if (_month == 0) {                
            --_year;
            _month = 12;
        }
        _day += getDay(_year, _month);                      
    }
    return *this;
}

在这里插入图片描述

函数:Date& operator++();

功能:前置++

思路:++d:加1,返回相加之后的内容

d.operator++(),返回值或者引用(值的效率低)

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

函数:Date operator++(int);

功能:后置++

思路:d++:加1,返回相加之前的内容;参数没有实际的意思,作为标记,返回只能是值(返回的是临时对象)

Date operator++(int)
{
    //保存未修改之前的内容
    Date temp(*this);
    *this += 1;
    //返回未修改之前的内容
    return temp;
}

函数:Date& operator–();

功能:前置–

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

函数:Date operator–(int);

功能:后置–

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

函数:bool operator<(const Date& 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);

功能:日期比较(等于)

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

函数:bool operator<=(const Date& d);

功能:日期比较(小于等于)

bool operator<=(const Date& d)
{
    return operator<(d) || operator==(d);
}

函数:bool operator>=(const Date& d);

功能:日期比较(大于等于)

思路:不小于

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

函数:bool operator!=(const Date& d);

功能:日期比较(不等于)

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

函数:bool operator>(const Date& d);

功能:日期比较(大于)

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

函数:int operator-(const Date& d);

功能:日期相差天数

思路:大小比较,小的日期累加,判断日期是否相等,输出累加的天数

int operator-(const Date& d)
{
    //相加,判等,大小比较
    Date max = *this;
    Date min = d;
    int flag = 1;
    if (min > max) { //min.operator>(max)
        min = *this;
        max = d;
        flag = -1;
    }
    int day = 0;
    while (min < max) { // min.operator<(max)
        ++day;
        ++min;  // min.operator++()
    }
    return day * flag; 
}

在这里插入图片描述

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

功能:<< 的重载,使用友元函数

ostream& operator<<(ostream& _cout, const Date& d)
{
	_cout << d._year << "-" << d._month << "-" << d._day;
	return _cout;
}

函数:friend istream& operator>>(istream& _cin, Date& d);

功能:>> 的重载,使用友元函数

istream& operator>>(istream& _cin, Date& d)
{
	_cin >> d._year >> d._month >> d._day;
	if (!d.IsInvalidDate())
	{
		cout << "输入日期错误!" << d << endl;
	}
	return _cin;
}

函数:bool IsInvalidDate();

功能:判断输入是否合法

bool IsInvalidDate()
{
    if ((_year > 0) && (_month > 0 && _month < 13) && (_day > 0 && _day <= getDay(_year, _month)))
    {
        return true;
    }
    return false;
}

函数:int getDay(int year, int month);

功能:获取某年某月的天数

int getDay(int year, int month) 
{
    static int days[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
    int day = days[month];
    if (month == 2) {
        //判断是否为闰年
        if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
            ++day;
        }
    }
    return day;
}
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值