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

2024-04-19 14:04
文章标签 c++ 日期 计算器 爱恋

本文主要是介绍我与C++的爱恋:日期计算器,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!


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

🔥个人主页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),建议重载为全局函数。

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

这篇关于我与C++的爱恋:日期计算器的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/917741

相关文章

使用Python创建一个功能完整的Windows风格计算器程序

《使用Python创建一个功能完整的Windows风格计算器程序》:本文主要介绍如何使用Python和Tkinter创建一个功能完整的Windows风格计算器程序,包括基本运算、高级科学计算(如三... 目录python实现Windows系统计算器程序(含高级功能)1. 使用Tkinter实现基础计算器2.

C#如何调用C++库

《C#如何调用C++库》:本文主要介绍C#如何调用C++库方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录方法一:使用P/Invoke1. 导出C++函数2. 定义P/Invoke签名3. 调用C++函数方法二:使用C++/CLI作为桥接1. 创建C++/CL

python处理带有时区的日期和时间数据

《python处理带有时区的日期和时间数据》这篇文章主要为大家详细介绍了如何在Python中使用pytz库处理时区信息,包括获取当前UTC时间,转换为特定时区等,有需要的小伙伴可以参考一下... 目录时区基本信息python datetime使用timezonepandas处理时区数据知识延展时区基本信息

C++如何通过Qt反射机制实现数据类序列化

《C++如何通过Qt反射机制实现数据类序列化》在C++工程中经常需要使用数据类,并对数据类进行存储、打印、调试等操作,所以本文就来聊聊C++如何通过Qt反射机制实现数据类序列化吧... 目录设计预期设计思路代码实现使用方法在 C++ 工程中经常需要使用数据类,并对数据类进行存储、打印、调试等操作。由于数据类

Java实现优雅日期处理的方案详解

《Java实现优雅日期处理的方案详解》在我们的日常工作中,需要经常处理各种格式,各种类似的的日期或者时间,下面我们就来看看如何使用java处理这样的日期问题吧,感兴趣的小伙伴可以跟随小编一起学习一下... 目录前言一、日期的坑1.1 日期格式化陷阱1.2 时区转换二、优雅方案的进阶之路2.1 线程安全重构2

Linux下如何使用C++获取硬件信息

《Linux下如何使用C++获取硬件信息》这篇文章主要为大家详细介绍了如何使用C++实现获取CPU,主板,磁盘,BIOS信息等硬件信息,文中的示例代码讲解详细,感兴趣的小伙伴可以了解下... 目录方法获取CPU信息:读取"/proc/cpuinfo"文件获取磁盘信息:读取"/proc/diskstats"文

C++使用printf语句实现进制转换的示例代码

《C++使用printf语句实现进制转换的示例代码》在C语言中,printf函数可以直接实现部分进制转换功能,通过格式说明符(formatspecifier)快速输出不同进制的数值,下面给大家分享C+... 目录一、printf 原生支持的进制转换1. 十进制、八进制、十六进制转换2. 显示进制前缀3. 指

C++中初始化二维数组的几种常见方法

《C++中初始化二维数组的几种常见方法》本文详细介绍了在C++中初始化二维数组的不同方式,包括静态初始化、循环、全部为零、部分初始化、std::array和std::vector,以及std::vec... 目录1. 静态初始化2. 使用循环初始化3. 全部初始化为零4. 部分初始化5. 使用 std::a

C++ vector的常见用法超详细讲解

《C++vector的常见用法超详细讲解》:本文主要介绍C++vector的常见用法,包括C++中vector容器的定义、初始化方法、访问元素、常用函数及其时间复杂度,通过代码介绍的非常详细,... 目录1、vector的定义2、vector常用初始化方法1、使编程用花括号直接赋值2、使用圆括号赋值3、ve

Mysql表如何按照日期字段的年月分区

《Mysql表如何按照日期字段的年月分区》:本文主要介绍Mysql表如何按照日期字段的年月分区的实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、创键表时直接设置分区二、已有表分区1、分区的前置条件2、分区操作三、验证四、注意总结一、创键表时直接设置分区