[C++]日期类计算器的模拟实现

news2024/10/7 14:31:41

目录

日期类计算器的模拟实现::

                                                1.获取某年某月的天数

                                                2.构造函数

                                                3.拷贝构造函数

                                                4.赋值运算符重载

                                                5.析构函数

                                                6.日期+=天数

                                                7.日期+天数

                                                8.日期-天数

                                                9.日期-=天数

                                               10.前置++的运算符重载

                                               11.后置++的运算符重载

                                               12.前置--的运算符重载

                                               13.后置--的运算符重载

                                               14.>的运算符重载

                                               15.<的运算符重载

                                               16.==的运算符重载

                                               17.>=的运算符重载

                                               18.<=的运算符重载

                                               19.!=的运算符重载

                                               20.<<的运算符重载

                                               21.>>的运算符重载

                                               22.日期-日期


日期类计算器的模拟实现::

1.获取某年某月的天数

int GetMonthDay(int year, int month)
{
	static int monthDayArray[13] = { 0,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];
	}
}

2.构造函数

Date(int year = 1, int month = 1, int day = 1)
{
		_year = year;
		_month = month;
		_day = day;
		//检查日期是否合法
		if (!((year >= 1)
			&& (month >= 1 && month <= 12)
			&& (day >= 1 && day <= GetMonthDay(year, month))))
		{
			cout << "非法日期" << endl;
		}
}

3.拷贝构造函数

// 拷贝构造函数 形参加const 防止写反了 问题就可以检查出来了
Date(const Date& d)
{
	_year = d._year;
	_month = d._month;
	_day = d._day;
}

4.赋值运算符重载

//d1 = d2 
//注:1.要注意两个参数的顺序 2.这里面参数不加引用不会导致无穷递归 但为了避免拷贝构造最好加引用
Date& operator=(const Date& d)
{
	//为了支持链式赋值 if是为了避免自己给自己赋值 d1 = d1
	if (this != &d)
	{
		_year = d._year;
		_month = d._month;
		_day = d._day;
	}
	return *this;
}

5.析构函数

~Date()//可不写
{
    ;
}

日期类因为没有申请资源,所以无需写析构函数,编译器默认生成的析构函数就可以。

6.日期+=天数

//d1 += 100
//天满了进月 月满了进年 
Date& operator+=(int day)
{
	//避免 d1 += -1000的情形
	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;
}

7.日期+天数

//d1 + 100
Date operator+(int day) const
{
	Date ret(*this);
	ret += day;//ret.operator+=(day)
	return ret;
}

8.日期-天数

//d1 - 100
Date operator-(int day) const
{
	Date ret(*this);
	ret -= day;
	return ret;
}

9.日期-=天数

//d1 -= 100
Date& operator-=(int day)
{
	//避免 d1 -= -1000
	if (day < 0)
	{
		return *this += -day;
	}
	_day -= day;
	while (_day <= 0)
	{
		--_month;
		if (_month == 0)
		{
			--_year;
			_month = 12;
		}
		_day += GetMonthDay(_year, _month);
	}
	return *this;
}

10.前置++的运算符重载

//前置++
Date& operator++()
{
	//会调用 operator+=(int day)
	*this += 1;
	return *this;
}

11.后置++的运算符重载

//后置++ —多一个int参数主要是为了和前置++进行区分 构成函数重载
Date operator++(int)
{
	Date tmp(*this);
	*this += 1;
	return tmp;
}

12.前置--的运算符重载

//前置--
Date& operator--()
{
	//复用运算符重载-=
	*this -= 1;
	return *this;
}

13.后置--的运算符重载

//后置--
Date operator--(int)
{
	Date tmp = *this;
	*this -= 1;
	return tmp;
}

14.>的运算符重载

//d1 > d2
bool operator>(const Date& d) const
{
	if (_year > d._year)
	{
		return true;
	}
	else if (_year == d._year && _month > d._month)
	{
		return true;
	}
	else if (_year == d._year && _month == d._month && _day > d._day)
	{
		return true;
	}
	return false;
}

15.<的运算符重载

//d1 < d2
bool operator<(const Date& d) const
{
	return !(*this >= d);
}

16.==的运算符重载

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

17.>=的运算符重载

//d1 >= d2
bool operator>=(const Date& d) const
{
	return *this > d || *this == d;
}

18.<=的运算符重载

//d1 <= d2
bool operator<=(const Date& d) const
{
	return !(*this > d);
}

