【C++】日期类的实现

在实现日期类时候,需要考虑到许多细节性问题:
(1)构造日期时候初始化值的合法性;
(2)日期加减之后内容的合法性;
(3)日期自增自减返回值类型可以采用 &,以及合法性检验;
(4)代码的复用性-------例如判断两个日期不等:可以常规方法判断不等;或是采用判等方法取反

class Date
{
public:
	//判断闰年
	static bool IsLeapYear(int year)
	{   //闰年:能被4整除不能被100整除,或能被 400 整除
		if (((0 == year % 4) && (0 != year % 100)) || (0 == year % 400))
			return true;
		return false;
	}

	// 获取某年某月的天数
	static int GetMonthDay(int year, int month)
	{
		int days[] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
		if (2 == month && IsLeapYear(year)) {     //闰年二月有29天
			days[month] += 1;
		}
		return days[month];
	}

	// 全缺省的构造函数
	Date(int year = 1900, int month = 1, int day = 1)
		:_year(year), _month(month), _day(day)
	{
		//判断初始化的合理性
		if (((_month < 0) || (_month > 12)) || ((_day < 0) || (_day > GetMonthDay(_year, _month))))
		{
			cout << "输入的日期不合法!初始化为默认的日期值:1900/1/1 !" << endl;
			_year = 1900;
			_month = 1;
			_day = 1;
		}
		cout << "Date(int,int,int)" << endl;
	}

	// 拷贝构造函数  // d2(d1)
	Date(const Date& d)
		:_year(d._year), _month(d._month), _day(d._day)
	{
		cout << "Date(const)" << this << endl;
	}


	// 赋值运算符重载
  // d2 = d3 -> d2.operator=(&d2, d3)
	Date& operator=(const Date& d)      //返回引用类型
	{
		_year = d._year;
		_month = d._month;
		_day = d._day;

		return *this;
	}

	// 析构函数
	~Date()
	{
		cout << "~Date()" << this << endl;
	}


	// 日期+=天数
	Date& operator+=(int day)
	{
		//正常情况下
		_day += day;
		//判断合法性
		while (_day > GetMonthDay(_year, _month)) {
			_day -= GetMonthDay(_year, _month);   
			//当前天数超出本月应有的天数,则月数应该增加,使得天数在合法值以内
			_month += 1;
			if (_month == 13)
			{
				_year += 1;
				_month = 1;
			}
		}
		return *this;
	}



	// 日期+天数
	Date operator+(int day)const
	{
		//加一个负数==减一个正数
		if (day < 0) {
			return *this - (-day);
		}

		//正常情况下
		Date tmp(*this);  //对 tmp 的修改
		tmp._day += day;
		int GetOfDays = 0;
		while (tmp._day > (GetOfDays = tmp.GetMonthDay(tmp._year, tmp._month))) {
			tmp._day -= GetOfDays;
			tmp._month++;
			if (tmp._month == 13) {
				tmp._year += 1;
				tmp._month = 1;
			}
		}
		return tmp;
	}


	// 日期-天数
	Date operator-(int day)const
	{
		//判断 day 的正负,减去一个负数==加一个正数
		if (day < 0) {
			return *this + (-day);
		}

		//正常情况下做差
		Date tmp(*this);
		tmp._day -= day;
		int GetOfDays = 0;
		while (tmp._day < 0) {
			tmp._month--;
			if (tmp._month == 0) {
				tmp._year -= 1;
				tmp._month = 12;
			}
			GetOfDays = GetMonthDay(tmp._year, tmp._month);
			tmp._day += GetOfDays;
		}
		return tmp;
	}



	// 日期-=天数
	Date& operator-=(int day)
	{
		_day -= day;
		int GetOfDays = 0;
		while (_day < 0) {
			_month -= 1;
			if (_month == 0) {
				_year -= 1;
				_month = 12;
			}
			GetOfDays = GetMonthDay(_year, _month);
			_day += GetOfDays;
		}
		return *this;
	}

	// 日期-日期 返回天数 (*******)
	int operator-(const Date& d)const
	{
		//采用小日期相加加到大日期来计算差值
		Date minDay(*this);
		Date maxDay(d);
		if (minDay > maxDay) {
			maxDay = *this;
			minDay = d;
		}

		//小日期自增
		int count = 0;
		while (minDay != maxDay) {
			count++;
			++minDay;
		}

		return count; //返回差值
	}



	// 前置++
	Date& operator++()
	{
		++a : a自增并返回自增之后的值
		//_day += 1;
		//while (_day > GetMonthDay(_year, _month)) {
		//	_day -= GetMonthDay(_year, _month);
		//	_month += 1;
		//	if (_month == 13) {
		//		_year += 1;
		//		_month = 1;
		//	}
		//}

		*this = *this + 1;
		return *this;
	}


