C++运算符重载(日期类全实现)

#include<iostream>
using namespace std;

class Date
{
public:
	//Date(){}
	~Date() {}
	int getMonthDay(int y, int m)
	{
		switch (m)
		{
		case 1:
		case 3:
		case 5:
		case 7:
		case 8:
		case 10:
		case 12:return 31;
		case 4:
		case 6:
		case 9:
		case 11:return 30;
		case 2:
			if (y % 400 == 0 || y % 4 == 0 && y % 100 != 0)
				return 29;
			return 28;
		}
	}
	Date(int y = 1900, int  m = 1, int d = 1)
	{
		this->year = y;
		this->month = m;
		this->day = d;
		cout << "this" << this << endl;
		//cout << year << "/" << month << "/" << day << endl;
	}
	Date(const Date& d)//必须传引用
	{
		year = d.year;
		month = d.month;
		day = d.day;
		cout << "d:" << &d << endl;
		cout << "this:" << this << endl;
	}
	Date& operator=(const Date& d)//赋值运算符重载必须为成员函数
	{
		this->year = d.year;
		this->month = d.month;
		this->day = d.day;
		//cout << year << "/" << month << "/" << day << endl;
		return *this;
	}
	Date& operator+=(int d)
	{
		while (d > this->getMonthDay(year, month))//增加的天数小于当前月份的最大天数
		{
			d -= this->getMonthDay(year, month);
			month++;
			if (month == 13)
			{
				month = 1;
				year++;
			}
		}
		day += d;
		if (day > this->getMonthDay(year, month))//此时day有可能会超过当前月的最大天数
		{
			day -= this->getMonthDay(year, month);
			month++;
		}
		cout << year << "/" << month << "/" << day << endl;
		return *this;
	}
	Date operator+(int d)
	{
		Date temp;
		temp = *this;
		while (d > this->getMonthDay(year, month))//增加的天数小于当前月份的最大天数
		{
			d -= this->getMonthDay(year, month);
			month++;
			if (month == 13)
			{
				month = 1;
				year++;
			}
		}
		day += d;
		if (day > this->getMonthDay(year, month))//此时day有可能会超过当前月的最大天数
		{
			day -= this->getMonthDay(year, month);
			month++;
		}
		cout << year << "/" << month << "/" << day << endl;
		return temp;//只是返回值不同
	}
	Date operator-(int d)
	{
		Date temp;
		temp = *this;
		while (d > this->getMonthDay(year, month))//增加的天数小于当前月份的最大天数
		{
			d -= this->getMonthDay(year, month);
			month--;
			if (month == 0)
			{
				month = 12;
				year--;
			}
		}
		day -= d;
		if (day < 0)//此时day有可能会超过当前月的最大天数
		{
			month--;
			day += this->getMonthDay(year, month);
		}
		cout << year << "/" << month << "/" << day << endl;
		return temp;//只是返回值不同
	}
	Date& operator-=(int d)
	{
		while (d > this->getMonthDay(year, month))//增加的天数小于当前月份的最大天数
		{
			d -= this->getMonthDay(year, month);
			month--;
			if (month == 0)
			{
				month = 12;
				year--;
			}
		}
		day -= d;
		if (day < 0)//此时day有可能会超过当前月的最大天数
		{
			month--;
			day += this->getMonthDay(year, month);
		}
		cout << year << "/" << month << "/" << day << endl;
		return *this;
	}
	Date& operator++()//前++, 返回的是自定义数据类型的引用
	{
		day++;
		if (day > this->getMonthDay(year, month))
		{
			month++;
			if (month > 12)
			{
				year++;
				month = 1;

			}
			day = 1;
		}
		//cout << year << "/" << month << "/" << day << endl;
		return *this;
	}
	Date operator++(int)//后++,有占位参数int,实际调用:d1.operator(100)  参数没有实际意义,这样显示调用,也可以:d1++
	{
		Date temp = *this;
		day++;
		if (day > this->getMonthDay(year, month))
		{
			month++;
			if (month > 12)
			{
				year++;
				month = 1;
				day = 1;
			}
		}
		//cout << year << "/" << month << "/" << day << endl;
		return temp;
	}
	Date operator--(int)
	{
		Date temp = *this;
		day--;
		if (day < 0)
		{
			month--;
			if (month < 0)
			{
				year--;
				month = 12;
				day = 31;
			}
			day = this->getMonthDay(year, month);
		}
		return temp;

	}
	Date& operator--()
	{
		day--;
		if (day < 0)
		{
			month--;
			if (month < 0)
			{
				year--;
				month = 12;
				day = 31;
			}
			day = this->getMonthDay(year, month);
		}
		return *this;
	}
	bool operator>(const Date& d)
	{
		if (this->year != d.year)
			return this->year > d.year ? true : false;
		else if (this->month != d.month)
			return this->month > d.month ? true : false;
		else if (this->day != d.day)
			return this->day > d.day ? true : false;
	}
	bool operator==(const Date& d)
	{
		if (this->day == d.day && this->month == d.month && this->year == d.year)
			return true;
		return false;
	}
	bool operator>=(const Date& d)
	{
		if (this->year != d.year)
			return this->year >= d.year ? true : false;
		else if (this->month != d.month)
			return this->month >= d.month ? true : false;
		else if (this->day != d.day)
			return this->day >= d.day ? true : false;
	}
	bool operator<(const Date& d)
	{
		if (this->year != d.year)
			return this->year < d.year ? true : false;
		else if (this->month != d.month)
			return this->month < d.month ? true : false;
		else if (this->day != d.day)
			return this->day < d.day ? true : false;
	}
	bool operator<=(const Date& d)
	{
		if (this->year != d.year)
			return this->year <= d.year ? true : false;
		else if (this->month != d.month)
			return this->month <= d.month ? true : false;
		else if (this->day != d.day)
			return this->day <= d.day ? true : false;
	}
	bool operator!=(const Date& d)
	{
		if (this->day == d.day && this->month == d.month && this->year == d.year)
			return false;
		return true;
	}
	int getyearDay(int year)
	{
		if (year % 400 == 0 || year % 4 == 0 && year % 100 != 0)
			return 366;
		return 365;
	}
	int operator-(const Date& d)
	{
		int dis = 0;
		Date temp = d;
		while (++temp != *this)//顶级算法
			dis++;
		cout << year << "/" << month << "/" << day << "到" << d.year << "/" << d.month << "/" << d.day << "有" << dis << "天" << endl;
		return dis;
	}
	friend ostream& operator<<(ostream& os, const Date& d);
private:
	int year;
	int month;
	int day;
};

ostream& operator<<(ostream& os, const Date& d)
{
	cout << d.year << "/" << d.month << "/" << d.day << endl;
	return os;
}
int main()
{
	//Date d1(2022,11,20);
	//cout << d1.getMonthDay(2022, 11) << endl;
	//Date d2(d1);
	//Date d3;
	//d3 = d2;
	d3 += 1000;
	//d3 += 100;
	//d3 + 1;
	//d3 - 100;
	//d3 -= 1;

	//Date d4(2022, 12, 31);
	++d4;

	//cout<<(d4 > d3);

	//d4 - d3;
	Date d1;
	++d1;
	cout << d1;
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值