【C++】类与对象(日期计算器)

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

   🌈个人主页:秦jh__https://blog.csdn.net/qinjh_?spm=1010.2135.3001.5343
🔥 系列专栏:

【C++】类与对象(日期计算器),C++,c++,开发语言,c语言,算法

目录

头文件

函数实现


前言

    💬 hello! 各位铁子们大家好哇。

             今日更新了类与对象日期计算器的内容
    🎉 欢迎大家关注🔍点赞👍收藏⭐️留言📝

头文件

#include<iostream>
#include<assert.h>
using namespace std;

class Date
{
public:
	bool CheckInvalid() const;
	Date(int year = 1, int month = 1, int day = 1);
	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) const;
	Date& operator-=(int day);

	//++d1
	Date& operator++();

	//为了跟前置++区分,强行增加一个int形参,构成重载区分
	//d1++
	Date operator++(int);

	//日期-日期
	int operator-(const Date& d) const;

	//如果不声明和定义分离,本质就是内联
	int GetMonthDay(int year, int month) const
	{
		assert(month > 0 && month < 13);
		static int monthDays[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;
		}

		return monthDays[month];
	}

	void Print() const
	{
		cout << _year << "/" << _month << "/" << _day << endl;
	}

	//void operator<<(ostream& out)
	//{
	//	out << _year << "年" << _month << "月" << _day << "日" << endl;
	//}

	//友元声明
	friend ostream& operator<<(ostream& out, const Date& d);
	friend istream& operator>>(istream& in, Date& d);

private:
	int _year;
	int _month;
	int _day;
};

ostream& operator<<(ostream& out, const Date& d);
istream& operator>>(istream& in,  Date& d);

声明流插入<<和流提取>>时,应在类外面声明,不然this指针会占据第一个形参,Date就只能是左操作数了,打印时就会变成d1<<cout; ,不符合常规写法在类外面声明时,为了不把private打开,可以进行友元声明,就可以在不打开private的情况下,访问private。

函数实现

#include"Date.h"

Date::Date(int year , int month, int day )
{
	_year = year;
	_month = month;
	_day = day;

	if (!CheckInvalid())
	{
		cout << "构造日期非法" << endl;
	}
}

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)
		{
			if (_day < d._day)
			{
				return true;
			}
		}
	}
	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 _year == d._year
		&& _month == d._month
		&& _day == d._day;
}
bool Date::operator!=(const Date& d) const
{
	return !(*this == d);
}

Date& Date::operator+=(int day)
{
	_day += day;
	while (_day > GetMonthDay(_year, _month))
	{
		_day -= GetMonthDay(_year, _month);
		++_month;
		if (_month == 13)
		{
			++_year;
			_month = 1;
		}
	}
	return *this;//可以用引用返回,出了作用域还在
}

Date Date::operator+(int day) const
{
	//Date tmp(*this);
	Date tmp = *this;  //拷贝构造
	tmp += day;

	return tmp;
}



//Date Date::operator+(int day)
//{
//	//Date tmp(*this);
//	Date tmp = *this;  //拷贝构造
//
//	tmp._day += day;
//	while (tmp._day > GetMonthDay(tmp._year, tmp._month)) 
//	{
//		tmp._day -= GetMonthDay(tmp._year, tmp._month); 
//		++tmp._month; 
//		if (tmp._month == 13) 
//		{ 
//			++tmp._year; 
//			tmp._month = 1; 
//		}
//	}
//	return tmp;  //不能用引用返回,因为他是局部对象,出了作用域就不在了
//}
//
//
//Date& Date::operator+=(int day)
//{
//	*this = *this + day;
//	return *this;
//}

Date Date::operator-(int day) const
{
	Date tmp = *this;
	tmp -= day;
	return tmp;
}

Date& Date::operator-=(int day)
{
	_day -= day;
	while(_day < 0)
	{
		--_month;
		if (_month == 0)
		{
			--_year;
			_month = 12;
		}
		_day += GetMonthDay(_year, _month);
	}
	return *this;
}

//++d  ->d.operator++()  编译器自动转换
Date& Date::operator++()
{
	*this += 1;
	return *this;
}

//d++  ->d.operator(0)   C++规定:后置++重载时多增加一个int类型的参数,但调用函数时该参数不用传递,编译器自动传递
Date Date::operator++(int)  //规定只能是int,形参名字可以不写
{
	Date tmp = *this;
	*this += 1;
	return tmp;
}

int Date::operator-(const Date& d) const
{
	int flag = 1;
	Date max = *this;
	Date min = d;

	if (*this < d)
	{
		int flag = -1;
		max = d;
		min = *this;
	}

	int n = 0;
	while (min != max)
	{
		++min;
		++n;
	}
	return n * flag;
}

