我与C++的爱恋:日期计算器

embedded/2024/9/25 10:32:55/


外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

🔥个人主页guoguoqiang. 🔥专栏我与C++的爱恋

Alt

朋友们大家好啊,在我们学习了默认成员函数后,我们通过上述内容,来实现一个简易的日期计算器。


头文件的声明

#pragma once
#include <iostream>
using namespace std;#include <assert.h>
class Date {friend ostream& operator<<(ostream& out, const Date& d);friend istream& operator>>(istream& in, Date& d);
public:Date(int year = 1900, int month = 1, int day = 1);void Print() const;//直接定义类里面,它默认是inline//频繁调用int GetMonthDay(int year,int month) {assert(month > 0 && month < 13);static int monthDayArray[13] = { -1,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];}}bool CheckDate();bool operator<(const Date& d)const;bool operator<=(const Date& d)const;bool operator>(const Date& d)const;bool operator>=(const Date& d)const;bool operator==(const Date& d)const;bool operator!=(const Date& d)const;Date& operator+=(int day);Date operator+(int day)const;Date& operator-=(int day);Date operator-(int day)const;// 前置++Date& operator++();// 后置++Date operator++(int);// 后置--Date operator--(int);// 前置--Date& operator--();int operator-(const Date& d)const;
private:int _year;int _month;int _day;
};
// 重载
ostream& operator<<(ostream& out, const Date& d);
istream& operator>>(istream& in, Date& d);

函数实现

获取某年某月的天数

class Date {
public://直接定义类里面,它默认是inline//频繁调用int GetMonthDay(int year,int month) {assert(month > 0 && month < 13);static int monthDayArray[13] = { -1,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];}}

为了按月份访问数组,所以我们设置大小为13,由于要多次访问,所以将这个数组变量设置在全局。
如果是2月并且是闰年则返回29,否则就返回当前月份的天数。

1.判断日期是否合法

bool Date::CheckDate() {if(_month<1||_month>12|| _day<1 || _day>GetMonthDay(_year, _month)) {return false;}else {return true;}
}

如果不合法则返回false,合法则返回true;

2.全缺省默认构造函数

Date ::Date(int year , int month , int day) {_year = year;_month = month;_day = day;if (!CheckDate()){cout << "日期非法" << endl;}
}

在头文件中缺省源文件中不需要。

3.拷贝构造函数

Date::Date(const Date& d) {_year = d._year;_month = d._month;_day = d._day;
}

4.七个运算符重载

除了赋值运算符之外,我们只需要写一个等于和一个大于或者小于就可以简单的实现另外四个函数。

首先,==的重载

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

我们再写一个<的重载

bool  Date:: operator<(const Date& d)const {if (_year < d._year) {return true;}else if (_year == d._year) {if (_month < d._month) {return true;}else if (_month == d._month) {return _day < d._day;}}return false;
}

按年月日逐次判断。

剩余的大于小于等的重载直接复用即可

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

赋值运算符重载

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

5.日期计算函数

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

day超过该月的天数则++month,如果month==13则++year,再将month置为1月;

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

==Date& Date::operator+=(int day)==是会调用它本身然后返回修改后的对象

特点:

直接修改:它修改调用对象的状态,即增加的天数直接反映在原对象上
返回引用:返回调用它的对象的引用,允许链式操作
	Date d1(2024,  4, 15);d1 += 4;//这里d1变为2024 4 19

Date Date:: operator+(int day)const是创建一个临时变量,然后在这个临时变量上+=day,然后返回原变量+=day后的结果。
特点:
1.不直接修改,不会修改原对象,而是返回一个修改后的对象。
2.返回对象,是一个临时变量,返回原对象+=day后的结果。

