C++ --- 日期类的实现

一、日期类介绍

日期类是一个实现日期相关计算的类,其成员变量有:年(_year)、月(_month)、日(_day),成员函数包括日期的加、减、日期加天数等相关运算的接口。

例如,下图是一个实现日期相关计算的计算器

二、接口定义和重要接口实现思路

1)接口的定义和功能描述

// 获取某年某月的天数
    int GetMonthDay(int year, int month);

    // 全缺省的构造函数
    Date(int year = 1900, int month = 1, int day = 1);

    // 拷贝构造函数
    // d2(d1)
    Date(const Date& d);

    // 赋值运算符重载
    // d2 = d3 -> d2.operator=(&d2, d3)
    Date& operator=(const Date& d);

    // 析构函数
    ~Date();

    // 日期+=天数
    Date& operator+=(int day);

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

    // 日期-天数
    Date operator-(int day);

    // 日期-=天数
    Date& operator-=(int day);

    // 前置++
    Date& operator++();

    // 后置++
    Date operator++(int);

    // 后置--
    Date operator--(int);

    // 前置--
    Date& operator--();

    // >运算符重载
    bool operator>(const Date& d);

    // ==运算符重载
    bool operator==(const Date& d);

    // >=运算符重载
    inline bool operator >= (const Date& d);

    // <运算符重载
    bool operator < (const Date& d);

    // <=运算符重载
    bool operator <= (const Date& d);

    // !=运算符重载
    bool operator != (const Date& d);

    // 日期-日期 返回天数
    int operator-(const Date& d);

2)重点、难点接口的实现思路

1.获取某年某月的天数

一年中,除了二月的天数有闰年和平年之分,其他月份的天数都是固定的。因此,可以定义一个整型数组,数组元素个数为13,其中下标为0的位置不适用,其他1~12表示十个月份的天数。当获取某天的天数时,如果是二月我们在进行单独的判断即可。

int Date::GetMonthDay(int year, int month)
{
	int date[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
	if (month == 2)
	{
		//判断如果是闰年返回29,平年返回28
		if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
			return 29;
		else
			return date[2];
	}
	return date[month];
}

2.赋值运算符的重载

赋值运算符重载的使用方法:d1 = d2  ---> d1.operate=(d2),因此需要将d2的年月日赋值给d1,并且返回d1,d1在这里是隐形的this指针,我们需要返回d1的引用,直接对this解引用即可。

// d2 = d3 -> d2.operator=(&d2, d3)
Date& Date::operator=(const Date& d)
{
	_year = d._year;
	_month = d._month;
	_day = d._day;

	return *this;
}

3.日期+=天数 

先将天数加到日期的_day中,在将日期调整到合法日期。即天满月加1,月满年加1.

// 日期+=天数
Date& Date::operator+=(int day)
{
	_day += day;

	//将日期调整合法
	while(_day > this->GetMonthDay(_year, _month))
	{
		_day -= this->GetMonthDay(_year,_month);
		_month++;
		if (_month > 12)
		{
			_month = _month % 12;
			_year++;
		}
	}

	return *this;
}

4.日期+天数

重新使用当前日期实例化一个新的对象,对新的对象调用+=操作,最后返回新的对象,这样原对象的内容不会改变,返回的+之后的日期。后面很多接口都可以考虑复用已有接口。

// 日期+天数
Date Date::operator+(int day)
{
	//重新实例化一个和this相同的对象,对该对象进行+=操作
	Date ret(*this);

	ret += day;
	return ret;
}

5.日期-=天数 

将当前日期对应的日-day,如果日期小于0,则月份应该-1,继续减当月天数循环此过程直至天>0.

// 日期-=天数
Date& Date::operator-=(int day)
{
	if (day < 0)
	{
		return *this += (-day);
	}

	_day -= day;
	while (_day <= 0)
	{
		_month--;
		if (_month == 0)
		{
			_month = 12;
			_year--;
		}
		_day += this->GetMonthDay(_year,_month);
	}

	return *this;
}

6.日期-日期 

判断出那个日期大那个日期小,用小日期加天数,直到等于大日期为止。

// 日期-日期 返回天数
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)
	{
		++n;
		++min;
	}

	return n*flag;
}

三、接口实现

void Date::displayInfo()
{
	cout << _year << "-" << _month << "-" << _day << endl;
}

