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

【C++初阶】日期类的实现

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

【C++初阶】日期类的实现

目录

 1.获取某年某月的天数

2.全缺省构造函数

3.赋值运算符重载

日期+=天数

日期+天数

日期-天数

日期-=天数

前置++

后置++

前置--

后置--

日期-日期

4.运算符重载

日期类的实现:Gitee


 1.获取某年某月的天数
//获取某年某月的天数
	int GetMonthDay(int year,int month)
	{
		assert(month > 0 && month < 13);
		static int monthDays[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
		//闰年的2月是29天
		if (month == 2 && (year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
		{
			return 29;
		}
		return monthDays[month];
	}

2.全缺省构造函数
//全缺省的构造函数
	Date(int year = 0, int month = 1, int day = 1)
	{
		_year = year;
		_month = month;
		_day = day;
		//判断日期是否合法
		if (_year < 0
			|| _month <=0 || _month >= 13
			|| _day <= 0 || _day > GetMonthDay(_year,_month))
		{
			cout << _year << "/" << _month << "/" << _day << "->"<<"非法日期"< 

3.赋值运算符重载

日期+=天数
//日期+=天数
Date& Date::operator+=(int day)
{
	if (day < 0)
	{
		return *this -= -day;
	}
	_day += day;
	//日期不合法,进位
	while (_day > GetMonthDay(_year, _month))
	{
		_day -= GetMonthDay(_year, _month);
		_month++;
		if (_month == 13)
		{
			_year++;
			_month = 1;
		}
	}
	return *this;//出了作用域,d1还在

	//赋用+
	
}

日期+天数
//日期+天数
Date Date::operator+(int day)
{
	

	//赋用+=
	Date ret = *this;
	ret += day;
	return ret;
}

日期-天数
//日期-天数
Date Date::operator-(int day)
{
	Date ret = *this;
	ret -= day;
	return ret;
}

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

前置++
//前置++
Date& Date::operator++()
{
	//_day += 1;
	日期不合法,进位
	//if (_day > GetMonthDay(_year, _month))
	//{
	//	_day -= GetMonthDay(_year, _month);
	//	_month++;
	//	if (_month == 13)
	//	{
	//		_year++;
	//		_month = 1;
	//	}
	//}
	//赋用
	*this += 1;
	return *this;
}

后置++

按正常的运算符重载规则,无法区分前置++和后置++重载
为了区分,这里做一个特殊处理,给后置++增加一个int参数,这样他们两个就构成函数重载

//后置++
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);
	*this -= 1;
	return tmp;
}

日期-日期
//日期-日期   
int Date::operator-(const Date& d)
{
	Date max = *this, 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;
}

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

日期类的实现:Gitee

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

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

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