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


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

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

头文件的声明

cpp 复制代码
#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);

函数实现

获取某年某月的天数

cpp 复制代码
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.判断日期是否合法

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

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

2.全缺省默认构造函数

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

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

3.拷贝构造函数

cpp 复制代码
Date::Date(const Date& d) {
	_year = d._year;
	_month = d._month;
	_day = d._day;
}

4.七个运算符重载

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

首先,==的重载

cpp 复制代码
bool Date:: operator==(const Date& d)const {
	return _year == d._year
		&& _month == d._month
		&& _day == d._day;
}

我们再写一个<的重载

cpp 复制代码
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;
}

按年月日逐次判断。

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

cpp 复制代码
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);
}

赋值运算符重载

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

5.日期计算函数

cpp 复制代码
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月;

cpp 复制代码
Date Date:: operator+(int day)const {
	Date tmp = *this;
	tmp += day;
	return tmp;
}

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

特点:

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

Date Date:: operator+(int day)const是创建一个临时变量,然后在这个临时变量上+=day,然后返回原变量+=day后的结果。
特点:

1.不直接修改,不会修改原对象,而是返回一个修改后的对象。

2.返回对象,是一个临时变量,返回原对象+=day后的结果。

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

如果我们要+嵌套到+=中呢?

对比我们能发现,两种加法都要创建一个新的变量,效果相同,但是加等,右边复用加的时候又创建对象,对比左边效率降低,所以用加复用加等效果更好

同理完成日期的减等和减

cpp 复制代码
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.前后置+±-;

cpp 复制代码
// 前置++
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.两个日期相减

cpp 复制代码
// 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.重载输入输出

cpp 复制代码
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),建议重载为全局函数。

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

相关推荐
mozun202012 分钟前
VS BUG(6) LINK : fatal error LNK1158: 无法运行“rc.exe”
c++·bug·vs·链接器·资源文件
潘yi.1 小时前
web技术与nginx网站环境部署
服务器·网络·nginx
安顾里1 小时前
Linux命令-iostat
linux·运维·服务器
whoarethenext1 小时前
初始https附带c/c++源码使用curl库调用
服务器·c++·qt·https·curl
100编程朱老师2 小时前
面试:什么叫Linux多路复用 ?
linux·运维·服务器
群联云防护小杜2 小时前
云服务器主动防御策略与自动化防护(下)
运维·服务器·分布式·安全·自动化·音视频
Jtti2 小时前
Jtti:nginx服务器如何限制访问频率
服务器·网络·nginx
cloues break.2 小时前
C++进阶----多态
开发语言·c++
struggle20252 小时前
LinuxAgent开源程序是一款智能运维助手,通过接入 DeepSeek API 实现对 Linux 终端的自然语言控制,帮助用户更高效地进行系统运维工作
linux·运维·服务器·人工智能·自动化·deepseek
Despacito0o3 小时前
C++核心编程:类与对象全面解析
开发语言·c++