19.!=的运算符重载

//d1 != d2
bool operator!=(const Date& d) const
{
	return !(*this == d);
}

20.<<的运算符重载

//内联函数和静态成员一样 调用处展开 不进符号表
inline ostream& operator<<(ostream& out, const Date& d)
{
	out << d._year << "年" << d._month << "月" << d._day << "日" << endl;
	return out;
}

21.>>的运算符重载

//cin >> d1 编译器转化成operator(cin,d1) 形参中相比<< 去掉了const
inline istream& operator>>(istream& in, Date& d)
{
	in >> d._year >> d._month >> d._day;
	return in;
}

22.日期-日期

//日期-日期
int operator-(const Date& d) const
{
	Date max = *this;
	Date min = d;
	int flag = 1;
	if (*this < d)
  //总结:凡是内部不改变成员变量 也就是不改变*this数据的 这些成员函数都应该加const
  //if (d > *this)
	{
		max = d;
		min = *this;
		flag = -1;
	}
	int n = 0;
	while (min != max)
	{
		++n;
		//复用++ ++到和d1日期相等 就是相差多少天
		++min;
	}
	return n * flag;
}

Date.h

#pragma once
#include <iostream>
using namespace std;
class Date
{
	//友元声明(类的任意位置)声明友元时可以不用加inline
	friend ostream& operator<<(ostream& out, const Date& d);
	friend istream& operator>>(istream& in, Date& d);
public:
	int GetMonthDay(int year, int month)
	{
		static int monthDayArray[13] = { 0,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];
		}
	}
	Date(int year = 1, int month = 1, int day = 1)
	{
		_year = year;
		_month = month;
		_day = day;
		//检查日期是否合法
		if (!((year >= 1)
			&& (month >= 1 && month <= 12)
			&& (day >= 1 && day <= GetMonthDay(year, month))))
		{
			cout << "非法日期" << endl;
		}
	}
	// 拷贝构造函数 形参加const 防止写反了 问题就可以检查出来了
	Date(const Date& d)
	{
		_year = d._year;
		_month = d._month;
		_day = d._day;
	}
	//d1 == d2
	bool operator==(const Date& d) const;
	//d1 > d2
	bool operator>(const Date& d) const;
	//d1 >= d2
	bool operator>=(const Date& d) const;
	//d1 <= d2
	bool operator<=(const Date& d) const;
	//d1 < d2
	bool operator<(const Date& d) const;
	//d1 != d2
	bool operator!=(const Date& d) const;
	//d1 += 100
	Date& operator+=(int day);
	//d1 + 100
	Date operator+(int day) const;
	//d1 = d2 注:1.要注意两个参数的顺序 2.这里面参数不加引用不会导致无穷递归 但为了避免拷贝构造最好加引用
	Date& operator=(const Date& d)
	{
		//为了支持链式赋值 if是为了避免自己给自己赋值 d1 = d1
		if (this != &d)
		{
			_year = d._year;
			_month = d._month;
			_day = d._day;
		}
		return *this;
	}
	//d1 -= 100
	Date& operator-=(int day);
	//d1 - 100
	Date operator-(int day) const;
	//++的操作数只有一个 不传参
	//前置++
	Date& operator++();
	//编译器为了区分前置++和后置++ 规定在后置的函数上加了一个参数
	//后置++
	Date operator++(int);
	//允许成员函数加const 此时this指针的类型为:const Date* const this
	void Print() const
	{
		cout << _year << "/" << _month << "/" << _day << endl;
	}
	//前置--
	Date& operator--();
	//后置--
	Date operator--(int);
	//日期-日期
	int operator-(const Date& d) const;
	//流插入
	//d1 << cout编译器会转化成d1.operator<<(cout) this指针抢了左操作数d1的位置
	//<<和>>的重载一般不写成成员函数 因为this默认抢了第一个参数的位置 Date类对象就是左操作数 不符合使用习惯和可读性
	/*void operator<<(ostream& out)
	{
		out << _year << "年" << _month << "月" << _day << "日" << endl;
	}*/
	//取地址重载
	Date* operator&()
	{
		return this;
	}
	//const成员取地址重载
	const Date* operator&() const
	{
		return this;
	}
	//取地址重载和const成员取地址重载不实现 编译器会默认生成
