简易的 一个 Date类(日期类)

 Date 日期类

Date类需要包括三个成员变量,年,月,日,注意年月日皆应该使用整形。

对日期 类,需要判断是否为闰年,因此决定2月的天数,并且要使用重载运算符相关的知识用来解决对日期类对象的输入与输出。

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); //<= 
Date operator+(int days); // 加天数 
Date& operator+=(int days);// += 
Date operator-(int days); //减天数 
int operator-(const Date& d); // 两个日期相隔天数 
Date& operator-=(int days); //-= 
Date& operator++( ); //前置++ 
Date operator++(int); //后置++
Date& operator--()//前置-- 
Date operator--(int)//后置--
Date& operator–( ); //前置– 
Date operator–(int); //后置 – 
 

#include<iostream>
using namespace std;

class Date{
public:
	Date()
	{

	}
	//实现对象初始化
	Date(int year, int mouth, int day)
		:_year(year), _mouth(mouth), _day(day)
	{  //判断初始化时,给的日期是否正确,若不正确,则重新赋值
		if (_mouth > 12)
		{
			cout << *this << endl;
			cout << "please check your init,and please repeat your input!:month!" << endl;
			cin >> _year >> _mouth >> _day;
		}
		if (_day > Adiust_mouth())
		{
			cout << *this << endl;
			cout << "please check your init,and please repeat your input!:day!" << endl;
			cin >> _year >> _mouth >> _day;
		}
	}
	//析构函数,释放资源,若无资源,可以不进行显式定义
	~Date()
	{
	}
	//实现拷贝,如 Date d2(d1);
	Date(Date& d)
	{
		_year = d._year;
		_mouth = d._mouth;
		_day = d._day;
	}
	//赋值,Date d2=d1;并且可以实现 d3=d2=d1;
	Date& operator=(const Date& d)
	{
		_year = d._year;
		_mouth = d._mouth;
		_day = d._day;
		return *this;
	}
	//判断是否为瑞年,若为瑞年,返回1,否则返回0
	int Adjust_year()
	{
		if (_year % 4 == 0 || _year % 400 == 0
			|| (_year % 3200 == 0 && _year % 172800 == 0))
			return 1;
		else
			return 0;
	}
	//判断一个月的天数,主要根据是否瑞年,确保2月的天数
	int Adiust_mouth()
	{
		static	int b = 0;
		switch (_mouth)
		{
		case 1:b = 31; break;
		case 2:b = 28; break;
		case 3:b = 31; break;
		case 4:b = 30; break;
		case 5:b = 31; break;
		case 6:b = 30; break;
		case 7:b = 31; break;
		case 8:b = 31; break;
		case 9:b = 30; break;
		case 10:b = 31; break;
		case 11:b = 30; break;
		case 12:b = 31; break;
		}
		if (Adjust_year() == 1 && b == 28)
			b = 29;
		return b;

	}

	//实现现有日期加上days的天数
	Date  operator+(int days)
	{
		Date temp(*this);
		if (_day + days >= Adiust_mouth())
		{
			temp._mouth = _mouth + (_day + days) / 30;
			if (temp._mouth > 12)
			{
				temp._year = _year + temp._mouth / 12;
				temp._mouth = temp._mouth - 12 *( temp._mouth / 12);
			}
			temp._day = _day + days - Adiust_mouth();
		}
		else
		temp._day = _day + days;
		return temp;
	}
	//实现现有日期减去days天数
	Date operator-(int days)
	{
		Date temp(*this);
		if (_day - days < 1)
		{
			temp._mouth = _mouth - 1-(_day - days) / 30;
			if (temp._mouth<1)
			{
				temp._year = _year - 1;
				temp._mouth = 12 * (-temp._mouth )/ 12 + 12 + temp._mouth;
			}
			temp._day = _day + Adiust_mouth() - days;
		}
		else
		temp._day = _day - days;
		return temp;
	}
	//两个日期相差多少天
	int operator-(const Date& d)
	{
		//假设每一年均为365天,每个月均为30天。
		Date dd(1970, 1, 1);
		int a = (d._year - dd._year) * 365 + (d._mouth - dd._mouth) * 30 + (d._day - dd._day);
		int b = (_year - dd._year) * 365 + (_mouth - dd._mouth) * 30 + (_day - dd._day);
		return b - a;

	}
	//前置++
	Date& operator++()
	{
		if (_day == Adiust_mouth())
		{
			if (_mouth == 12)
			{
				_mouth = 1;
				_year = _year + 1;
			}
			else
			_mouth++;
			_day++;

		}
		else
		_day = _day + 1;
		return *this;
	}
	//前置--
	Date& operator--()
	{
		if (_day == 1)
		{
			if (_mouth == 1)
			{
				_mouth = 12;
				_year = _year - 1;
			}
			/*else
			_mouth--;
			_day = 30;*/
		}
		else
		_day = _day - 1;
		return *this;
	}
	//后置++
	Date operator++(int)
	{
		Date temp(*this);
		if (_day<Adiust_mouth())
		_day = _day + 1;
		else
		{
			if (_mouth == 12)
			{
				_mouth = 1;
				_year = _year + 1;
			}
			else
			_mouth = _mouth + 1;
			_day = 1;
		}
		return temp;
	}
	//后置--
	Date operator--(int)
	{
		Date temp(*this);
		if (_day == 1)
		{
			if (_mouth == 1)
			{
				_mouth = 12;
				_year = _year - 1;
			}
			else
			_mouth--;
			_day = 30;
		}
		else
		_day = _day - 1;
		return temp;
	}
	//重载符号'>'
	bool operator>(const Date& d)const
	{
		if (_year > d._year || (_year == d._year && _mouth > d._mouth) 
			|| (_year == d._year && _mouth == d._mouth && _day > d._day))
		{
			return 1;
		}
		else return 0;
	}
	//重载符号'<'
	bool operator<(const Date& d)const
	{
		if (*this > d)
			return 0;
		else
			return 1;
	}
	//重载符号'=='
	bool operator==(const Date& d)const
	{
		if (_year == d._year && _mouth == d._mouth && _day == d._day)
			return 1;
		else
			return 0;
	}
	//重载符号'>='
	bool operator>=(const Date& d)
	{
		if (_year >= d._year || (_year == d._year && _mouth >= d._mouth) 
			|| (_year == d._year && _mouth == d._mouth && _day >= d._day))
		{
			return 1;
		}
		else return 0;
	}
	//重载符号'<='
	bool operator<=(const Date& d)
	{
		if (*this >= d)
			return 0;
		else
			return 1;
	}
	//重载符号'!='
	bool operator!=(const Date& d)
	{
		if (*this == d)
			return 0;
		else
			return 1;
	}
	//重载符号 '<<'输出
	friend ostream& operator<<(ostream& _cout, const Date& d)
	{
		_cout << d._year << "-" << d._mouth << "-" << d._day;
		return _cout;
	}
	//重载符号 '>>'输入
	friend istream& operator>>(istream& _cin, Date& d)
	{
		nn:  _cin >> d._year >> d._mouth >> d._day;
		
		if (d._mouth > 12)
		{
			cout << "please check your input!month!" << endl;
			goto nn;
		}
		if (d._day > d.Adiust_mouth())
		{
			cout << "please check your input! :day!" << endl;
			goto nn;
		}
		return _cin;
	}

private:
	int _year;
	int _mouth;
	int _day;
};


int main()
{
	Date d1(2018, 11, 34);
	cout << d1 << endl;
	Date d2(2018, 13, 20);
	cout << d2 << endl;
	Date t1;
	cin >> t1;
	cout << t1 << endl;
	
	system("pause");
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值