c++万年历

#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
using namespace std;

class Date {
	friend ostream& operator<<(ostream& _cout, const Date& d);
public:    
	//构造
	Date(int year = 1900, int month = 1, int day = 1)
	{
		if (year <= 0 || month <= 0 || month > 12 || day <= 0 || day > GetDay(year, month))
		{
			cout << "非法日期,已设置为默认值:1900-1-1" << endl;
			_year = 1900;
			_month = 1;
			_day = 1;
		}
		_year = year;
		_month = month;
		_day = day;
	}
	//拷贝构造
	Date(const Date& d)
	{
		_year = d._year;
		_month = d._month;
		_day = d._day;
	}
	//获取天数以便后面进位
	int GetDay(int year, int month)
	{
		//有12个月
		static int days[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
		int day = days[month - 1];
		if (month == 2)
		{
			if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
			{
				day++;
			}
		}
		return day;
	}
	Date& operator=(const Date& d)
	{
		if (this != &d)
		{
			_year = d._year;
			_month = d._month;
			_day = d._day;
		}
		return *this;
	}
	Date& operator+=(int days)
	{
		if (days < 0)
			return *this -= -days;
		_day += days;
		//然后判断当前天数是否大于当月天数
		while(_day > GetDay(_year, _month))
		{
			//减去当月天数
			_day -= GetDay(_year, _month);
			//月份进位
			++_month;
			//判断月份是否合理
			if (_month == 13)
			{
				//如果月份超限,年份进位,月份变为一月
				_month = 1;
				++_year;
			}
		}
		return *this;
	}
	Date& operator-=(int days)
	{
		if (days < 0)
			return *this += -days;
		_day -= days;
		while (_day <= 0)
		{
			--_month;
			if (_month <= 0){
				_month = 12;
				--_year;
			}
			_day += GetDay(_year, _month - 1);
		}
		//cout << _year << _month << _day << endl;
		return *this;
	}
	Date operator+(int days)
	{
		Date ret(*this);
		ret += days;
		return ret;
	}
	Date operator-(int days)
	{
		Date ret(*this);
		ret -= days;
		return ret;
	}
	int operator-(const Date& d)
	{
		Date d1(*this);
		Date d2(d);
		int day = 0;
		if (d1 > d2)
		{
			while (d1 > d2)
			{
				--d1;
				++day;
			}
			return day;
		}
		else
		{
			while (d1 < d2)
			{
				++d1;
				++day;
			}
			return -day;
		}
	}
	//++date
	Date& operator++()
	{
		return *this += 1;
	}
	//date++
	Date operator++(int)
	{
		Date ret(*this);
		*this += 1;
		return ret;
	}
	//--date
	Date& operator--()
	{
		return *this -= 1;
	}
	//date--
	Date operator--(int)
	{
		Date ret(*this);
		*this -= 1;
		return ret;
	}
	bool operator>(const Date& d)const
	{
		return _year > d._year || 
			_year == d._year && _month > d._month ||
			_year == d._year && _month == d._month && _day == d._day;
	}
	bool operator>=(const Date& d)const
	{
		return (*this > d) || (*this == d);
	}
	bool operator<(const Date& d)const
	{
		return _year < d._year ||
			_year == d._year && _month < d._month ||
			_year == d._year && _month == d._month && _day < d._day;

	}
	bool operator<=(const Date& d)const
	{
		return (*this < d) || (*this == d);
	}
	bool operator==(const Date& d)const
	{
		return (_year == d._year) 
			&& (_month == d._month) 
			&& (_day == d._day);
	}
	bool operator!=(const Date& d)const
	{
		return !(*this == d);
	}
	/*void operator<<(ostream& _cout)
	{
		_cout << _year << "-" << _month << "-" << _day;
	}*/
private:    
	int _year;    
	int _month;    
	int _day;
};

ostream& operator<<(ostream& _cout, const Date& d)
{
	_cout << d._year << "-" << d._month << "-" << d._day;
	return _cout;
}

int main()
{
	Date d(2018, 9, 9);
	d -= 30;
	cout << d << endl;
	//d.operator<<(cout);
	//d << cout;
	system("pause");
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值