private:
	int _year;
	int _month;
	int _day;
};
//结论:对于自定义类型,尽量用前置,减少拷贝,提高效率
//全局函数调用:cout << d1转化成operator<<(cout,d1)
//全局函数的定义和全局变量不能放在.h文件中 因为函数的定义在Date.cpp和test.cpp都会展开 函数地址进入符号表 链接器链接两个.cpp文件时相同的函数地址会报错
//解决方法:1.改成静态 2.声明和定义分离
//static修饰函数只在当前文件可见 不会进入符号表
//static void operator<<(ostream& out,const Date& d)
//{
//	out << d._year << "年" << d._month << "月" << d._day << "日" << endl;
//}
//ostream& operator<<(ostream& out, const Date& d);
//内联函数和静态成员一样 调用处展开 不进符号表
inline ostream& operator<<(ostream& out, const Date& d)
{
	out << d._year << "年" << d._month << "月" << d._day << "日" << endl;
	return out;
}
//cin >> d1 编译器转化成operator(cin,d1) 形参中相比<< 去掉了const
inline istream& operator>>(istream& in, Date& d)
{
	in >> d._year >> d._month >> d._day;
	return in;
}

Date.cpp

#include"Date.h"
//d1 == d2
bool Date::operator==(const Date& d) const
{ 	return _year == d._year
		&& _month == d._month
		&& _day == d._day;
}
//d1 > d2
bool Date::operator>(const Date& d) const
{
	if (_year > d._year)
	{
		return true;
	}
	else if (_year == d._year && _month > d._month)
	{
		return true;
	}
	else if (_year == d._year && _month == d._month && _day > d._day)
	{
		return true;
	}
	return false;
}
//d1 >= d2
bool Date::operator>=(const Date& d) const
{
	return *this > d || *this == d;
}
//d1 <= d2
bool Date::operator<=(const Date& d) const
{
	return !(*this > d);
}
//d1 < d2
bool Date::operator<(const Date& d) const
{
	return !(*this >= d);
}
//d1 != d2
bool Date::operator!=(const Date& d) const
{
	return !(*this == d);
}
//d1 += 100
//天满了进月 月满了进年 
Date& Date::operator+=(int day)
{
	//避免 d1 += -1000的情形
	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;
}
//d1 + 100
Date Date::operator+(int day) const
{
	Date ret(*this);
	ret += day;//ret.operator+=(day)
	return ret;
}
//d1 -= 100
Date& Date::operator-=(int day)
{
	//避免 d1 -= -1000
	if (day < 0)
	{
		return *this += -day;
	}
	_day -= day;
	while (_day <= 0)
	{
		--_month;
		if (_month == 0)
		{
			--_year;
			_month = 12;
		}
		_day += GetMonthDay(_year, _month);
	}
	return *this;
}
//d1 - 100
Date Date::operator-(int day) const
{
	Date ret(*this);
	ret -= day;
	return ret;
}
//前置++
Date& Date::operator++()
{
	//会调用 operator+=(int day)
	*this += 1;
	return *this;
}
//后置++ —多一个int参数主要是为了和前置++进行区分 构成函数重载
Date Date::operator++(int)
{
	Date tmp(*this);
	*this += 1;
	return tmp;
}
//前置--
Date& Date::operator--()
{
	//复用运算符重载-=
	*this -= 1;
	return *this;
}
//后置--
Date Date::operator--(int)
{
	Date tmp = *this;
	*this -= 1;
	return tmp;
}
//日期-日期
int Date::operator-(const Date& d) const
{
	Date max = *this;
	Date min = d;
	int flag = 1;
	if (*this < d)
  //总结:凡是内部不改变成员变量 也就是不改变*this数据的 这些成员函数都应该加const
  //if (d > *this)
	{
		max = d;
		min = *this;
		flag = -1;
	}
	int n = 0;
	while (min != max)
	{
		++n;
		//复用++ ++到和d1日期相等 就是相差多少天
		++min;
	}
	return n * flag;
}
//为了支持链式流插入 cout<< d1 <<d2 返回cout类对象
//ostream& operator<<(ostream& out,const Date& d)
//{
//	out << d._year << "年" << d._month << "月" << d._day << "日" << endl;
//	return out;
//}

最后和大家分享一段最近在emo中始终鼓舞自己的一段话,希望大家能在艰苦漫长的学习岁月中,不忘初心找寻真正的自己,把所有的美好装进独属于自己的记忆里,调整心态,继续砥砺前行!

     无人问津也好,技不如人也罢,你都要试着安静下来,去做自己该做的事情,而不是让烦恼和焦虑毁掉你本就不多的热情和定力。心可以碎,手却不能停,该干什么干什么,在崩溃中继续努力前行。

                                                                                                                                        ——余华

