日期类具体代码实现(C++)

//前面几节举例代码的全部编写
#include <iostream>
#include <stdbool.h>
using namespace std;

class Date

{
public:


	
	// 获取某年某月的天数
	int GetMonthDay(int year, int month)
	{
		int day[13] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 29};
		if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
		{
			if (month == 2)
			{
				return day[12];
			}
			else
			{
				return day[month];
			}
		}
		else
		{
			return day[month];
		}
		
	}
	friend ostream& operator<<(ostream& output, Date& a);
	//友元函数


	// 全缺省的构造函数

	Date(int year = 1900, int month = 1, int day = 1)
	{
		_year = year;
		_month = month;
		_day = day;
	}
	// 拷贝构造函数
  // d2(d1)

	Date(const Date& d)
	{
		this->_year = d._year;
		this->_month = d._month;
		this->_day = d._day;
	}



	// 赋值运算符重载

  // d2 = d3 -> d2.operator=(&d2, d3)
	Date & operator=(const Date& d)
	{
		Date a;
		this->_year = d._year;
		this->_month = d._month;
		this-> _day = d._day;
		return *this;
	}



	// 析构函数
	//由于没有开辟空间,所有析构函数可有可无,析构函数会默认自动生成。
	~Date()
	{
		this->_year = 0;
		this->_month = 0;
		this->_day = 0;
	}



	 日期+=天数

	Date& operator+=(Date d)
	{
		this->_year = this->_year + d._year;
		this->_month = this->_month + d._month;
		this->_day = this->_day+d._day;
		return  d;
	}



	 日期+天数

	Date operator+(int day)
	{
		this->_day = this->_day + day;
		if (day > GetMonthDay(this->_year, this->_month))
		{
			this->_month = this->_month + 1;
			this->_day = this->_day - GetMonthDay(this->_year, this->_month);
			if (this->_month > 12)
			{
				this->_year = this->_year + 1;
				this->_month = 0;
			}
		}
		return *this;
	}



	 日期-天数

	Date operator-(int day)
	{
		while (this->_day < day)
		{
			_month = _month - 1;
			_day = _day + GetMonthDay(_year, _month);
		}
			_day = _day - day;
			return *this;
	}



	 日期-=天数

	Date& operator-=(Date d)
	{
		this->_year = this->_year - d._year;
		this->_month = this->_month - d._month;
		this->_day = this->_day - d._day;
		return  d;
	}



	 前置++

	Date& operator++()
	{
		this->_year = this->_year + 1;
		this->_month = this->_month + 1;
		this->_day = this->_day + 1;
	}



	 后置++

	Date operator++(int)
	{
		Date tmp;
		tmp._year = this->_year;
		this->_year++;
		tmp._month = this->_month;
		this->_month++;
		tmp._day = this->_day;
		this->_day++;
		return tmp;
	}



	 后置--


	Date operator--(int)
	{
		this->_year = this->_year;
		this->_year--;
		this->_month = this->_month;
		this->_month--;
		this->_day = this->_day;
		this->_day--;
	}



	 前置--

	Date& operator--()
	{
		this->_year = this->_year - 1;
		this->_month = this->_month - 1;
		this->_day = this->_day - 1;
	}



	 >运算符重载

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



	 ==运算符重载

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



	 >=运算符重载

	bool operator >= (const Date& d)
	{
		return !(*this < d);
	}



	 <运算符重载

	bool operator<(const Date& d)
	{
		if (this->_year == d._year)
		{
			if (this->_month == d._month)
			{
				if (this->_day < d._day)
				{
					return true;
				}
				else
				{
					return false;
				}
			}
			else
			{
				if (this->_month < d._month)
				{
					return true;
				}
				else
				{
					return false;
				}
			}

		}
		else
		{
			if (this->_year < d._year)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
	}



	 <=运算符重载

	bool operator <= (const Date& d)
	{
		return !(*this > d);
	}



	 !=运算符重载

	bool operator != (const Date& d)
	{
		return !(*this == d);
	}



	 日期-日期 返回天数

	int operator-( Date& d)
	{
		int n = 0;
		int data = 0;
		if (_year == d._year)
		{
			if (_month == d._month)
			{
				if (_day == d._day)
				{
					return 0;
				}
				if (_day > d._day)
				{
					data = 1;
				}
				else
				{
					data = -1;
				}
			}
			if (_month > d._month)
			{
				data = 1;
			}
			else if(_month<d._month)
			{
				data = -1;
			}
			
		}
		if (_year > d._year)
		{
			data = 1;
		}
		else if (_year < d._year)
		{
			data = -1;
		}
		while (_day != d._day)
		{

			if (d._day > GetMonthDay(_year, _month))
			{
				d._month = d._month + 1;
				d._day = 0;
			}
			d._day = d._day + 1;
			n = n + 1;
		}
		return n * data;

	}




private:

	int _year;

	int _month;

	int _day;

};

ostream& operator<<(ostream& output, Date& a)
{
	cout << a._year << "年" << a._month << "月" << a._day << "天" << endl;
	return output;
}

int main()
{
	Date d1(2000,2,29);
	Date d4(2,1,2);
	Date d5(2,1,5);
	Date d6(2,1,1);
	//Date d6;
	Date d2(2005,10,29);
	//Date d2(d1);
	//Date d3 = d1;
	//int a = 10;
	//cout << d1<<endl;
	//cout << d2<<endl;
	//cout << d3<<endl;
	//d4 = d1;
	//cout << d4 << endl;
	//int day = d6.GetMonthDay(2000,2);
	GetMonthDay函数中的2000,2对应的形参为year,month并非为_year,_month并不相同d6由于全缺省构造函数初始化但不影响该函数的进行。
	//cout << day << endl;
	//d5+=d5 ;
	//cout << d5 << endl;
	/*int ret = d5 - d4;
	cout << ret << endl;*/
	/*cout << d5 << endl;
	d5 = d5 + 30;
	cout << d5 << endl;
	cout << d6 << endl;
	d6 = d6 - 30;
	cout << d6 << endl;*/
	int day = d5 - d6;
	cout << day << endl;
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值