0


【C ++基础】第五篇 类和对象 日期计算器

【C ++基础】第五篇 类和对象 日期计算器

写在前面

更新情况记录:
最近更新时间更新次数2022/10/141
参考博客与书籍以及链接:
(非常感谢这些博主们的文章,将我的一些疑问得到解决。)
参考博客链接或书籍名称总目录:https://blog.csdn.net/m0_54381284/article/details/126810194


正文

文章目录

1.前置知识

直接上我写的博客

C++类和对象

2.功能描述

日期的运算

日期+=天数、日期+天数、日期-天数、日期-=天数、日期前置++、日期后置++、日期前置–、日期后置++、日期-日期返回一个天数。

日期之前比较大小

==、>、>=、<、<=、!=。

3.功能接口及日期类的声明

Date.h

using namespace std;

class Date
{
public:
    // 获取某年某月的天数
    int GetMonthDay(int year, int month);
    //全缺省的构造函数
    Date(int year = 1900, int month = 1, int day = 1);
    //拷贝构造函数
    Date(const Date& d);
    //赋值运算符重载
    Date& operator = (const Date & d);
    // 析构函数
    ~Date();
    // 日期+=天数
    Date& operator+=(int day);
    // 日期+天数
    Date operator+(int day);
    // 日期-天数
    Date operator-(int day);
    // 日期-=天数
    Date& operator-=(int day);
    // 前置++
    Date& operator++();
    // 后置++
    Date operator++(int);
    // 后置--
    Date operator--(int);
    // 前置--
    Date& operator--();
    // >运算符重载
    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);
    //打印日期
    void Print()
    {
        cout << _year << "-" << _month << "-" << _day << endl;
    }
private:
    int _year;
    int _month;
    int _day;
};

4.接口的实现

没有特别说明这里面代码都是放在Date.cpp中。

1.构造函数

写成全缺省的,不需要编译器自己构造

Date.h

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

Date.cpp

Date::Date(int year, int month, int day)
{
    _year = year;
    _month = month;
    _day = day;
}
2.析构函数

又没有开辟空间,编译器自己的够用。