出自 "includeevey" 中的一段话

彼此分享给大家的目的一样,希望所有正在学技术被技术所困的友友们,在艰难的时候看到这句话能披荆斩棘,勇往直前!

我的博客即将同步至腾讯云开发者社区,邀请大家一同入驻:https://cloud.tencent.com/developer/support-plan?invite_code=3ngxw7pcde80c

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/415949.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

前后端交互系列之Axios详解(包括拦截器)

目录前言一&#xff0c;服务器的搭建二&#xff0c;Axios的基本使用2.1 Axios的介绍及页面配置2.2 如何安装2.3 Axios的前台代码2.4 Axios的基本使用2.5 axios请求响应结果的结构2.6 带参数的axios请求2.7 axios修改默认配置三&#xff0c;axios拦截器3.1 什么是拦截器3.2 拦截…

Go分布式爬虫笔记(二十)

文章目录20 调度引擎调度引擎目标通道函数选项模式函数式选项模式的好处通道底层原理无缓冲区的通道带缓冲区的通道Select 机制的底层原理思考题在我们的课程中&#xff0c;schedule 函数其实有一个 bug&#xff0c;您能看出来吗&#xff1f;你觉得可以用什么方式找出这样的 Bu…

OTA A/B 分区升级 update_engine简介

近期猛然发现公司的项目都已经换成了AB升级&#xff0c;AB升级之前一直有所了解&#xff0c;只是一直都没有去仔细查看过其具体升级流程&#xff0c;这两天抽空捋了捋&#xff0c;简单整理下。 AB升级&#xff08;谷歌官网叫法无缝更新&#xff09;是自android7.0开始新增的一…

头歌(Linux之进程管理一):第2关:进程创建操作-fork

任务描述 在上一关我们学习如何获取进程的pid信息&#xff0c;本关我们将介绍如何编程创建一个新的进程。 本关任务&#xff1a;学会使用C语言在Linux系统中使用fork系统调用创建一个新的进程。 相关知识 在Linux系统中创建进程有很多函数可以使用&#xff0c;其中包括了系…

初识Elasticsearch

文章目录介绍一、什么是elasticsearch&#xff1f;二、基本概念三、安装elasticsearch与kibana四、安装kibana&#xff08;跟ES要在同一个网络中&#xff09;五、IK分词器总结介绍 好处&#xff1a;可以帮助从海量数据中查找需要的内容&#xff1b; 一、什么是elasticsearch&…

ETL工具-pentaho企业实战部署

&#x1f4e2;&#x1f4e2;&#x1f4e2;&#x1f4e3;&#x1f4e3;&#x1f4e3; 哈喽&#xff01;大家好&#xff0c;我是【IT邦德】&#xff0c;江湖人称jeames007&#xff0c;10余年DBA及大数据工作经验 一位上进心十足的【大数据领域博主】&#xff01;&#x1f61c;&am…

C++二叉搜索树与KV模型

二叉搜索树与KV模型二叉搜索树概念与操作性能分析实现KV模型二叉搜索树 本章是为了C的map和set做铺垫 概念与操作 二叉搜索树又称二叉排序树&#xff0c;它或者是一棵空树&#xff0c;或者是具有以下性质的二叉树: 若它的左子树不为空&#xff0c;则左子树上所有节点的值都小…

面试题之vue的响应式

文章目录前言一、响应式是什么&#xff1f;二、Object.defineProperty二、简单模拟vue三、深度监听四、监听数组总结前言 为了应对面试而进行的学习记录&#xff0c;可能不够有深度甚至有错误&#xff0c;还请各位谅解&#xff0c;并不吝赐教&#xff0c;共同进步。 一、响应式…

如何做好 IT 项目管理?做好项目管理常用的9大项目管理平台、7大管理方法

一个好的管理&#xff0c;是70%在流程、规范、工具&#xff0c;剩下的30%自由发挥。一个不好的管理&#xff0c;只有地板&#xff0c;每个人都要自己想办法&#xff0c;够到天花板。一个好的工具&#xff0c;就是帮助团队够到天花板的台阶。——刘润 项目管理是一门复杂的艺术&…

统一的文件管理,团队轻松协作