	Date d1(2024,  4, 15);d1 += 4;Date d2=d1+4;//这里d2 变为2024 4 23   d1仍是2024 4 19//这里d1变为2024 4 19

如果我们要+嵌套到+=中呢?
在这里插入图片描述
对比我们能发现,两种加法都要创建一个新的变量,效果相同,但是加等,右边复用加的时候又创建对象,对比左边效率降低,所以用加复用加等效果更好

同理完成日期的减等和减

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

6.前后置+±-;

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

7.两个日期相减

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

先确定哪个日期小,再进行判别符号,再进行计算天数差通过!=直接判别(也可以使用<)不过小于的判别复杂,所以这里使用!=
++min(这里的++就是前置++);最后返回n*flag 如果flag=-1,表示第一个日期是小于第二个日期的,因此为负值。

8.重载输入输出

ostream& operator<<(ostream& out, const Date& d) {out << d._year << "年" << d._month << "月" << d._day<<"日"<<endl;return out;
}
istream& operator>>(istream& in, Date& d) {cout << "请依次输入年月日";in >> d._year >> d._month >> d._day;if (!d.CheckDate()) {cout << "日期非法" << endl;}return in;
}

operator<< 想重载为成员函数,可以,但是用起来不符合正常逻辑,不建议这样处理(因为Date*this占据一个参数位置d<<cout),建议重载为全局函数。

本篇内容到此结束,感谢大家观看!!!


http://www.ppmy.cn/embedded/8505.html

相关文章

深度学习基础

深度学习的概念&#xff1a; 深度学习的概念源于人工神经网络的研究&#xff0c;是机器学习研究中的一个新的领域&#xff0c;其动机在于建立、模拟人脑进行分析学习的神经网络&#xff0c;它模仿人脑的机制来解释数 据&#xff0c;例如图像&#xff0c;声音和文本。 机器学习…

centos修改启动项加载不同内核

一.背景&#xff1a; 虚拟机中有时需要编译好几个内核版本&#xff0c;make install后系统存在几个内核版本。需要再哪个内核上开发调试就启动特定的内核版本。这就需要修改启动时的内核版本&#xff0c;再物理机或虚拟机启动时可以上下键选择。但有时是docket云环境中或远程时…

完全小白如何用Windows电脑运行人生中的第一个Bert文本分类代码(更简单版)

诸神缄默不语-个人CSDN博文目录 我准备过几天录一个讲解视频。先等一下&#xff0c;现在只有图文版。 本文代码最早写于2024年3月27日&#xff0c;不保证未来以下代码及操作过程仍然可以使用。 本文主要关注中文仇恨检测短文本分类&#xff0c;数据集来源于datasets官网&…

C# Promise对象详解

在C#中&#xff0c;异步编程是一个核心概念&#xff0c;尤其是在需要处理I/O密集型任务或与用户界面交互的应用程序中。C#提供了多种方法来处理异步操作&#xff0c;其中最值得注意的是async和await关键字&#xff0c;以及与之相关的Task和Promise对象。在本博客中&#xff0c;…

Python爬虫入门

Python爬虫入门&#xff1a;探索网络数据的宝藏 爬虫&#xff0c;也被称为网络爬虫或网页爬虫&#xff0c;是一种自动化的网络信息检索程序。它们被广泛用于从互联网上抓取信息&#xff0c;这些信息可以用于数据分析、数据挖掘、内容摘要、搜索引擎构建等多种场景。Python作为…

气象观测站点数据下载与处理

一、下载途径 全国400多个气象站气候数据&#xff08;1942-2022&#xff09; 王晓磊&#xff1a;中国空气质量/气象历史数据 | 北京市空气质量历史数据 气象数据免费下载网站整理 中国气象站观测的气象数据怎么下载 二、R语言处理 2.1 提取站点文件 library(dplyr) library(…

restful请求风格的增删改查-----查询and添加

一、restful风格的介绍 restful也称之为REST ( Representational State Transfer )&#xff0c;可以将它理解为一种软件架构风格或设计风格&#xff0c;而不是一个标准。简单来说&#xff0c;restful风格就是把请求参数变成请求路径的一种风格。例如&#xff0c;传统的URL请求…

服务器渲染技术(JSPELJSTL)

目录 前言 一.JSP 1.基本介绍 3.page指令(常用) 4.JSP三种常用脚本 4.1 声明脚本 <%! code %> 4.2 表达式脚本 <% code %> 4.3 代码脚本 <% code %> 4.4 注释 <%-- 注释 --%> 5. JSP 内置对象 5.1 基本介绍 5.2 九个内置对象 6.JSP域对象 二…