// 获取某年某月的天数
int Date::GetMonthDay(int year, int month)
{
	int date[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
	if (month == 2)
	{
		//判断如果是闰年返回29,平年返回28
		if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
			return 29;
		else
			return date[2];
	}
	return date[month];
}

//全省参数的构造函数,声明时定义了,定义时就不需要定义
Date::Date(int year, int month, int day)
{
	_year = year;
	_month = month;
	_day = day;

	//判断日期是否合法
	if (!(_year > 0 &&
		_month > 0 && _month <= 12 &&
		day <= this->GetMonthDay(_year, _month) && day > 0))
		cout << "日期非法" << endl;
}

// 拷贝构造函数
// d2(d1)
Date::Date(const Date& d)
{
	_year = d._year;
	_month = d._month;
	_day = d._day;
}

// 赋值运算符重载
// d2 = d3 -> d2.operator=(&d2, d3)
Date& Date::operator=(const Date& d)
{
	_year = d._year;
	_month = d._month;
	_day = d._day;

	return *this;
}

// 日期+=天数
Date& Date::operator+=(int day)
{
	if (day < 0)
	{
		//+=负数等于-=负数的绝对值
		return *this -= day;
	}

	_day += day;
	//将日期调整合法
	while(_day > this->GetMonthDay(_year, _month))
	{
		_day -= this->GetMonthDay(_year,_month);
		_month++;
		if (_month > 12)
		{
			_month = _month % 12;
			_year++;
		}
	}

	return *this;
}

// 日期+天数
Date Date::operator+(int day)
{
	//重新实例化一个和this相同的对象,对该对象进行+=操作
	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)
		{
			_month = 12;
			_year--;
		}
		_day += this->GetMonthDay(_year,_month);
	}

	return *this;
}

// 前置++
Date& Date::operator++()
{
	//返回++后
	this->operator+=(1);

	return *this;
}

// 后置++
Date Date::operator++(int)
{
	Date ret(*this);

	this->operator++();
	return ret;
}

// 后置--
Date Date::operator--(int)
{
	Date ret(*this);

	this->operator-=(1);
	return ret;
}

// 前置--
Date& Date::operator--()
{
	this->operator-=(1);
	return *this;
}

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

// ==运算符重载
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->operator==(d) || this->operator>(d);
}

// <运算符重载
bool Date::operator < (const Date& d)
{
	return !(this->operator>=(d));
}

// <=运算符重载
bool Date::operator <= (const Date& d)
{
	return this->operator<(d) || this->operator==(d);
}

// !=运算符重载
bool Date::operator != (const Date& d)
{
	return !(this->operator==(d));
}

// 日期-日期 返回天数
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)
	{
		++n;
		++min;
	}

	return n*flag;
}

4.测试用例

int main()
{
	//创建一个默认天数的Date类型的对象
	Date day1;
	//创建一个指定日期的Date类型的对象day2
	Date day2(2021,2,7); 
	day2.displayInfo();

	//测试GetMonthDay
	printf("天数:%d\n",day1.GetMonthDay(2021,2));

	//测试全省参数的构造函数,使用监视窗口测试

	//测试拷贝构造函数,构造出一个和day2相同的对象day3
	Date day3(day2);
	day3.displayInfo();

	//测试赋值运算符
	day1.operator=(day2);
	day1.displayInfo();

	//测试日期+=天数
	day3.operator+=(100);
	day3.displayInfo();

	//测试日期+天数
	day2.operator+(100).displayInfo();
	day2.displayInfo();

	//测试日期-=天数
	day3.displayInfo();
	//day3.operator-=(130);
	//day3.displayInfo(); //2021-1-8
	
	//测试日期-天数
	day3.operator-(107).displayInfo();
	day3.displayInfo();
	day3.operator-=(107);

	//测试前置++
	day3.operator++().displayInfo();
	day3.displayInfo();

	//测试后置++
	day3.operator++(2).displayInfo();
	day3.displayInfo();

	//测试>
	Date day4(2021,3,1);
	printf("%d\n",day4.operator>(day3));

	//测试==
	Date day5(day4);
	printf("%d\n", day5.operator==(day4));

	//测试>=
	printf("%d\n", day5.operator==(day4));


	return 0;
}

 

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

疯狂嘚程序猿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值