目前IT行业大都采用项目经理制的管理方式&#xff0c;这种管理方式下各个部门间相互独立&#xff0c;同时各部门间也缺乏沟通协作。因此IT行业在文件管理上主要面临以下几个问题&#xff1a; 文档缺乏集中管理&#xff1a;企业在管理过程中产生的大量文件分散在各个部门中&…

Python升级 pip : python -m pip install --upgrade pip,socket.timeout加入超时处理方法

人生苦短&#xff0c;我用python 最近又遇到了一个小的报错问题&#xff0c; 趁现在我还没有忘记&#xff0c; 赶紧来写一写… python 安装包资料报错交流:点击此处跳转文末名片获取 WARNING: You are using pip version 19.3.1; however, version 20.0.2 is available. You…

系统学习Numpy(一)——numpy的安装与基础入门[向量、矩阵]

系列文章目录 numpy的安装与基础入门[向量、矩阵与维度] numpy的安装与基础入门[向量、矩阵与维度]系列文章目录前言numpy安装向量与矩阵生成向量生成矩阵向量类型前言 numpy是科学计算以及机器学习深度学习的基础必备工具&#xff0c;本文将介绍numpy的安装&#xff0c;以及…

C语言课设项目-51单片机-中断系统

(创作不易&#xff0c;感谢有你&#xff0c;你的支持&#xff0c;就是我前行的最大动力&#xff0c;如果看完对你有帮助&#xff0c;请留下您的足迹&#xff09; 目录 51单片机的中断系统 一、中断的概念 二、51单片机的中断系统结构 三、中断允许控制 四、中断…

C#,初学琼林(06)——组合数的算法、数据溢出问题的解决方法及相关C#源代码

1 排列permutation 排列&#xff0c;一般地&#xff0c;从n个不同元素中取出m&#xff08;m≤n&#xff09;个元素&#xff0c;按照一定的顺序排成一列&#xff0c;叫做从n个元素中取出m个元素的一个排列(permutation)。特别地&#xff0c;当mn时&#xff0c;这个排列被称作全…

vs code c语言断点调试window版解决方案

序&#xff1a; 1、这一步不懂劝退多少人&#xff0c;博主搜到了多少博文都是mac的&#xff0c;结果发现都对不上&#xff01; 先看最终效果演示 接下去我每个步骤&#xff0c;你都仔细看&#xff0c;漏看一个环境都对不上&#xff01; 正文 1、先去看博主的c/c运行环境配置图…

10-vue3动画

文章目录1.vue的transition动画1.1transition的基本使用1.2transition组件的原理1.3过渡动画的class1.4class的命名规则和添加时机1.5显示的指定过渡时间1.6过渡的模式mode1.7动态组件的切换1.8.appear初次渲染2、animation动画2.1同时设置animation和transition3.结合第三方库…

【Bard】来自谷歌的“吟游诗人”

个人主页&#xff1a;【&#x1f60a;个人主页】 文章目录前言Bard与相关产品的对比Bard VS 弱智吧来自对手的评论ChatGPT文心一言总结&#xff1a;前言 相比较ChatGPT的话题不断&#xff0c;谷歌的“Bard”显然低调了许多&#xff0c;在“画大饼”失败一个多月后&#xff0c…

【Python开发手册】深入剖析Google Python开发规范:规范Python注释写作

&#x1f496; 作者简介&#xff1a;大家好&#xff0c;我是Zeeland&#xff0c;全栈领域优质创作者。&#x1f4dd; CSDN主页&#xff1a;Zeeland&#x1f525;&#x1f4e3; 我的博客&#xff1a;Zeeland&#x1f4da; Github主页: Undertone0809 (Zeeland) (github.com)&…

高通开发系列 - linux kernel内核升级msm-4.9升级至msm-4.19(2)

By: fulinux E-mail: fulinux@sina.com Blog: https://blog.csdn.net/fulinus 喜欢的盆友欢迎点赞和订阅! 你的喜欢就是我写作的动力! 目录 MDSS PLL驱动问题msm-4.19内核适配nand flashMDSS PLL驱动问题 | /home/peeta/sc262R_private_rl/build-msm8909/tmp/work-shared/ms…

第01章_Java语言概述

第01章_Java语言概述 讲师&#xff1a;尚硅谷-宋红康&#xff08;江湖人称&#xff1a;康师傅&#xff09; 官网&#xff1a;http://www.atguigu.com 1. Java知识脉络图 1.1 Java基础全程脉络图 1.2 本章专题与脉络 2. 抽丝剥茧话Java 2.1 当前大学生就业形势 麦可思研究院…