C++ 日期类的实现

支持 日期的加减天数,以及日期减日期得到相差的天数。主要包含一下内容:

1.基本成员变量:

private:

	int _year;
	int _month;
	int _day;

2.构造函数 与拷贝构造的实现

       

//构造函数
Date::Date(const int year ,const int month ,const int day ) {
	if (!IsLegalDate(year, month, day)) {
		cout << "非法日期!" << std::endl;
		exit(1);
	}//检查
	_year = year;
	_month = month;
	_day = day;
}
//拷贝构造
Date::Date(const Date& d) {
	_year = d._year;
	_month = d._month;
	_day = d._day;
}

3.日期类的基本功能

//判断日期是否合法
bool Date::IsLegalDate(const int year, const int month, const int day)const {
	if (year <= 0 || (month <= 0 && month > 12) || (day <= 0 || day > GetMonthDay(year, month)))
		return false;
	return true;
}
//返回月份天数
int Date::GetMonthDay(const int year, const int month)const {
	static int MonthDayArray[] = { 31,28,31,30,31,30,31,31,30,31,30,31 };
	return MonthDayArray[month - 1] + (month == 2 && IsLeapYear(year) ? 1 : 0);
}
//打印年月日
void print() const{
	cout << _year << '.' << _month << '.' << _day << std::endl;
}
//是否是闰年
	bool IsLeapYear(const int year) const{
		return (!(year % 4) && year % 100) || !(year % 400);
	}

3.运算符重载

包含一些基础算法。

//+= 天数
Date& Date:: operator+=(const int day) {
	_day += day;//加天数
	for (int monthday = GetMonthDay(_year, _month); _day > monthday; monthday = GetMonthDay(_year, _month)) {//调整
		_day -= monthday;
		_month++;
		if (_month > 12) {
			_month = 1;
			_year++;
		}
	}
	return *this;
}
//+ 天数
Date Date::operator+(const int day) const {
	Date tmp(*this);
	return tmp += day;
}


//-=天数
Date& Date::operator-=(const int day) {
	_day -= day;//减天数
	while (_day <= 0) {//调整
		_month--;
		if (_month <= 0) {
			_month = 12;
			_year--;
			if (_year <= 0) {//检查
				cout << "非法日期!" << std::endl;
				exit(1);
			}
		}
		_day += GetMonthDay(_year, _month);
	}
	return *this;
}
//- 天数
Date Date::operator-(const int day)const
{
	Date tmp(*this);
	return tmp-=day;
//前置++
	Date& operator++() {
		return *this += 1;
	}
	//后置++
	//后置++其实是前置++的重载函数,参数只用于区分,并不使用
	Date operator++(int) {
		Date tmp(*this);
		*this += 1;
		return tmp ;
	}
	//前置--
	Date& operator--() {
		return *this -= 1;
	}
	//后置--
	//参数用于区分重载
	Date operator--(int) {
		Date tmp(*this);
		*this -= 1;
		return tmp;
	}
//日期类-日期类返回间隔的天数(与日期减天数构成函数重载)
int Date::operator-(Date const& d)const {
	int dev = 0;
	if (_year > d._year)//计算年差
		for (int y = d._year; y != _year; y++)
			dev += IsLeapYear(y) ? 366 : 365;
	else
		for (int y = _year; y != d._year; y++)
			dev -= IsLeapYear(y) ? 366 : 365;
	//月差
	if (_month > d._month)
		for (int m = d._month; m != _month; m++)
			dev += GetMonthDay(d._year, m);
	else
		for (int m = _month; m != d._month; m++)
			dev -= GetMonthDay(d._year, m);
	//日差
	dev += _day - d._day;
	return dev;
}
//赋值运算符 根据原赋值运算符的规则,返回引用
Date& Date::operator=(Date const& d) {
	if (this != &d) {
		_year = d._year;
		_month = d._month;
		_day = d._day;
	}
	return *this;
}
//关系运算符 年月日依次比较
bool Date::operator<(const Date& d) const{
	return _year < d._year ? true : (_year == d._year ? (_month < d._month ? true : (_month == d._month ? (_day < d._day) : false)) : false);
}
bool Date::operator==(const Date& d) const{
	return d._year == _year && d._month == _month && d._day == _day;
}
bool Date::operator>(const Date& d) const{
	return _year > d._year ? true : (_year == d._year ? (_month > d._month ? true : (_month == d._month ? (_day > d._day) : false)) : false);
}

	
	bool operator<=(const Date& d)const {
		return *this == d || *this < d;
	}
	bool operator>=(const Date& d)const {
		return *this == d || *this > d;
	}
	bool operator!=(const Date& d)const {
		return !(*this == d);
	}
	//取反重载为 将日期的真实性颠倒
	bool operator!()const {
		return !IsLegalDate(_year, _month, _day);
	}

完整代码如下:https://gitee.com/leyi999/Cpulspuls/blob/master/%E6%97%A5%E6%9C%9F%E7%B1%BB%E7%9A%84%E5%AE%9E%E7%8E%B0/test.cpphttps://gitee.com/leyi999/Cpulspuls/blob/master/%E6%97%A5%E6%9C%9F%E7%B1%BB%E7%9A%84%E5%AE%9E%E7%8E%B0/test.cpp

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

余额充值