用C++书写一个简单的Date类

该类的要求
1、输入一个日期判断其是否合法
2、给该日期加上或减去一个天数,返回一个日期。
3、给两个日期相减,要求其返回它们之间相差的天数。
4、这里也要区别一下前置++,后置++和前置- -,后置- -的区别。
5、输入两个日期进行比较然后,返回一个bool型。

class Date
{
public:
	Date(int year = 1900, int month = 1, int day = 1)
	{
		//输入一个日期判断其是否合法
		if (year <= 0 || month > 12 || month<1 || day <= 0 || day>GetMonthDay(year, month))
		{
			cout << "非法日期!日期已设为默认值:1900-1-1"<<endl;
			_year = 1900;
			_month = 1;
			_day = 1;
		}
		//如果不是非法日期就进行直接赋值操作
		_year = year;
		_month = month;
		_day = day;
	}
	//Date类的拷贝函数
	Date(const Date &d)
	{
		_year = d._year;
		_month = d._month;
		_day = d._day;
	}
	int GetMonthDay(int year, int month)
	{
		//static延长局部变量的生命周期 在静态数据段
		//堆空间存malloc开辟 栈里面函数局部变量 代码段存代码
		static int days[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
		int day = days[month];
		if (month == 2){
			//判断是否为闰年
			if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)){
				day += 1;
			}
		}
		return day;
	}
	//加上一个天数要求其返回一个日期
	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){
				_month = 1;
				++_year;
			}
		}
		return *this;
	}
	//减去一个天数要求返回一个日期
	Date& operator-=(int day)
	{
		//考虑减的日期是否为负
		if (day<0){
			//减去一个负的就相当加上一个正的
			return *this += -day;
		}
		_day -= day;
		//考虑是否借位
		while (_day <= 0)
		{
			--_month;
			if (_month ==0){
				_month = 12;
				--_year;
			}
			//考虑要先前一个月进行 
			_day += GetMonthDay(_year, _month);
		}
		return *this;
	}
	//赋值运算符重载
	Date &operator=(const Date& d)
	{
		if (this != &d){
			_year = d._year;
			_month = d._month;
			_day = d._day;
		}
	}
	//这是一个局部变量
	Date operator+(int days)//这里用引用不合适,引用指向的空间不受保护
	{
		//Date+day,本身Date没变
		//拷贝一个,用一下拷贝构造
		Date ret(*this);
		ret += days;
		return ret;
	}
	Date operator-(int days)
	{
		Date ret(*this);
		ret -= days;
		return ret;
	}
	//两个日期相减
	int operator-(const Date&d)
	{
		//如果是大日期-小日期就是正的,小日期累加就是大日期
		//如果是小日期-大日期就是负的,小日期累加就是大日期(负向)
		//本身这两个值就没发生变化,因此要进行拷贝操作
		Date ret(*this);
		int flag = 1;// 作用起到一个标记作用,如果是正向就相乘,负向就乘以其相反数
		if (ret<d){
			flag = -1;
		}
		int day=0;//在这里做一个标记,作用其目的就是想让其值等于相差的天数
		if (ret < d){
			while (ret < d){
				++ret;//加ret天数直到其和d的日期相等为止。
				++day;
			}
		}
		else{
			while (ret>d){
				--ret;
				++day;
			}
		}
		return day*flag;
	}
	//前置++,++Date
	Date& operator++()
	{
		//先自加再返回
		return *this += 1;
	}
	//后置++,Date++
	Date operator++(int)
	{
		//Date的值变了,但返回的值没变
		Date ret(*this);
		*this+= 1;
		return ret;
	}
	//前置--,--Date
	Date& operator--()
	{
		return *this -= 1;
	}
	//后置--,Date--
	Date operator--(int)
	{
		Date ret(*this);
		*this -= 1;
		return ret;
	}
	//*this >d
	bool operator>(const Date& d)const
	{
		if (this->_year > d._year){
			return true;
		}
		else if (this->_year == d._year){
			if (this->_month > d._month){
				return true;
			}
			else if (this->_month == d._month){
				if (this->_day > d._day){
					return true;
				}
			}
		}
		return false;
	}
	bool operator>=(const Date& d)const
	{
		return (*this > d) || (*this == d);
	}
	bool operator<(const Date& d)const
	{
		return !(*this >= d);
	}
	bool operator<=(const Date& d)const
	{
		return !(*this>d);
	}
	bool operator==(const Date& d)const
	{
		return _year == d._year&&_month == d._month&&_day == d._day;
	}
	bool operator!=(const Date& d)const
	{
		return !(*this == d);
	}
	void display()
	{
		cout << _year << "-" << _month << "-" << _day << endl;
	}
private:
	int _year;
	int _month;
	int _day;
};

测试函数

void TestDate()
{
	Date day1(2019, 1, 30);
	cout << "原来day1日期" << endl;
	day1.display();
	Date day2(2019, 3, 8);
	cout << "原来day2日期" << endl;
	day2.display();
	day1++;
	cout << "day1++: ";
	day1.display();
	day2--;
	cout << "day2--: ";
	day2.display();
	Date day3=day1 + 62;
	cout << "day1++: +62: ";
	day3.display();
	Date day4=day2 - 96;
	cout << "day2--: -96: ";
	day4.display();
	//测定
	Date day5(2018, 13, 31);
	day5.display();
}
void TestDate2()
{
	Date d1(2019, 3, 8);
	cout << "原来d1日期" << endl;
	d1.display();
	Date d2(2019,1,1);
	cout << "原来d2日期" << endl;
	d2.display();
	int d3 = d1 - d2;
	int d4 = d2 - d1;
	cout <<"d3=d1-d2: "<<d3 << endl;
	cout << "d4=d2-d1: " << d4 << endl;
	cout <<"d1>d2:  "<< (d1 > d2) << endl;
	cout << "d1>=d2:  " << (d1 >= d2) << endl;
	cout << "d1<d2:  " << (d1<d2) << endl;
	cout << "d1<=d2:  " << (d1<= d2) << endl;
	cout << "d1==d2:  " << (d1==d2) << "\n";
	cout << "d1!=d2:  " << (d1 != d2) << "\n";
}

输出结果:
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_43198968/article/details/88691479