Date::~Date()
{
}
3.获取某年某月的天数
int Date::GetMonthDay(int year, int month)
{
    //加static免得每次调用都要构造
    static int monthDayArray[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
    if (month == 2 && (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
    {
        return 29;
    }
    else
    {
        return monthDayArray[month];
    }
}
4.赋值运算符重载
Date& Date::operator = (const Date& d)
{
    _year = d._year;
    _month = d._month;
    _day = d._day;
    return *this;
}
5.日期+=天数
Date& Date::operator+=(int day)
{
    _day += day;
    while (_day > GetMonthDay(_year, _month))
    {
        _day -= GetMonthDay(_year, _month);
        _month++;

        if (_month == 13)
        {
            ++_year;
            _month = 1;
        }
    }
    return *this;
}
6.日期+天数
Date Date::operator+(int day)
{
    Date ret(*this);
    ret += day;
    return ret;
}
7.日期-天数
Date Date::operator-(int day)
{
    Date ret(*this);
    ret -= day;
    return *this;
}
8.日期-=天数
Date& Date::operator-=(int day)
{
    _day -= day;
    while (_day <= 0)
    {
        _day+= GetMonthDay(_year, _month);
        _month--;

        if (_month == 0)
        {
            --_year;
            _month = 12;
        }
    }
    return *this;
}
9.前置++

效果:前置++,每次先++,然后再用

Date& Date::operator++()
{
    *this += 1;
    return *this;
}
10.后置++

效果:后置++,每次先用(保持原值),然后再++

Date Date::operator++(int)
{
    //返回的值没有加加
    Date tmp = *this;
    //但实际上已经加加了
    *this += 1;
    //返回的值没有加加
    return tmp;
}

一个小问题
编译器是如何辨别前置++与后置++的?

规定:后置++的形参加一个int,就像这样operator++(int);
假如没有的话,就会变成下面这个样子:

++d1;//转换成d1.operator++();
d1++;//转换成d2.operator++();
//这样就无法区分了

综上所述:需要加不加形参来辨别前置++与后置++;
下面的前置–与后置–也是同样的道理;

11.后置–
Date Date::operator--(int)
{
    Date tmp = *this;
    *this -= 1;
    return tmp;
}
12.前置–
Date& Date::operator--()
{
    *this -= 1;
    return *this;
}
13.>运算符重载
bool Date::operator>(const Date& d)
{
    if (_year > d._year)
    {
        return true;
    }
    else if (_year == d._year && _month > d._month)
    {
        return true;
    }
    else if (_year == d._year && _month == d._month && _day > d._day)
    {
        return true;
    }
    return false;
}
14. ==运算符重载
bool Date::operator==(const Date& d)
{
    return (_year == d._year && _day == d._day && _month == d._month);
}
15. >=运算符重载

可以复用之前写的代码,来降低代码量。

bool Date::operator >= (const Date& d)
{
    return *this>d||*this==d;
}
16.<运算符重载
bool Date::operator < (const Date& d)
{
    return !(*this > d);
}
17.<=运算符重载
bool Date::operator <= (const Date& d)
{
    return *this < d || *this == d;
}
18.!=运算符重载
bool Date::operator != (const Date& d)
{
    return !(*this == d);
}
19.日期-日期 返回天数

不需要条件判断,用一个n来计数。

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

5.完整代码

1.Date.h
#pragma once
#include<iostream>
using namespace std;

class Date
{
public:
     获取某年某月的天数
    int GetMonthDay(int year, int month);
    全缺省的构造函数
    Date(int year = 1900, int month = 1, int day = 1);
    拷贝构造函数
    Date(const Date& d);
    赋值运算符重载
    Date& operator = (const Date & d);
     析构函数
    ~Date();
    /// 日期+=天数
    Date& operator+=(int day);
    /// 日期+天数
    Date operator+(int day);
    /// 日期-天数
    Date operator-(int day);
    /// 日期-=天数
    Date& operator-=(int day);
    // 前置++
    Date& operator++();
    // 后置++
    Date operator++(int);
    // 后置--
    Date operator--(int);
    // 前置--
    Date& operator--();
    /// >运算符重载
    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);
    ///打印日期
    void Print()
    {
        cout << _year << "-" << _month << "-" << _day << endl;
    }
private:
    int _year;
    int _month;
    int _day;
};
2.Date.cpp
#define _CRT_SECURE_NO_WARNINGS
#include "Date.h"

Date::Date(int year, int month, int day)
{
    _year = year;
    _month = month;
    _day = day;
}
Date::~Date()
{

}
Date::Date(const Date& d)
{
    _year = d._year;
    _month = d._month;
    _day = d._day;
}
int Date::GetMonthDay(int year, int month)
{
    static int monthDayArray[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
    if (month == 2 && (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
    {
        return 29;
    }
    else
    {
        return monthDayArray[month];
    }
}

Date& Date::operator = (const Date& d)
{
    _year = d._year;
    _month = d._month;
    _day = d._day;
    return *this;
}
Date& Date::operator+=(int day)
{
    _day += day;
    while (_day > GetMonthDay(_year, _month))
    {
        _day -= GetMonthDay(_year, _month);
        _month++;

        if (_month == 13)
        {
            ++_year;
            _month = 1;
        }
    }
    return *this;
}
Date Date::operator+(int day)
{
    Date ret(*this);
    ret += day;
    return ret;
}
Date Date::operator-(int day)
{
    Date ret(*this);
    ret -= day;
    return *this;
}
Date& Date::operator-=(int day)
{
    _day -= day;
    while (_day <= 0)
    {
        _day+= GetMonthDay(_year, _month);
        _month--;

        if (_month == 0)
        {
            --_year;
            _month = 12;
        }
    }
    return *this;
}
bool Date::operator>(const Date& d)
{
    if (_year > d._year)
    {
        return true;
    }
    else if (_year == d._year && _month > d._month)
    {
        return true;
    }
    else if (_year == d._year && _month == d._month && _day > d._day)
    {
        return true;
    }
    return false;
}
bool Date::operator==(const Date& d)
{
    return (_year == d._year && _day == d._day && _month == d._month);
}
bool Date::operator >= (const Date& d)
{
    return *this>d||*this==d;
}
bool Date::operator < (const Date& d)
{
    return !(*this > d);
}
bool Date::operator <= (const Date& d)
{
    return *this < d || *this == d;
}
bool Date::operator != (const Date& d)
{
    return !(*this == d);
}
int Date::operator-(const Date& d)
{
    Date max = *this;
    Date min = d;
    int flag = 1;
    if (max < min)
    {
        max = d;
        min = *this;
        flag = -1;
    }
    int n = 0;
    while (max != min)
    {
        n++;
        min+=1;
    }
    return n * flag;
}

Date& Date::operator++()
{
    *this += 1;
    return *this;
}
Date Date::operator++(int)
{
    //返回的值没有加加
    Date tmp = *this;
    //但实际上已经加加了
    *this += 1;
    //返回的值没有加加
    return tmp;
}
Date& Date::operator--()
{
    *this -= 1;
    return *this;
}
Date Date::operator--(int)
{
    Date tmp = *this;
    *this -= 1;
    return tmp;
}

观众老爷们自己写测试用例吧。
本文完毕。

标签: c语言 c++

本文转载自: https://blog.csdn.net/m0_54381284/article/details/127329216
版权归原作者 潮.eth 所有, 如有侵权,请联系我们删除。

“【C ++基础】第五篇 类和对象 日期计算器”的评论:

还没有评论