【C++】日期运算代码的实现

  👑个人主页:啊Q闻       

🎇收录专栏:《C++》           

 🎉我自会去见我的山

💡感谢阅读,欢迎关注,点赞,收藏,评论💡

前言

实现日期的相加减,这个代码有助于我们进一步理解运算符重载的相关知识,也是对C++基础知识的一个运用。

 代码思路

1.实现日期的哪些运算?

实现日期类的运算,我们要实现那些运算,是要根据实际情况判断哪些运算是有实际意义的,如日期加减天数是有意义,日期减日期可以求两个日期间的间隔天数是有意义的,但是日期相加是没有意义的,所以,我们要写的运算有:

 2.如何实现日期加减天数?

日期减天数类似于上面。

3.利用复用可以大大减少代码量 ,问题在于实现谁复用谁?

减法类似上面。

4.前置加加和后置加加如何区分? 

为了区分,构成重载,给后置++强行增加一个int形参,这里不需要写形参名,这个参数仅仅是为了区分前置和后置。

5.如何实现日期减日期?

需要注意的是:

下面代码中的*this<d中的< , min!=max中的!=,它是在实现日期类的比较,日期类的比较需要我们自己实现。

代码实现 


 Date.h中:

#pragma once
#include<iostream>
#include<assert.h>
using namespace std;
class Date
{
public:
	//初始化
	Date(int year = 1900, int month = 1, int day = 1)
	{
		_year = year;
		_month = month;
		_day = day;
	}
	//打印
	void Print();
	//判断二月是否为闰二月要反复调用,所以我们将其定为内联函数
	int GetMonthDay(int year, int month)const
	{
		assert(month > 0 && month < 13);
		static int MonthArray[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 MonthArray[month];
		}
	}
	//运算的实现
	Date operator+(int day)const;
	Date operator-(int day)const;
	Date& operator+=(int day);
	Date& operator-=(int day);
	Date& operator++();
	Date operator++(int);
	Date& operator--();
	Date operator--(int);
	int 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;
	bool operator!=(const Date& d) const;

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

  .cpp中: 

#include"Date.h"
void Date:: Print()
{
	cout << _year << "-" << _month << "-" << _day;
}
//独立实现加
Date Date::operator+(int day)const
{
	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)
{
	if (day < 0)
	{
		*this += (-day);
		return *this;
	}
	_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;
	tmp +=day;
	return tmp;
}
//复用+
Date& Date::operator+=(int day)
{
	*this += day;
	return *this;
}
 Date Date::operator-(int day)const
{
	 Date tmp = *this;
	 tmp -= day;
	 return tmp;
}
 Date& Date:: operator-=(int day)
{
	 if (day < 0)
	 {
		 *this += (-day);
		 return *this;
	 }
	 _day -= day;
	 while (_day <=0)
	 {
		 _month--;
		 if (_month == 0)
		 {
			 _year--;
			 _month = 12;
		 }
		 _day += GetMonthDay(_year, _month);
	 }
	 return *this;
}
 Date& Date:: operator++()
 {
	 *this += 1;
	 return *this;
}
 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 -= 1;
	 return tmp;
}
 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;
 }

 // d1 <= d2
 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);
 }

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

 希望我的博客能对你有帮助,铁子们可以自己去实现实现。

  • 11
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
(1) 测试日期类成员函数,在主函数中列出菜单选项,可以完成日期加减比较等测试功能。 (2) 完善程序功能,在日期相加的菜单选项中增加日期加天数,结果为新日期日期家月份,结果为新日期,要考虑闰年情况。 (3) 完善程序功能,在日期相减的菜单选项中增加日期减天数,结果为新日期日期减月份,结果为新日期,要考虑闰年情况。 (4) 显示日期时增加显示星期及英文形式的月份的功能。 (5) 增加输入的甄别功能,即输入非法数据(如负数、日期超过31天、时间超过24小时等情况)的识别显示功能。 (1) 仿照日期类编写时间类CTime_t,可以完成时间的设置、运算、比较等功能。 (2) 增加时间的输入功能,既可以选择输入格式,可以输入hh:mm:ss格式的信息。 (3) 增加时间的输出格式,可以输出12小时的时间格式。 (4) 编写时间和日期的派生类CDati,完成日期与时间的联合设置、运算、比较等功能,要求该派生类可以完成:日期时间加天数或时间等于新的日期时间,日期时间减天数或等于新的日期时间,两个日期时间相减等于天数或时间等工作,在程序中考虑闰年等具体情况,并重载各种运算符。 (5) 增加输入的甄别功能,即输入非法数据,即输入非法数据(如负数、日期超过31天、时间超过24小时等情况)的识别显示功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值