c++日期类

#include <iostream>
using namespace std;
class Date
{
public:
	Date(int year = 2017, int month = 9, int day = 20)
		: _year(year)
		, _month(month)
		, _day(day)
	{}

	Date& operator=(const Date& d);

	// 前置++ 
	Date& operator++();
	// 后置++ 
	Date operator++(int);
	Date& operator--();
	Date operator--(int);

	//days天之后的日期 
	Date operator+(int days);
	// days天之前的日期 
	Date operator-(int days);

	// 两个日期之间的距离 
	int operator-(const Date& d);
	bool operator==(const Date& d);
	bool operator!=(const Date& d);
	bool operator>(const Date& d);
	bool operator<(const Date& d);
private:
	int _year;
	int _month;
	int _day;
};
Date&  Date::operator=(const Date& d)
{
	if (this != &d)
	{
		this->_year = d._year;
		this->_month = d._month;
		this->_day = d._day;
	}
	return *this;
}
Date& Date::operator++()
{
		if ((this->_month == 1) || (this->_month == 3)
			|| (this->_month == 5) || (this->_month == 7)
			|| (this->_month == 8) || (this->_month == 10)
			|| (this->_month == 12)
			)
		{
			if ((this->_day == 31)&&(this->_month) != 12)
			{
				this->_month += 1;
				this->_day = 1;
			}
			else if ((this->_day == 31) && (this->_month) == 12)
			{
				this->_year += 1;
				this->_month = 1;
				this->_day = 1;
			}
			else
			{
				this->_day += 1;
			}
		}
		else if (this->_month == 2)
		{
			if (((this->_year % 4 == 0) && (this->_year % 100 != 0)) || (this->_year % 400 == 0))
			{
				if (this->_day == 29)
				{
					this->_month += 1;
					this->_day = 1;
				}
				else
				{
					this->_day += 1;
				}
			}
			else
			{
				if (this->_day == 28)
				{
					this->_month += 1;
					this->_day = 1;
				}
				else
				{
					this->_day += 1;
				}
			}
		}
		else
		{
			if (this->_day == 30)
			{
				this->_month += 1;
				this->_day = 1;
			}
			else
			{
				this->_day += 1;
			}
		}
	return *this;
}
Date Date::operator++(int)
{
	Date temp;
	temp = *this;
	if ((this->_month == 1) || (this->_month == 3)
		|| (this->_month == 5) || (this->_month == 7)
		|| (this->_month == 8) || (this->_month == 10)
		|| (this->_month == 12)
		)
	{
		if ((this->_day == 31) && (this->_month) != 12)
		{
			this->_month += 1;
			this->_day = 1;
		}
		else if ((this->_day == 31) && (this->_month) == 12)
		{
			this->_year += 1;
			this->_month = 1;
			this->_day = 1;
		}
		else
		{
			this->_day += 1;
		}
	}
	else if (this->_month == 2)
	{
		if (((this->_year % 4 == 0) && (this->_year % 100 != 0)) || (this->_year % 400 == 0))
		{
			if (this->_day == 29)
			{
				this->_month += 1;
				this->_day = 1;
			}
			else
			{
				this->_day += 1;
			}
		}
		else
		{
			if (this->_day == 28)
			{
				this->_month += 1;
				this->_day = 1;
			}
			else
			{
				this->_day += 1;
			}
		}
	}
	else
	{
		if (this->_day == 30)
		{
			this->_month += 1;
			this->_day = 1;
		}
		else
		{
			this->_day += 1;
		}
	}
	return temp;
}
Date& Date::operator--()
{
	if ((this->_month == 2) || (this->_month == 4)
		|| (this->_month == 6) || (this->_month == 7)
		|| (this->_month == 9) || (this->_month == 11)
		)
	{
		if (this->_day == 1)
		{
			this->_month -= 1;
			this->_day = 31;
		}
		else
		{
			this->_day -= 1;
		}
	}
	else if (this->_month == 3)
	{
		if (((this->_year % 4 == 0) && (this->_year % 100 != 0)) || (this->_year % 400 == 0))
		{
			if (this->_day == 1)
			{
				this->_month -= 1;
				this->_day = 29;
			}
			else
			{
				this->_day -= 1;
			}
		}
		else
		{
			if (this->_day == 1)
			{
				this->_month -= 1;
				this->_day = 28;
			}
			else
			{
				this->_day -= 1;
			}
		}
	}
	else
	{
		if ((this->_month == 1)&& this->_day == 1)
		{
			_year -= 1;
			_month = 12;
			_day = 31;
		}
		else if (this->_day == 1)
		{
			this->_month -= 1;
			this->_day = 30;
		}
		else
		{
			this->_day -= 1;
		}
	}
	return *this;
}
Date Date::operator--(int)
{
	Date temp;
	temp = *this;
	if ((this->_month == 2) || (this->_month == 4)
		|| (this->_month == 6) || (this->_month == 8)
		|| (this->_month == 9) || (this->_month == 11)
		)
	{
		if (this->_day == 1)
		{
			this->_month -= 1;
			this->_day = 31;
		}
		else
		{
			this->_day -= 1;
		}
	}
	else if (this->_month == 3)
	{
		if (((this->_year % 4 == 0) && (this->_year % 100 != 0)) || (this->_year % 400 == 0))
		{
			if (this->_day == 1)
			{
				this->_month -= 1;
				this->_day = 29;
			}
			else
			{
				this->_day -= 1;
			}
		}
		else
		{
			if (this->_day == 1)
			{
				this->_month -= 1;
				this->_day = 28;
			}
			else
			{
				this->_day += 1;
			}
		}
	}
	else
	{
		if (this->_day == 1)
		{
			this->_month -= 1;
			this->_day = 30;
		}
		else
		{
			this->_day -= 1;
		}
	}
	return temp;
}
Date Date::operator+(int days)
{
	Date temp;
	temp = *this;
	while (days--)
	{
		++temp;
	}
	return temp;
}
Date Date::operator-(int days)
{
	Date temp;
	temp = *this;
	while (days--)
	{
		--temp;
	}
	return temp;
}

