我与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

相关文章

Java调用C++动态库超详细步骤讲解(附源码)

《Java调用C++动态库超详细步骤讲解(附源码)》C语言因其高效和接近硬件的特性,时常会被用在性能要求较高或者需要直接操作硬件的场合,:本文主要介绍Java调用C++动态库的相关资料,文中通过代... 目录一、直接调用C++库第一步:动态库生成(vs2017+qt5.12.10)第二步:Java调用C++

C/C++错误信息处理的常见方法及函数

《C/C++错误信息处理的常见方法及函数》C/C++是两种广泛使用的编程语言,特别是在系统编程、嵌入式开发以及高性能计算领域,:本文主要介绍C/C++错误信息处理的常见方法及函数,文中通过代码介绍... 目录前言1. errno 和 perror()示例:2. strerror()示例:3. perror(

C++变换迭代器使用方法小结

《C++变换迭代器使用方法小结》本文主要介绍了C++变换迭代器使用方法小结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录1、源码2、代码解析代码解析:transform_iterator1. transform_iterat

详解C++中类的大小决定因数

《详解C++中类的大小决定因数》类的大小受多个因素影响,主要包括成员变量、对齐方式、继承关系、虚函数表等,下面就来介绍一下,具有一定的参考价值,感兴趣的可以了解一下... 目录1. 非静态数据成员示例:2. 数据对齐(Padding)示例:3. 虚函数(vtable 指针)示例:4. 继承普通继承虚继承5.

C++中std::distance使用方法示例

《C++中std::distance使用方法示例》std::distance是C++标准库中的一个函数,用于计算两个迭代器之间的距离,本文主要介绍了C++中std::distance使用方法示例,具... 目录语法使用方式解释示例输出:其他说明:总结std::distance&n编程bsp;是 C++ 标准

C++ 中的 if-constexpr语法和作用

《C++中的if-constexpr语法和作用》if-constexpr语法是C++17引入的新语法特性,也被称为常量if表达式或静态if(staticif),:本文主要介绍C++中的if-c... 目录1 if-constexpr 语法1.1 基本语法1.2 扩展说明1.2.1 条件表达式1.2.2 fa

Python使用date模块进行日期处理的终极指南

《Python使用date模块进行日期处理的终极指南》在处理与时间相关的数据时,Python的date模块是开发者最趁手的工具之一,本文将用通俗的语言,结合真实案例,带您掌握date模块的六大核心功能... 目录引言一、date模块的核心功能1.1 日期表示1.2 日期计算1.3 日期比较二、六大常用方法详

C++中::SHCreateDirectoryEx函数使用方法

《C++中::SHCreateDirectoryEx函数使用方法》::SHCreateDirectoryEx用于创建多级目录,类似于mkdir-p命令,本文主要介绍了C++中::SHCreateDir... 目录1. 函数原型与依赖项2. 基本使用示例示例 1:创建单层目录示例 2:创建多级目录3. 关键注

C++从序列容器中删除元素的四种方法

《C++从序列容器中删除元素的四种方法》删除元素的方法在序列容器和关联容器之间是非常不同的,在序列容器中,vector和string是最常用的,但这里也会介绍deque和list以供全面了解,尽管在一... 目录一、简介二、移除给定位置的元素三、移除与某个值相等的元素3.1、序列容器vector、deque

C++常见容器获取头元素的方法大全

《C++常见容器获取头元素的方法大全》在C++编程中,容器是存储和管理数据集合的重要工具,不同的容器提供了不同的接口来访问和操作其中的元素,获取容器的头元素(即第一个元素)是常见的操作之一,本文将详细... 目录一、std::vector二、std::list三、std::deque四、std::forwa