	// 后置++
	Date operator++(int)
	{
		//a++:先使用 a 的值,后自增
		Date tmp(*this);
		/*_day += 1;
		while (_day > GetMonthDay(_year, _month)) {
			_day -= GetMonthDay(_year, _month);
			_month += 1;
			if (_month == 13) {
				_year += 1;
				_month = 1;
			}
		}*/
		*this = *this + 1;
		return tmp;
	}

	// 前置--
	Date& operator--()
	{
		--a
		//_day -= 1;
		//while (_day == 0) {
		//	_month -= 1;
		//	if (_month == 0) {
		//		_year -= 1;
		//		_month = 12;
		//	}
		//	_day = GetMonthDay(_year, _month);
		//}

		*this = *this - 1;
		return *this;
	}


	// 后置--
	Date operator--(int)
	{
		Date tmp(*this);
		/*_day -= 1;
		while (_day == 0) {
			_month -= 1;
			if (_month == 0) {
				_year -= 1;
				_month = 12;
			}
			_day = GetMonthDay(_year, _month);
		}*/
		*this = *this - 1;
		return tmp;
	}


	// >运算符重载
	bool operator>(const Date& d)const
	{
		if (_year == d._year) {
			if (_month == d._month)
				return _day > d._day;
			else
				return _month > d._month;
		}
		else
			return _year > d._year;
	}


	// ==运算符重载
	bool operator==(const Date& d)const
	{
		return (_year == d._year && _month == d._month && _day == d._day);
	}

	// >=运算符重载
	bool operator >= (const Date& d)const
	{
		if (_year == d._year) {
			if (_month == d._month)
				return _day >= d._day;
			else
				return _month >= d._month;
		}
		else
			return _year >= d._year;
	}

	// <运算符重载
	bool operator < (const Date& d)const
	{
		/*if (_year == d._year) {
			if (_month == d._month)
				return _day < d._day;
			else
				return _month < d._month;
		}
		else
			return _year < d._year;*/


		//return !((*this == d) || (*this > d));
		return !(*this >= d);
	}
	// <=运算符重载
	bool operator <= (const Date& d)const
	{
		if (_year == d._year) {
			if (_month == d._month)
				return _day <= d._day;
			else
				return _month <= d._month;
		}
		else
			return _year <= d._year;
	}

	// !=运算符重载
	bool operator != (const Date& d)const
	{
		//return !(*this == d);
		return !((_year == d._year) && (_month == d._month) && (_day == d._day));
	}
private:
	int _year;
	int _month;
	int _day;
};

测试用例:

void Test()
{
#if 0
	//日期-日期的差值
	Date d1(2022, 11, 22);
	Date d2(2022, 5, 1);
	cout << d1 - d2 << endl;     //205
#endif

#if 0
	//日期 +=\-=\+\-
	Date d1(2022, 11, 29);
	d1 += 3;        //2022/12/2

	d1 -= 5;         //2022/11/27---------d1

	Date d2;
	//d2 = d1 - 10;       //2022/11/17-----d2
	d2 = d1 + (-10);

	//d2 = d1 + 200;      //2023/6/15
	d2 = d1 - (-200);

	//d2 = d1 - 100;      //2022/8/19
	d2 = d1 + (-100);

#endif

#if 0
	//前置++、-- 后置++、--测试
	Date d1(2022, 11, 30);
	Date d2;
	d2 = d1++;   //后置++,d2 保存 d1 修改之前的值

	d2 = d1--;  //后置--

	d2 = ++d1;  //前置++, d2 保存修改之后的 d1
	d2 = --d1;  //前置--

#endif

#if 0
	//初始化检测
	/*Date d1(2022, 13, 1);
	Date d2(2022, 1, 100);*/


	//Date d1(2022, 11, 29);
	//Date d2(d1);            //拷贝构造

	//Date d3(2022, 11, 30);
	//if (d3 > d1)
	//	cout << "d3>d1" << endl;

	//Date d4(2012, 1, 1);
	//if (d4 > d1)
	//	cout << "d4>=d1" << endl;
	//else
	//	cout << "d4<d1" << endl;
	//
	//d4 = d1;              //赋值运算符  
	//if (d4 == d1)
	//	cout << "d4==d1" << endl;

	//Date d5(2023, 1, 1);
	//if (d5 != d1)
	//	cout << "d5!=d1" << endl;

#endif
}
int main()
{
	Test();
	return 0;
}

欢迎评论留言哦~~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值