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

这篇具有很好参考价值的文章主要介绍了[C++]日期类计算器的模拟实现。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

目录

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

                                                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];
	}
}

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

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;
		}
}

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

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

3.拷贝构造函数

Date(const Date& d)
{
	_year = d._year;
	_month = d._month;
	_day = d._day;
}

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

4.赋值运算符重载

Date& operator=(const Date& d)
{
	if (this != &d)
	{
		_year = d._year;
		_month = d._month;
		_day = d._day;
	}
	return *this;
}

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

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;
}

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

7.日期+天数

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

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

8.日期-天数

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

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

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;
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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;
}

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

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

15.<的运算符重载

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

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

16.==的运算符重载

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

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

17.>=的运算符重载

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

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

18.<=的运算符重载

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

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

19.!=的运算符重载

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

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

20.<<的运算符重载

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

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

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

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

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

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

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

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

21.>>的运算符重载

inline istream& operator>>(istream& in, Date& d)
{
	in >> d._year >> d._month >> d._day;
	return in;
}

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

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;
}

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

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;
};
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;
}

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

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

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

                                                                                                                                        ——余华

出自 "includeevey" 中的一段话

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

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

我的博客即将同步至腾讯云开发者社区,邀请大家一同入驻:https://cloud.tencent.com/developer/support-plan?invite_code=3ngxw7pcde80c文章来源地址https://www.toymoban.com/news/detail-438138.html

到了这里,关于[C++]日期类计算器的模拟实现的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 【Java】Java使用Swing实现一个模拟计算器(有源码)

       📝个人主页:哈__ 期待您的关注  今天翻了翻之前写的代码,发现自己之前还写了一个计算器,今天把我之前写的代码分享出来。  我记得那会儿刚学不会写,写的乱七八糟,但拿来当期末作业还是不错的哈哈。 直接上源码,后上讲解。 计算器上的按键不少,我们都定

    2024年04月11日
    浏览(49)
  • C++ 日期计算器

    概要 本篇主要探究C++ 实现日期计算器 Date 构造函数 构造函数,他是在创建类的时候调用进行初始化操作,我们这里声明与定义分离,所以它的参数不需要填写缺省值,缺省值在声明的时候定义即可。 Date 拷贝构造函数 拷贝构造函数和构造函数作用相似,拷贝构造函数是将已

    2024年02月21日
    浏览(40)
  • 【c++】简单的日期计算器

    🔥个人主页 : Quitecoder 🔥 专栏 : c++笔记仓 朋友们大家好啊,在我们学习了默认成员函数后,我们本节内容来完成知识的实践,来实现一个简易的日期计算器 头文件声明所有函数: 第一个函数,获取 某月天数 为了按照月的月份直接访问数组,我们设置大小为13,由于要进

    2024年04月09日
    浏览(68)
  • 【C++】类与对象(日期计算器)

       🌈个人主页: 秦jh__https://blog.csdn.net/qinjh_?spm=1010.2135.3001.5343 🔥 系列专栏: 目录 头文件 函数实现      💬 hello! 各位铁子们大家好哇。              今日更新了类与对象日期计算器的内容      🎉 欢迎大家关注🔍点赞👍收藏⭐️留言📝 声明流插入和流提

    2024年03月10日
    浏览(51)
  • 在线推算两个日期相差天数的计算器

     具体请前往:在线推算两个日期相差天数的计算器

    2024年02月14日
    浏览(44)
  • 【C++】如何用C++写一个日期计算器

    目录 前言 代码的布局 设计数据 方法声明 方法的实现 获取某年某月的天数 *全缺省的构造函数 * 拷贝构造函数 *赋值运算符重载 *析构函数 日期+=天数 日期+天数 日期-天数 日期-=天数 前置++ 后置++ 后置-- 前置-- 实现比较大小运算符重载思路 运算符重载 ==运算符重载 *  = 运算

    2024年04月23日
    浏览(47)
  • 【C++初阶】第三站:类和对象(中) -- 日期计算器

    目录 前言 日期类的声明.h 日期类的实现.cpp 获取某年某月的天数 全缺省的构造函数 拷贝构造函数 打印函数 日期 += 天数 日期 + 天数 日期 -= 天数 日期 - 天数 前置++ 后置 ++ 前置 -- 后置-- 日期类中比较运算符的重载 运算符重载 ==运算符重载 != 运算符重载 =运算符重载 运算符

    2024年02月19日
    浏览(36)
  • 模拟计算器编程教程,中文编程开发语言工具编程实例

    模拟计算器编程教程,中文编程开发语言工具编程实例 中文编程系统化教程,不需英语基础。学习链接 ​​​​​​https://edu.csdn.net/course/detail/39036 课程安排:初级1 1  初级概述 2  熟悉构件取值赋值 3 折叠式菜单滑动面板编程 4 自定义图形窗口自定义标题栏编程 5 多行文本

    2024年02月08日
    浏览(55)
  • 图形界面科学计算器 功能:用户界面模拟真实计算器(具体可参考手机计算器APP),显示0~9按键、+、-、*、/运算符和小数点、=、(),按下对应按键,算式区域(可用Label组件)显示用户输入的内容,

    图形界面科学计算器 功能:用户界面模拟真实计算器(具体可参考手机计算器APP),显示0~9按键、+、-、*、/运算符和小数点、=、(),按下对应按键,算式区域(可用Label组件)显示用户输入的内容,按等号,计算结果并显示。 要求: 1.采用图形用户界面 2.正常输入算式,

    2024年02月03日
    浏览(43)
  • 实现复数计算器

            本论文描述了一个复数计算器的设计和实现,旨在扩展传统计算器的功能,以支持复数的加法、减法、乘法和除法。通过使用Java编程语言和Swing图形用户界面库,我们创建了一个直观、易于使用的界面,允许用户输入复数,并执行基本的算术运算。         计

    2024年02月02日
    浏览(54)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包