简易日期类

【任务要求】

    1.实现:日期 + 天数 = 日期;

    2.实现:日期 - 天数 = 日期;

    3.实现:日期 - 日期 = 天数;


【代码实现】

#include <iostream>
using namespace std;

class Date
{
	friend ostream& operator<<(ostream& os ,const Date& d);//友元
public:
	Date(int year = 1900, int month = 1, int day = 1)//构造函数
		:_year(year)
		,_month(month)
		,_day(day)
	{
		if (IsInvalidDate()) //处理无效日期
		{
			_year = 1900;
			_month = 1;
			_day = 1;
		}
	}
	Date(const Date& d)//拷贝构造函数
	{
		_year = d._year;
		_month = d._month;
		_day = d._day;
	}
	Date& operator=(const Date& d) //赋值运算符重载
	{
		if(this != &d)
		{
			this->_year = d._year;
			this->_month = d._month;
			this->_day = d._day;
		}
		return *this;
	}
public:
	/*判断日期是否有效*/
	bool IsInvalidDate()
	{
		if ((_year<1900)
			||((_month < 1) || (_month > 12))
			||((_day < 1) || _day > GetMonthDay()))
		{
			return true;
		}
		return false;
	}
	/*判断_year是否为闰年*/
	bool LeapYear()
	{
		return (0 == _year %4 &&0 != _year % 100)||(0 == _year % 400);
	}
	/*得到_month有多少天*/
	int GetMonthDay()
	{
		int Days[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
		if(LeapYear())
		{
			Days[2] += 1;
		}
		return Days[_month];
	}
	/*判断两个日期是否相等*/
	bool operator==(const Date& d)
    {
		return (this->_year == d._year) && (this->_month == d._month) && (this->_day == d._day);
    }
	bool operator!=(const Date& d)
    {
		return !(*this == d);
    }
	/*判断*this与d的大小关系*/
	bool operator>(const Date& d)
    {
		if(_year > d._year)
		{
			return true;
		}
		else if(_year == d._year)
		{
			if(_month > d._month)
			{
				return true;
			}
			else if(_month == d._month)
			{
				if(_day > d._day)
				{
					return true;
				}
			}
		}
		return false;
    }
	bool operator<(const Date& d)
	{
		return !(*this > d);
	}
	bool operator>=(const Date& d)
	{
		return ((*this > d)||(*this == d));
	}
	bool operator<=(const Date& d)
	{
		return ((*this < d)||(*this == d));
	}
public:
	void GetNowDate()//得到新的日期
	{
		while(_day <= 0)
		{
			if (_month == 1)
			{
				_month = 12;
				_year -= 1;	 
			}
			else 
			{
				_month -= 1;
			}
			_day += GetMonthDay();
		}
		while(_day > GetMonthDay())
		{
			_day -= GetMonthDay(); 
			if(_month == 12)
			{
				_month = 1;
				_year += 1;
			}
			else
			{
				_month += 1;
			} 
		}
	}
	
	Date operator+(int day)	//日期 + 天数 =日期
	{
		int Days[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
		Date d(*this);
		d._day += day;
		d.GetNowDate();
		return d;
	}
	Date operator-(int day)	//日期 - 天数 =日期
	{
		int Days[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
		Date d(*this);
		d._day -= day;
		d.GetNowDate();
		return d;
	}
	Date& operator+=(int day)
	{
		_day += day;
		this->GetNowDate();
		return *this;
	}
	Date& operator-=(int day)
	{
		_day -= day;
		this->GetNowDate();
		return *this;
	}
	int operator-(const Date& d) //日期-日期 =天数
	{
		int day = 0;
		if(*this > d)
		{
			Date d1 = *this;
			*this = d;
		}
		while(*this != d)
		{
			*this += 1;
			day++;
		}
		return day;
	}
private:
	int _year;
	int _month;
	int _day;
};

ostream& operator<<(ostream& os ,const Date& d)  //输出运算符重载   
{
	os << d._year << "-" << d._month << "-" <<d._day << endl;
    return os;
}

int main()
{				  
	Date d(2016, 3, 2);
	Date d1(1995, 7, 14);
	int ret = d-d1;
	cout << "By the end of "
	     << d <<"You have already lived for "
	     << ret << " days" << endl;
	system("pause");
	return 0;
}

结果:

wKiom1bWwyniBmg_AAATelSQaQc829.png

本文出自 “Pzd流川枫” 博客,请务必保留此出处http://xujiafan.blog.51cto.com/10778767/1746869

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值