C++日期类的实现

目录

一、基本功能

二、代码实现

1.函数的声明:

2.函数的定义


一、基本功能

1.判断是否为闰年

2.日期加(减)天数

3.日期自增(自减)

4.日期减日期

二、代码实现

1.函数的声明:

#include<assert.h>
#include<iostream>
using namespace std;

class Data
{
    
	friend ostream& operator<<(ostream& out, Data& d);
	friend istream& operator>>(istream& in, Data& d);

public:
	//是否为闰年
	bool LeapYear(int year);

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

	//全缺省构造函数
	Data(int year = 2022, int month = 9, int day = 1);

	//拷贝构造函数
	//Data(const Data& d);

	//赋值运算符重载
	Data operator=(const Data& d);
	bool operator>(const Data& d)const;
	bool operator>=(const Data& d)const;
	bool operator<(const Data& d)const;
	bool operator<=(const Data& d)const;
	bool operator==(const Data& d)const;
	bool operator!=(const Data& d)const;

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

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

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

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

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

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

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

	//前置--
	Data operator--();

	//日期-日期
	int operator-(const Data& d);

	//打印
	void print()const
	{
		cout << _year << '-' << _month << '-' << _day << endl;
	}


private:
	int _year;
	int _month;
	int _day;

};

2.函数的定义

#include"Data.h"

//是否为闰年
bool Data::LeapYear(int year)
{
	return (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0));
}

//获取某年某月的天数
int Data::GetMonthDay(int year, int month)
{
    //数组大小最好开13,方便映射,很多成员函数都会用到,所以加上static
	static int daysarr[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
	if (month == 2 && LeapYear(year))
	{
		return 29;
	}
	else
	{
		return daysarr[month];
	}
}

//构造函数
Data::Data(int year = 1 ,int month = 1 , int day = 1)
{
//这里需要加判断,防止输入不合法的日期
	if (month > 0  && month < 13 
		&& day > 0 && day <= GetMonthDay(year, month))
	{
		_year = year;
		_month = month;
		_day = day;
	}
	else
	{
		cout << "非法日期" << endl;
		assert(false);

	}
}

//拷贝构造函数 可以不写,编译器会自动完成浅拷贝
//Data:: Data(const Data& d)
// {
//	 _year = d._year;
//	 _month = d._month;
//	 _day = d._day;
// }

//赋值运算符重载 比较日期,建议先写== >(<),比较大小互斥,因此可以复用。
bool Data::operator>(const Data& d)const
{
	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;

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

bool Data:: operator>=(const Data& d)const
{
	return !(*this < d) ;
}
bool Data::operator<(const Data& d)const
{
	return !(*this > d);
}
bool Data::operator<=(const Data& d)const
{
	return  *this <  d || *this == d ;
}
bool Data::operator!=(const Data& d)const
{
	return !(*this == d);
}

//赋值运算符重载
Data Data::operator=(const Data& d)
{
//如果this == &d 说明传来的值是一样的,就没必要赋值
	if (this != &d)
	{
		_year = d._year;
		_month = d._month;
		_day = d._day;
	}
	return *this;
}

//日期+=天数 自身要改变
//加上天数,判断是否比当前月大,如果大,就让_day减去当前月的天数,月加1,如果小,直接返回
//判断月是否超过12月,超过则月份重置为1,年加1。

Data& Data::operator+=(int day)
{
//如果传过来的是个负数,相当于日期减等天数
	if (day < 0)
	{
		*this -= -day;
		return *this;
	}

	_day += day;
	while (_day > GetMonthDay(_year, _month))
	{
		_day -= GetMonthDay(_year, _month);
		_month++;
		if (_month > 12)
		{
			_year++;
			_month = 1;
		}
	}
	return *this;

}
//日期+天数 自身不变,因此需要拷贝构造一个临时对象。可以直接复用+=
Data Data::operator+(int day)const
{
	Data tmp(*this);
	tmp+= day;

	return tmp;
	
}

//日期-=天数 自身改变
//减去天数,判断_day是否小于1,大于1直接返回,小于1则让月份减1,_day加上月份对应的天数。
//月份小于1则重置为12,年减1.
Data& Data::operator-=(int day)
{
//如果传的天数是个负数,就等于日期+=天数。
	if (day < 0)
	{
		*this += -day;
		return *this;
	}

	_day -= day;
	while (_day < 1)
	{
		_month--;
		if (_month < 1)
		{
			_month = 12;
			_year--;
		}
		_day += GetMonthDay(_year, _month);
	}
	return *this;
}

//日期-天数 自身不变,直接复用-=便可
Data Data::operator-(int day)const
{
	Data tmp(*this);
	tmp._day -= day;
	return tmp;
	
}



//前置++ 返回加1之后的值
Data& Data::operator++()
{
	*this += 1;
	return *this;
}

//后置++ 返回加1之前的值
Data Data::operator++(int)
{
	Data tmp(*this);
	*this += 1;
	return tmp;
}

//后置-- 返回减1之前的值
Data Data::operator--(int)
{
	Data tmp(*this);
	*this -= 1;
	return tmp;
}

//前置-- 返回减1之后的值
Data Data::operator--()
{
	*this -= 1;
	return *this;
}

//日期减日期返回天数
//让小的日期循环加1,直到小的日期等于大的日期,就停下来。用一个count统计小的日期加了多少次。
//最终count就是两日期之间的差
int Data::operator-(const Data& d)
{
 //假定第一个日期>第二个日期
	Data max = *this;
	Data min = d;
	int flag = 1;   
 //假定也许会出错,这时交换
	if (max < min)
	{
		max = d;
		min = *this;
		flag = -1;  
	}
	int count = 0;
	while (max != min)
	{
		++min;//建议用前置++ ,效率更高
        count++;
	}

	return count * flag;   //d1-d2,如果d1<d2,flag为-1则为负数,反之为正。
}


ostream& operator<<(ostream& out, Data& d)
{
	out << d._year << "年" << d._month << "月" << d._day << "日" << endl;

	return out;
}

 istream& operator>>(istream& in, Data& d)
{
	 int year, month, day;
	 in >> year >> month >> day;

	 if (month > 0 && month < 13
		 && day > 0 && day <= d.GetMonthDay(year, month))
	 {
		 d._year = year;
		 d._month = month;
		 d._day = day;
	 }
	 else
	 {
		 cout << "非法日期" << endl;
		 assert(false);
	 }

	 return in;
}

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

余额充值