C++ 类和对象------日期类运算符重载

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


class Date   
{
public:
	Date(int year = 1900, int month = 1, int day = 1)	//构造
		:_year(year)
		, _month(month)
		, _day(day)
	{
		if (!IsInvalid()) // this->IsInvalid(this) 
		{
			assert(false);
			//cout<<"非法日期"<	//exit(-1); 
		}
	}

	Date(const Date& d)	//拷贝构造
		:_year(d._year)
		, _month(d._month)
		, _day(d._day)
	{}

	Date& operator=(const Date& d)	//赋值操作符的重载
	{
		if (this != &d){
			this->_year = d._year;
			this->_month = d._month;
			this->_day = d._day;
		}
		return *this;
	}

	~Date()	//析构
	{
		cout << "~Date" << endl;
	}

	bool IsInvalid()	//判断日期是否合法
	{
		if (_year > 0 && (_month > 0 && _month < 13) && (GetMonthDay(_year, _month) >= _day && _day > 0)){
			return true;
		}
		else
		{
			return false;
		}
	}

	bool isLeapYear(int year)	//判断该年是否是闰年
	{
		if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0){
			return true;
		}
		return false;
	}

	int GetMonthDay(int year, int month)	//获取当前月天数
	{
		assert(month > 0 || month < 13);
		static int array[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
		if (isLeapYear(year)){
			if (month == 2){
				return array[month] + 1;
			}
		}
		return array[month];
	}

	void Show()	//打印日期
	{
		cout << _year << "-" << _month << "-" << _day << endl;
	}

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

	bool operator!=(const Date& d)
	{
		if (operator==(d)){
			return false;
		}
		return true;
	}

	bool operator>=(const Date& d)
	{
		if (operator>(d) || operator==(d)){
			return true;
		}
		return false;
	}

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

	bool 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;
	}
		// d1 < d2 
	bool operator<(const Date& d)
	{
		if (operator>=(d)){
			return false;
		}
		return true;
	}
		// d1 + 10 
	Date operator+(int day)
	{
		if (day < 0){
			return operator-(-day);
		}
		Date ret = *this;
		ret._day += day;
		while (ret._day >GetMonthDay(ret._year, ret._month)){
			ret._day -= GetMonthDay(ret._year, ret._month);
			ret._month++;
			if (ret._month > 12){
				ret._year++;
				ret._month = 1;
			}
		}
		return ret;
	}

	Date& operator+=(int day)
	{
		if (day < 0){
			return operator-=(-day);
		}
		_day += day;
		while (_day > GetMonthDay(_year, _month)){
			_day -= GetMonthDay(_year, _month);
			_month++;
			if (_month > 12){
				_year++;
				_month = 1;
			}
		}
		return *this;
	}

	Date operator-(int day)
	{
		if (day < 0){
			return operator+(-day);
		}
		Date ret = *this;
		ret._day -= day;
		while (day < 0){
			ret._day += GetMonthDay(ret._year, ret._month - 1);
			ret._month--;
			if (ret._month == 0){
				ret._year--;
				ret._month = 12;
			}
		}
		return ret;
	}

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

	int operator-(const Date& d)
	{
		int ret = 0;
		Date A;
		if (operator==(d)){
			printf("相同");
			return ret;
		}
		if (operator>(d)){
			A = d;
			while ((A._year != _year) || (A._month != _month)){
				ret += GetMonthDay(A._year, A._month);
				A._month++;
				if (A._month > 12){
					A._year++;
					A._month = 1;
				}
			}
			ret += _day;
			ret -= d._day;
			return ret;
		}
		else {
			A = *this;
			while ((A._year != d._year) || (A._month != d._month)){
				ret += GetMonthDay(A._year, A._month);
				A._month++;
				if (A._month > 12){
					A._year++;
					A._month = 1;
				}
			}
			ret += d._day;
			ret -= _day;
			return -ret;
		}
	}

	//++d1 
	Date& operator++() // 前置++
	{
		_day++;
		if (_day > GetMonthDay(_year, _month)){
			_month++;
			_day = 1;
			if (_month > 12){
				_year++;
				_month = 1;
			}
		}
		return *this;
	}
		//d1++ 
	Date operator++(int) // 后置++ 
	{
		Date ret = *this;
		_day++;
		if (_day > GetMonthDay(_year, _month)){
			_month++;
			_day = 1;
			if (_month > 12){
				_year++;
				_month = 1;
			}
		}
		return ret;
	}

	Date operator--()	//前置--
	{
		_day--;
		if (_day == 0){
			_month--;
			if (_month == 0){
				_year--;
				_month = 12;
			}
			_day = GetMonthDay(_year, _month);
		}
		return *this;
	}

	Date operator--(int)	//后置--
	{
		Date ret = *this;
		_day--;
		if (_day == 0){
			_month--;
			if (_month == 0){
				_year--;
				_month = 12;
			}
			_day = GetMonthDay(_year, _month);
		}
		return ret;
	}
private:
	int _year;
	int _month;
	int _day;
};

int main()
{
	Date d1;
	//d1.Show(); 

	Date d2(2017, 2, 28);
	d2.Show(); 

	++d1; // d1.operator++(&d1);
	d1.Show();
	d1++; // d1.operator++(&d1, 0); 
	d1.Show();

	Date d3(2012, 9, 4);
	Date d4(2018, 6, 22);

	printf("相差 %d 天\n",d4 - d3);

	while (1){
		;
	}
	return 0;
}

如有错误 请在评论下说明 谢谢

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值