栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C++ 日期类的实现

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/920298.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号