【c++】日期类相关重载运算符函数实现

前言:代码所需知识点在后两篇博客。默认成员函数+友元函数。

一相关思考

闰年问题:

  • 四年一闰,百年不闰/四百年一闰;
  • 只涉及到二月为28/29天。
  • 可写为类内函数,便于调用。

相关+,-运算符需要考虑隔好几个月的问题,使用循环解决。

二代码

#include<iostream>
using namespace std;	
class date {
public:
	friend ostream& operator<<(ostream& _cout, const date& d);
	date(int year, int mouth, int day)
		:_year(year)
		,_mouth(mouth)
	{
		_year = year;
		_mouth = mouth;
		_day = day;
	}
	date(const date&d) {
		_year = d._year;
		_mouth = d._mouth;
		_day = d._day;
	}
	date& operator=(const date& d) {
		_year = d._year;
		_mouth = d._mouth;
		_day = d._day;
		return *this;
	}
	int getmouthday(int year,int mouth) {
		int day[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
		if (mouth == 2 && ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)))
		{
			return day[2]+1;
		}
		return day[mouth];
	}
	date operator+(int days) {
		date temp(*this);
		int sum = days + _day;
				if (sum <= getmouthday(temp._year, temp._mouth)) {
					temp._day = sum;
					return temp;
				}
				else {
					while (sum > getmouthday(temp._year, temp._mouth))
					{
						sum -= getmouthday(temp._year, temp._mouth);
						if (temp._mouth == 12) {
							temp._mouth = 1;
							temp._year++;
						}
						else {
							temp._mouth++;
						}
					}
					temp._day = sum;
					return temp;
				}
	}
		date operator-(int days) {
			date temp(*this);
			int sum =  _day-days;
			if (sum >=1) {
				temp._day = sum;
				return temp;
			}
			else {
				while (sum <=0)
				{
					if (temp._mouth == 1) {
						temp._mouth = 12;
						temp._year--;
					}
					else {
						temp._mouth--;
					}
					sum += getmouthday(temp._year, temp._mouth);
				}
				temp._day = sum;
				return temp;
			}
		}
		int operator-(const date& d)
		{
			int i;
			int total = (_year - d._year)*365;
			if (_mouth > d._mouth) {
				for ( i = d._mouth; i < _mouth; i++)
				{
					total += getmouthday(_year, i);
				}
					total += _day - d._day;
			}
			else {
				for ( i = _mouth; i < d._mouth; i++)
				{
					total -= getmouthday(_year, i);
				}
				total += _day - d._day;
			}
			for (i = d._year; i <= _year; i++) {
				if ((i % 4 == 0 && i % 100 != 0) || (i % 400 == 0)) {
					if (i == d._year&&d._mouth > 2)
					{
						continue;
					}
					if (i == _year && d._mouth <= 2)
					{
						continue;
					}
					total++;
				}
			}
			return total;
		}
	date operator++() {
		date temp(*this);
		int sum = 1 + _day;
		if (sum <= getmouthday(temp._year, temp._mouth)) {
			temp._day = sum;
			return temp;
		}
		else {
			temp._mouth++;
			temp._day = 1;
			return temp;
		}
	}
	date operator++(int) {
		date temp(*this);
		int sum = 1 + _day;
		if (sum <= getmouthday(_year, _mouth)) {
			_day = sum;
			return temp;
		}
		else {
			while (sum > getmouthday(_year, _mouth))
			{
				sum -= getmouthday(_year, _mouth);
				if (_mouth == 12) {
					_mouth = 1;
					_year++;
				}
				else {
					_mouth++;
				}
			}
			_day = sum;
			return temp;
		}
	}
	date operator--() {
		date temp(*this);
		int sum = _day - 1;
		if (sum >= 1) {
			temp._day = sum;
			return temp;
		}
		else {
				if (temp._mouth == 1) {
					temp._mouth = 12;
					temp._year--;
				}
				else {
					temp._mouth--;
				}
			temp._day = getmouthday(temp._year, temp._mouth);
			return temp;
		}
	}
	date operator--(int) {
		date temp(*this);
		int sum = _day - 1;
		if (sum >= 1) {
			_day = sum;
			return temp;
		}
		else {
			if (_mouth == 1) {
				_mouth = 12;
				_year--;
			}
			else {
				_mouth--;
			}
			_day = getmouthday(_year,_mouth);
			return temp;
		}
	}
	bool operator<(const date& d)const {
		if (_year < d._year || (_year == d._year&&_mouth < d._mouth) || (_year == d._year&&_mouth == d._mouth&&_day < d._day)) {
			return true;
		}
		return false;
	}
	bool operator>(const date& d)const {
		if (_year > d._year || (_year == d._year&&_mouth > d._mouth) || (_year == d._year&&_mouth == d._mouth&&_day > d._day)) {
			return true;
		}
		return false;

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

	}
	bool operator>=(const date& d)const {
		return !(*this < d);
	}
	bool operator==(const date& d)const {
		if (_year == d._year&&_mouth == d._mouth&&_day == d._day) {
			return true;
		}
		return false;
	}
	bool operator!=(const date& d)const {
		return !(*this == d);
	}
private:
	int _year;
	int _mouth;
	int _day;
};
ostream& operator<<(ostream& _cout,const date& d) {
	_cout <<d._year << d._mouth <<d._day;
	return _cout;
}
void test() {
	date d1(2019, 9, 22);
	date d2(2019, 9, 21);
	date d3(2019, 9, 23);
	date d4 = d3 + 5;
	d2--;
	d1++;
	d4 = d4 + 30;
	cout << d4 - d1 << endl;
	cout << d3 << endl;
	cout << d2 << endl;
	cout << d1 << endl;
	cout << d4 << endl;
}
int main(){
	test();
	system("pause");
}

三.样例检测

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值