bool Date::operator==(const Date& d)
{
	if ((this->_year) == (d._year))
	{
		if ((this->_month) == (d._month))
		{
			if ((this->_day) == (d._day))
			{
				return 1;
			}
			else
			{
				return 0;
			}
		}
		else
		{
			return 0;
		}
	}
	else
	{
		return 0;
	}
	
}
int Date::operator-(const Date& d)
{
	int count = 0;
	Date temp = *this;
	if (temp == d)
	{
		return 0;
	}
	else
	{
		while (temp != d)
		{
			if (temp > d)
			{
				--temp;
				count++;
			}
			else
			{
				++temp;
				count++;
			}
		}
		return count;
	}
	
}
bool Date::operator!=(const Date& d)
{
	if (*this == d)
	{
		return 0;
	}
	else
	{
		return 1;
	}

}
bool Date::operator>(const Date& d)
{
	if ((this->_year) > (d._year))
	{
		return 1;
	}
	else if ((this->_year) == (d._year))
	{
		if ((this->_month) > (d._month))
		{
				return 1;
		}
		else if (this->_month == d._month)
		{
			if (this->_day > d._day)
			{
				return 1;
			}
			else
			{
				return 0;
			}
		}
		else
		{
			return 0;
		}

	}
	else
	{
		return 0;
	}
}
bool Date::operator<(const Date& d)
{
	if (*this > d)
	{
		return 0;
	}
	else if (*this == d)
	{
		return 0;
	}
	else
	{
		return 1;
	}
}
int main()
{
	Date ret(2017, 9, 21);
	Date temp(2016,8, 21);
	int x;
	ret++;
	temp = ret + 100;
	x = ret - temp;
	
	printf("%d\n", x);
	system("pause");
	return 0;
}



欢迎各位,批评指正!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值