bool Date::CheckInvalid() const
{
	if (_year <= 0
		|| _month < 1 || _month>12
		|| _day<1 || _day>GetMonthDay(_year, _month))
	{
		return false;
	}
	else
	{
		return true;
	}
}

ostream& operator<<(ostream& out, const Date& d)
{
	out << d._year << "年" << d._month << "月" << d._day << "日" << endl;
	return out;
}

istream& operator>>(istream& in, Date& d)
{
	while (1)
	{
		cout << "请输入年月日:" << endl;
		in >> d._year >> d._month >> d._day;

		if (!d.CheckInvalid())
		{
			cout << "输入了无效日期,请重新输入" << endl;
		}
		else
		{
			break;
		}	
	}
	return in;
}

上方实现时,有日期+天数和日期+=天数。二者实现其中一个即可复用另一个。但是要先实现哪一个更好呢?

【C++】类与对象(日期计算器),C++,c++,开发语言,c语言,算法文章来源地址https://www.toymoban.com/news/detail-838208.html

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

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

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

相关文章

  • 【C++】如何用C++写一个日期计算器

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

    2024年04月23日
    浏览(42)
  • [C++]日期类计算器的模拟实现

    目录 日期类计算器的模拟实现::                                                 1.获取某年某月的天数                                                 2.构造函数                                                 3.拷贝构造函数                      

    2024年02月03日
    浏览(64)
  • 【C++小项目】实现一个日期计算器

    目录 Ⅰ. 引入 Ⅱ. 列轮廓 Ⅲ. 功能的实现 构造函数 Print 判断是否相等 == | != ➡️==: ➡️!=: 判断大小 | = | | = ➡️: ➡️=: ➡️=: ➡️: 加减天数 + | += | - | -= ➡️+=: ➡️+: ➡️-: ➡️-=: 自增/自减 ++ | -- ➡️前置++ ➡️后置++ ➡️前置-- ➡️后置-- 日期减日期 ➡

    2024年02月11日
    浏览(33)
  • C++奇迹之旅:从0开始实现日期时间计算器

    通过前面学完了 C++ 的默认成员函数,实践出真知,本小节我们将一起来实现一个简单上手的日期时间计算器,阿森和你一起一步一步的操作实现! 完整代码在文章末尾哦 为了代码的维护性和可观型,我们在设置三个文件头文件 Date.h ,源文件 Date.cpp , Test.cpp 我们先把头文

    2024年04月28日
    浏览(24)
  • Android开发:kotlin语言实现简易计算器

    输入两个数字,可选加减乘除操作符,并计算显示对应结果 随系统切换语言 可对结果进行四舍五入操作 界面布局:activity_main.xml文件代码 字符定义:string.xml文件代码 逻辑实现:MainActivity.kt 文件代码 方法一(偷懒): 复制文件到对应位置 方法二: 1. 绘制界面 2. 编写逻辑

    2023年04月08日
    浏览(30)
  • 模拟计算器编程教程,中文编程开发语言工具编程实例

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

    2024年02月08日
    浏览(49)
  • 在线推算两个日期相差天数的计算器

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

    2024年02月14日
    浏览(37)
  • C++类和对象-多态->案例1计算器类、案例2制作饮品、案例3电脑组装需求分析和电脑组装具体实现

    #includeiostream using namespace std; #includestring //分别利用普通写法和多态技术实现计算器 //普通写法 class Calculator { public:     int getResult(string oper)     {         if (oper == \\\"+\\\") {             return m_Num1 + m_Num2;         }         else if (oper == \\\"-\\\") {             return m_Num

    2024年02月20日
    浏览(33)
  • MFC基于对话框——仿照Windows计算器制作C++简易计算器

    目录 一、界面设计 二、设置成员变量 三、初始化成员变量  四、初始化对话框 ​五、添加控件代码 1.各个数字的代码(0~9) 2.清除功能的代码 3.退格功能的代码 4.加减乘除功能的代码 5.小数点功能的代码 6.正负号功能的代码 7.等于功能的代码 六、源码领取方式 制作好之后

    2024年02月05日
    浏览(40)
  • C++简易计算器的实现

    定义: 计算器是近代人发明的可以进行数字运算的机器。 也就是说,计算器不等同于算盘,前者能自行运算,后者只能简便计算过程,在古代,人们发明了许多计算工具,如算筹、算盘、计算尺等,随着社会的发展和科技的进步,计算工具也经历了由简单到复杂,由低级向高级的发

    2024年02月11日
    浏览(32)

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

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

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

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

二维码1

领取红包

二维码2

领红包