日期类函数(计算日期)

#include<iostream>
using namespace std;

int runnian(int year)//判断该年是否是闰年
{
	if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0))
		return 1;
	else
		return 0;
}

int riqi(int& month,int& year)//因为每个月的的天数不一样,所以用一个函数进行判断
{
	int day = 0;
	if (month <= 12)
	{
		if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
			day = 31;
		else if (month == 2)
		{
			if (runnian(year) == 1)
				day = 29;
			else
				day = 28;
		}
		else
			day = 30;
	}
	else
	{
		year++;
		day = 1;
		month = 1;
	}
	return day;
}

class Date
{
public:
	Date(int year = 2017, int month = 9, int day = 10)//构造函数  大括号上面的三个代码是用来初始化
		: _year(year)
		, _month(month)
		, _day(day)
	{}

	Date& operator=(const Date& d)//重载=,因为在下文我们会用到将两个类变量进行等号赋值,而系统中的等号
	{
		this->_day = d._day;
		this->_month = d._month;
		this->_year = d._year;
		return *this;
	}

	// 前置++ 
	Date& operator++()
	{
		Date temp;
		temp._day = this->_day;
		temp._month = this->_month;
		temp._year = this->_year;
		this->_day = this->_day + 1;
		this->_month = this->_month + 1;
		this->_year = this->_year + 1;
		return temp;
	}
	// 后置++ 
	Date operator++(int)
	{
		this->_day = this->_day + 1;
		this->_month = this->_month + 1;
		this->_year = this->_year + 1;
		return *this;
	}
	//前置--
	Date& operator--()
	{
		Date temp;
		temp._day = this->_day;
		temp._month = this->_month;
		temp._year = this->_year;
		this->_day = this->_day - 1;
		this->_month = this->_month - 1;
		this->_year = this->_year - 1;
		return temp;
	}
	//后置--
	Date operator--(int)
	{
		this->_day = this->_day - 1;
		this->_month = this->_month - 1;
		this->_year = this->_year - 1;
		return *this;
	}

	//计算days天之后的日期  重载+运算符
	Date operator+(int days)
	{
		Date temp = *this;
		int day = riqi(temp._month, temp._year);
		for (int i = days; i > 0; i--)
		{
			if (temp._day ==day)
			{
				if (temp._month < 12)
					++temp._month;
				else
				{
					++temp._year;
					temp._month = 1;
					temp._day = 1;
				}
				day = riqi(temp._month, temp._year);
				temp._day = 1;
			}
			else
				temp._day++;
		}
		return temp;
	}
	// 计算days天之前的日期 
	Date operator-(int days)
	{
		Date temp = *this;
		int day = riqi(temp._month, temp._year);
		for (int i = days; i > 0; i--)
		{
			if (temp._day == 1)
			{
				if (temp._month > 1)
					--temp._month;
				else
				{
					--temp._year;
					temp._month = 12;
					temp._day = 31;
				}
				day = riqi(temp._month, temp._year);
				temp._day = day;
			}
			else
				temp._day--;
		}
		return temp;
	}

	// 两个日期之间的距离 
	int operator-(const Date& d)
	{
		int count = 0;
		Date temp = *this;
		//int day = riqi(temp._month, temp._year);
		while (1)
		{
			if (temp != d)
			{
				if (temp._day > 1)
					temp._day--;        
				else
				{
					if (temp._month > 1)
					{
						--temp._month;
						temp._day = riqi(temp._month, temp._year);
					}
					else
					{
						--temp._year;
						temp._month = 12;
						temp._day = 31;
					}
					
				}
					
			}
			else
				break;
			count++;
		}
		return count;
	}
	bool operator==(const Date& d)
	{
		if (_year != d._year)

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

			return 1;
		else
		{
			if (_month != d._month)
				return 1;
			else
			{
				if (_day != d._day)
					return 1;
				else
					return 0;
			}
		}
	}
	bool operator>(const Date& d)
	{
		if (_year > d._year)
			return 1;
		else if (_year < d._year)
			return 0;
		else
		{
			if (_month > d._month)
				return 1;
			else if (_month < d._month)
				return 0;
			else
			{
				if (_day > d._day)
					return 1;
				else if (_day < d._day)
					return 0;
				else
					return 0;
			}
		}
	}
	bool operator<(const Date& d)
	{
		if (_year > d._year)
			return 0;
		else if (_year < d._year)
			return 1;
		else
		{
			if (_month > d._month)
				return 0;
			else if (_month < d._month)
				return 1;
			else
			{
				if (_day > d._day)
					return 0;
				else if (_day < d._day)
					return 1;
				else
					return 0;
			}
		}
	}
	~Date()
	{}

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

int main()
{
	Date s1(2017, 1, 10);
	Date s2(2016,1, 10);
	int z = s1 - s2;
	cout << z << endl;
	system("pause");
	return 0;
}
注:上述代码,因为太过冗长所以如果if后面如果只跟一条语句,就会把大括号去掉,但不会影响代码的执行,只是看起来可能会稍有点困难,带来的不便,在此说一声抱歉。
  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值