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

初级C++:日期类的实现

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

初级C++:日期类的实现

文章目录
  • 短短的前言
  • 日期类

短短的前言
  • 对于自定义类型对象的运算符重载,要和原生运算符有相同的功能,比如等号,可以连续赋值
  • 以日期为例,日期之间的相加,没有意义;反之,日期之间的相减,则有意义。
  • 成员函数之间的复用,实现几个主要的,其他调用微调即可;复用参考:调用拷贝构造函数次数小的函数
日期类

**********************************头文件********************************
#include
#include
using namespace std;

class Date
{
public:
	/获取当月的天数
	int GetDays(int year, int month);
	/判断构造函数初始化的日期是否合法
	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 > GetDays(_year, _month))
		{
			cout << _year << "/" << _month << "/" << _day << "->";
			cout << "非法日期" << endl;
		}

	}
	void Print()
	{
		cout << _year << "年" << _month << "月" << _day << "日n";
	}
	Date& operator=(const Date d)
	{
		if (this != &d)
		{
			_year = d._year;
			_month = d._month;
			_day = d._day;
		}
		return *this;
	}
	/判断两个日期是否相等   需要改变实参的就用引用,不需要的就加const
	bool operator==(const Date& d);
	/判断比较两个日期的大小
	bool operator<(const Date& d);
	/>  >=同理
	bool operator>(const Date& d);
	bool operator<=(const Date& d);
	/+=天数
	Date& operator+=(int);
	/-=天数
	Date& operator-=(int d);
	/后置++加一天
	Date& operator++();
	/后置--,返回源来的,自减一
	Date operator++(int d);
	/加天数,减去天数同理
	Date operator+(int d);

	/减去日期相减
	int operator-(const Date& d);

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

**********************************函数定义********************************
#include"Dated.h"
/获取当月天数
int Date::GetDays(int year,int month)
{
	assert(month > 0 && month < 13);
	int ymd[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
	if (month == 2 && ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0))
	{
		return 29;
	}
	//正好和数组的值一一对应
	return ymd[month];
}

bool Date::operator==(const Date& d)
{
	return d._year == _year &&
		d._month == _month &&
		d._day == _day;
}

bool Date::operator<(const Date& d)
{
	if (_year
		return true;
	}
	else if(_year == d._year && _month < d._month)
	{
		return true;
	}
	else if (_year == d._year && _month == d._month  && _day
		return true;
	}
	return false;
}

/加上天数产生进位
Date& Date::operator+=(int d)
{
	if (d<0)
	{
		return *this -= d;
	}
	_day += d;
	while (_day>GetDays(_year,_month))
	{
		_day -= GetDays(_year, _month);
		_month++;
		if (_month>12)
		{
			++_year;
			_month=1;
		}
	}
	return *this;
}

Date& Date::operator-=(int d)
{
	if (d<0)
	{
		return *this += d;
	}
	_day -= d;
	while (_day<=0)
	{
		--_month;
		if (_month <1)
		{
			--_year;
			_month = 12;
		}
		//6. 8 - 30 = -22 + 31 = 5.9 
		_day += GetDays(_year, _month);
	}
	return *this;
}

/后置++加一天
Date& Date::operator++()
{
	*this += 1;
	return *this;
}

Date  Date::operator++(int d)
{
	Date tmp(*this);
	*this += 1;
	return tmp;
}

/加上天数
Date Date::operator+(int d)
{
	Date tmp = *this;
	tmp += d;
	return tmp;
}

bool Date::operator>(const Date& d)
{
	if (!(*this == d) && !(* this < d))
		return true;
	return false;
}


int Date::operator-(const Date& d)
{
	/默认大小日期
	Date max(*this);
	Date min = d;
	int flag = 1;
	if (max < min)/比较函数那里已经处理了
	{
		Date tmp = max;
		 max=min;
		min = tmp;
		flag = -1;
	}
	/利用++函数,直到两个函数相等
	int countd = 0;
	while (min < max)
	{
		++countd;
		++min;
	}
	return countd*flag;
}

bool Date::operator<=(const Date& d)
{
	if (*this == d  && !(*this > d))
	{
		return true;
	}
	else
	{
		return false;
	}
}
**********************************测试源********************************
int main()
{
	Date d1(2020,6,8);
	Date d2(d1);
	d2 -= 10;
	d2.Print();

	Date d3 = d1;
	d3 = d2;
	d3 += 100;
	d3.Print();

	d2 += 101;

	/ 输出操作符优先级较高
	cout << (d1 < d2) << endl;
	cout << (d1 == d2) << endl;
	cout << (d3 > d2) << endl;
	cout << "相差 " << (d2 - d3) << "  天  " << endl;
	return 0;
}
  • 当一个对象用一个对象初始化时,调用的是拷贝构造;已经创建出来的,用=,才是赋值重载。
  • 大于当月天数,月就进位,再减去新月当月的天数;当天数小于1,则月减一位,天数加上当月天数
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/960458.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

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

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