我与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),建议重载为全局函数。

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

相关推荐
x66ccff20 分钟前
【linux】4张卡,坏了1张,怎么办?
linux·运维·服务器
憧憬成为原神糕手24 分钟前
c++_list
开发语言·c++
zyh2005043025 分钟前
c++的decltype关键字
c++·decltype
2401_8628867843 分钟前
蓝禾,汤臣倍健,三七互娱,得物,顺丰,快手,游卡,oppo,康冠科技,途游游戏,埃科光电25秋招内推
前端·c++·python·算法·游戏
隔窗听雨眠1 小时前
基于Prometheus和Grafana的现代服务器监控体系构建
服务器
徐霞客3201 小时前
对于C++继承中子类与父类对象同时定义其析构顺序的探究
开发语言·c++
拾光师1 小时前
linux之网络命令
linux·服务器·网络
敲上瘾1 小时前
多态的使用和原理(c++详解)
开发语言·数据结构·c++·单片机·aigc·多态·模拟
毕设木哥2 小时前
25届计算机专业毕设选题推荐-基于python+Django协调过滤的新闻推荐系统
大数据·服务器·数据库·python·django·毕业设计·课程设计
心怀花木2 小时前
【C++】list容器的基本使用
c++·stl·list