c++实现日期类的运算符重载

发布于:2024-07-24 ⋅ 阅读:(148) ⋅ 点赞:(0)

先写一个.h文件来定义类并声明函数

#pragma once
#include<iostream>
#include<stdbool.h>


class Date
{
public:
	// 获取某年某月的天数
	int GetMonthDay(int year, int month);
	// 全缺省的构造函数
	Date(int year = 1900, int month = 1, int day = 1);
	// 日期+=天数
	Date& operator+=(int day);
	// 日期+天数
	Date operator+(int day);
	// 日期-天数
	Date operator-(int day);
	// 日期-=天数
	Date& operator-=(int day);
	// 前置++
	Date& operator++();
	// 后置++
	Date operator++(int);
	// 后置--
	Date operator--(int);
	// 前置--
	Date& operator--();
	// >运算符重载
	bool operator>(const Date& d);
	// ==运算符重载
	bool operator==(const Date& d);
	// >=运算符重载
	bool operator >= (const Date& d);
	// <运算符重载
	bool operator < (const Date& d);
	// <=运算符重载
	bool operator <= (const Date& d);
	// !=运算符重载
	bool operator != (const Date& d);
	// 日期-日期 返回天数
	int operator-(const Date& d);

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

再在.cpp文件内写完所有函数

#include"date.h"

int Date::GetMonthDay(int year, int month)
{
	static int monthDateArr[13] = { -1,31,30,31,30,31,30,31,31,30,31,30,31 };
	int day=monthDateArr[month];
	if((month==2)&&(year%400==0||year%4==0&&year%100==0))
	{
		day += 1;
	}
	return day;
}

Date::Date(int year, int month, int day)
{
	_year = year;
	_day = day;
	_month = month;
}

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

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

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

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

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

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

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

Date& Date::operator--()
{
	_day -= 1;
	while (_day < 0)
	{
		_month -= 1;
		if (_month < 0)
		{
			_year -= 1;
			_month = 12;
		}
		_day += GetMonthDay(_year, _month);
	}
	return *this;
}
bool Date::operator>(const Date& d)
{
	if (_year > d._year)
		return true;
	else if (_year < d._year)
		return false;
	else
	{
		if (_month > d._month)
			return true;
		else if (_month < d._month)
			return false;
		else
		{
			if (_day > d._day)
				return true;
			else
				return false;
		}
	}
}

bool Date::operator==(const Date& d)
{
	return _year == d._year && _month == d._month && _day == d._day;
}

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

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

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

bool Date::operator!=(const Date& d)
{
	return _year != d._year || _month != d._month || _day != d._day;
}

int Date::operator-(const Date& d)
{
	Date tmp = d;
	int day = 0;
	while(*this!=tmp)
	{
		if(*this>tmp)
		{
			tmp += 1;
		}
		else
		{
			tmp -= 1;
		}
		day++;
	}
	return day;
}


网站公告

今日签到

点亮在社区的每一天
去签到