c++运算符重载


#include<iostream>
#include<windows.h>
using namespace std;
class Date
{
public:
	Date(int year=1990, int month=1,int day=1)
	{
		if (year <= 0 || month <= 0 || month > 12 || day <= 0 || day > yearmonthday(year, month))
		{
			cout << "输入日期有误" << endl;
			_year = year;
			_month = month;
			_day = day;
		}
		else
		{
			_year = year;
			_month = month;
			_day = day;
		}
		
	}
	Date(const Date& d)
	{
		this->_year = d._year;
		this->_month = d._month;
		this->_day = d._day;
	}

	int yearmonthday(int year, int month)
	{
		static int arr[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
		if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
		{
			if (2 == month)
			{
				arr[month-1] += 1;
			}
		}
		int day = arr[month - 1];
		return day;
	}
	
	Date& operator+=(int day)
	{
		if (day<0)
		{
			return *this -= -day;
		}
		_day += day;
		//考虑是否需要进位
		while(_day > yearmonthday(_year, _month))
		{
			_day -= yearmonthday(_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 += yearmonthday(_year, _month);
		}
		return *this;
	}
	Date operator+(int days)
	{
		Date ret(*this);
		ret += days;
		return ret;
	}
	Date operator-(int days)
	{
		Date ret(*this);
		ret -= days;
		return ret;
	}
	/*前置++*/
	Date& operator++()
	{
		return *this += 1;   //先自加,再返回
	}
	//后置++
	Date operator++(int)
	{
		Date ret(*this);     //先返回自加之前的值,再返回
		*this+= 1;
		return ret;
	}
	Date operator--(int)      //后置--
	{
		Date ret(*this);
		*this-= 1;
		return ret;
	}
	Date& operator--()      //前置--
	{
		*this -= 1;
		return *this;
	}
	bool operator==(const Date& d) const
	{
		return this->_year == d._year&&this->_month == d._month&&this->_day == d._day;
	}
	bool operator>(const Date& d) const
	{
		if (this->_year > d._year)
		{
			return true;
		}
		else if ((this->_year == d._year) && (this->_month > d._month))
		{
			return true;
		}
		else if ((this->_year == d._year) && (this->_month >= d._month) && this->_day > d._day)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	bool operator>=(const Date& d) const
	{
		return !(*this < d );
	}
	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);
	}
	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;
				++day;
			}
		}
		else
		{
			while (ret>d)
			{
				--ret;
				--day;
			}
		}
		return day*flag;
	}
	void display()
	{
		cout << _year<<"." << _month <<"."<< _day;
	}

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


int main()
{
	Date d1(2019, 3,1);
	Date d2(2020, 1,1);
	cout<< "d1:" << endl;
	d1.display();
	cout <<endl<<"d2:"<< endl;
	d2.display();
	cout << 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) << endl;
	cout << "d1<d2     " << (d1 < d2) << endl;
	++d1;
	++d2;
	cout << "++d1:" << endl;
	d1.display();
	cout <<endl<< "++d2:" << endl;
	d2.display();
	Date d3=d2 + 365;
	cout << endl << "d2+365" << endl;
	d3.display();
	Date d4 = d2 - 365;
	cout <<endl <<"d2-365" << endl;
	d4.display();

	system("pause");
	return 0;

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值