C++日期类(运算符的重载)

日期类(运算符的重载)

在C++里可以实现运算符的重载,所以可以实现直接对日期类进行加减、比较等操作。

以下是整个Date.h头文件的源代码:

#pragma once
#include<cassert>
using namespace std;
class Date
{
public:
	Date(int year = 1900, int month = 1, int day = 1)
		:_year(year), _month(month), _day(day)
	{
		assert(IsInvalid());    //判断日期是否合法
	}
	Date(const Date& d)
	{
		_year = d._year;
		_month = d._month;
		_day = d._day;
	}
	~Date()
	{}
	void Display()     //打印日期类
	{
		cout << _year << "-" << _month << "-" << _day << endl;
	}

	bool IsInvalid()   //判断日期是否合法
	{
		if (_year >= 1900 && _month > 0 && _month < 13 && _day>0 && _day <= GetMonthDays(_year, _month))
		{
			return true;
		}
		return false;
	}
	int GetMonthDays(int year, int month)   //获取当前月份对应的天数
	{
		static int MonthDays[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
		if (month != 2)
		{
			return MonthDays[month];
		}
		else
		{
			if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))   //闰年
			{
				return 29;
			}
			return 28;
		}
	}
	Date operator+(int day);  //重载+
	Date& operator+=(int day);//重载++
	Date& operator++();    //前置++
	Date operator++(int);  //后置++

	Date operator-(int day);  //重载-
	Date& operator-=(int day);//重载-=
	Date& operator--();    //前置--
	Date operator--(int);  //后置--
	int operator-(const Date& d);   //两日期类对象相减返回一个整数

	bool operator>(const Date& d);  //重载>
	bool operator<(const Date& d);
	bool operator>=(const Date& d);
	bool operator<=(const Date& d);
	bool operator==(const Date& d);
	bool operator!=(const Date& d);

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

inline Date Date::operator+(int day)
{
	Date tmp(*this);
	if (day < 0)
	{
		return *this - (-day);
	}
	tmp._day = _day + day;
	while (!tmp.IsInvalid())
	{
		int MonthDay = GetMonthDays(tmp._year, tmp._month);
		tmp._day -= MonthDay;
		tmp._month++;
		if (tmp._month == 13)
		{
			tmp._year++;
			tmp._month = 1;
		}
	}
	return tmp;
}
inline Date&  Date::operator+=(int day)
{
	*this = *this + day;   //对加法重载的代码复用
	return *this;
}
inline Date&  Date::operator++()    //前置++
{
	return *this+=1;
}
inline Date  Date::operator++(int)  //后置++
{
	Date tmp(*this);
	*this += 1;
	return tmp;
}
inline Date Date::operator-(int day)
{
	Date tmp(*this);
	if (day < 0)
	{
		return *this + (-day);
	}
	tmp._day = _day - day;
	while (!tmp.IsInvalid())
	{
		if (tmp._month != 1)
		{
			tmp._month--;
		}
		else   //month==1的情况
		{
			tmp._year--;
			tmp._month = 12;
		}
		int MonthDay = GetMonthDays(tmp._year, tmp._month);
		tmp._day += MonthDay;
	}
	return tmp;

}
inline Date& Date::operator-=(int day)
{
	*this = *this - day;
	return *this;
}
inline Date& Date::operator--()    //前置--
{
	return *this -= 1;
}
inline Date Date::operator--(int)  //后置--
{
	Date tmp(*this);
	*this -= 1;
	return tmp;
}
inline bool Date::operator>(const Date& d)
{
	if (_year > d._year || (_year == d._year && (_month > d._month || (_month == d._month && _day > d._day))))
	{
		return true;
	}
	return false;
}
inline bool Date::operator<(const Date& d)
{
	return !(*this > d || *this == d);
}
inline bool Date::operator>=(const Date& d)
{
	return (*this > d || *this == d);
}
inline bool Date::operator<=(const Date& d)
{
	return (*this < d || *this == d);
}
inline bool Date::operator==(const Date& d)
{
	if (_year == d._year && _month == d._month && _day == d._day)
	{
		return true;
	}
	return false;
}
inline bool Date::operator!=(const Date& d)
{
	return !(*this == d);
}
inline int Date::operator-(const Date& d)     //两日期类对象相减返回一个整数
{
	int count = 0;
	Date tmp(*this);
	Date tmp2(d);
	
	if (tmp < tmp2)   //第一个日期比第二个小交换顺序
	{
		Date x;
		x = tmp;
		tmp = tmp2;
		tmp2 = x;
	}

	//处理_day使两个日期类的_day相同
	count = GetMonthDays(tmp2._year, tmp2._month) - tmp2._day + tmp._day;
	tmp2._day = tmp._day;
	if (tmp._day >= tmp2._day)  //count多加了一个月
	{
		tmp2._month++;
		if (tmp2._month == 13)
		{
			tmp2._year++;
			tmp2._month = 1;
		}
	}
	//处理_month使两日期类_month相同
	while (tmp._month != tmp2._month)
	{
		count = count + GetMonthDays(tmp2._year, tmp2._month);
		tmp2._month++;
		if (tmp2._month == 13) 
		{
			tmp2._year++;
			tmp2._month = 1;
		}
	}
	//处理两日期类的_year
	while (tmp._year > tmp2._year)
	{
		//判断是否为闰年,并且在二月份一下的才能算366天
		if (((tmp2._year % 4 == 0 && tmp2._year % 100 != 0) || (tmp2._year % 400 == 0)) && (tmp2._month <= 2))
		{
			count += 366;
		}
		else
		{
			count += 365;
		}
		tmp2._year++;
	}
	return count;
}


  • 9
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值