C++练习--日期类

#include<iostream>
#include<algorithm>
using namespace std;

class Date
{
public:
	bool isYear(int year)
	{
		if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) return true;
		return false;
	}

	int getMaxDays(int year, int month)//检查对应年的对应月的最大天数
	{
		int days = 0;
		static int months[] = { 31,28,31,30,31,30,31,31,30,31,30,31 };
		days = months[month - 1];
		if (month == 2 && isYear(year) )
		{
			days=29;
		}
		return days;
	}

	Date(int year = 0, int month = 1, int day = 1)//构造函数
	{
		if (year >= 0 && month > 0 && month < 13 && day>0 && day <= getMaxDays(year, month))
		{
			_year = year;
			_month = month;
			_day = day;
		}
		else
		{
			throw "输入非法";
		}
	}
	Date(const Date& d) //拷贝构造函数
	{
		_year = d._year;
		_month = d._month;
		_day = d._day;
	}

	bool operator==(const Date& d)//判断日期相等
	{
		return this->_year == d._year && this->_month == d._month && this->_day == d._day;
	}
	
	bool operator<(const Date& d) //判断日期小于
	{
		if (this->_year != d._year) return this->_year < d._year;
		if (this->_month != d._month) return this->_month < d._month;
		return this->_day < d._day;
	}
	
	bool operator<=(const Date& d) //判断日期小于等于
	{
		return *this < d || *this == d;
	}

	bool operator>(const Date& d)//判断日期大于
	{
		return !( *this <= d );
	}

	bool operator>=(const Date& d)//判断日期大于等于
	{
		return ! ( *this < d);
	}

	bool operator!=(const Date& d)///日期不等于
	{
		return !( *this == d );
	}


	Date operator+(int days)//日期的加天数
	{
		if (days < 0)
		{
			return *this - (-days);
		}
		
		Date res(*this);
		res._day += days;
	
		while (res._day >= getMaxDays(res._year, res._month))
		{
			
			res._day -= getMaxDays(res._year, res._month);
			res._month++;

			if (res._month == 13)
			{
				res._month = 1;
				res._year++;
			}
		}
		return res;
	}
	//++d1
	Date& operator++()
	{
		*this += 1;
		return *this;
	}

	//d1++;
	Date operator++(int)//为了构成函数重载
	{
		Date res = *this;
		*this += 1;
		return res;
	}

	Date operator-(int days)//日期减后是多少天
	{
		Date res = *this;
		if (days < 0)
		{
			return res + (-days);
		}

		while (days)
		{
			int cnt = min(days, res._day);
			res._day -= cnt;
			days -= cnt;
			if (res._day == 0)
			{
				res._month--;
				
				if (res._month == 0) --res._year, res._month = 12;
				
				if (res._year < 0)
				{
					throw "年份为负数";
				}

				res._day = getMaxDays(res._year, res._month);

			}
		}
		return res;
	}

	Date& operator-=(int days)//日期-=
	{
		*this = (*this - days); //调用重载的=
		return *this;
	}

	long long operator-(const Date& d)//两个日期中间差多少天
	{
		Date d1 = *this; Date d2(d); //均为拷贝构造
		//交换大小
		if (d1 < d2)
		{
			swap(d1._year, d2._year); swap(d1._month, d2._month); swap(d1._day, d2._day);
		}
		//方便点的做法是全部转化成天数后再加减
		long long cnt = 0;
		for (int i = 0; i < d1._year; i++)
		{
			if (isYear(i)) cnt += 366;
			else cnt += 365;
		}
		for (int i = 1; i < d1._month; i++)
		{
			cnt += getMaxDays(d1._year, i);
		}
		for (int i = 1; i <= d1._day; i++)
		{
			cnt++;
		}

		for (int i = 0; i < d2._year; i++)
		{
			if (isYear(i)) cnt -= 366;
			else cnt -= 365;
		}
		for (int i = 1; i < d2._month; i++)
		{
			cnt -= getMaxDays(d2._year, i);
		}
		for (int i = 1; i <= d2._day; i++)
		{
			cnt--;
		}

		return cnt;
	}
	//--d;
	Date& operator--()
	{
		*this -= 1;
		return *this;
	}
	Date operator--(int)
	{
		Date tmp(*this);
		*this -= 1;
		return tmp;
	}

	Date& operator+=(int days) //将日期变成+后的天数
	{
		if (days < 0)
		{
			return *this -= -days;
		}
		(*this)._day += days;
		while ( (*this)._day >= getMaxDays( (*this)._year, (*this)._month))
		{

			(*this)._day -= getMaxDays((*this)._year, (*this)._month);
			(*this)._month++;

			if ((*this)._month == 13)
			{
				(*this)._month = 1;
				(*this)._year++;
			}
		}
		return *this;
	}
	
	Date& operator=(const Date& d)
	{
		if (this != &d)
		{
			this->_year = d._year;
			this->_month = d._month;
			this->_day = d._day;
		}
		return *this;
	}


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

	~Date()
	{

	}

private:
	int _year;
	int _month;
	int _day;
};
int main(void)
{
	Date d(2021,9,6);
	cout << d << endl;
	cout << (d + 100) << endl;
	
	cout << (d == (d + 1) )<< endl;

	cout << (d < d + 1) << endl;
	d += 10;
	cout << d << endl;

	Date d2 = d;//拷贝构造
	cout << d2 << endl;

	Date d3(2021, 9, 8);
	cout << d3 << endl;

	d2 = d3;///重载赋值运算符

	cout << d2 << endl;

	//测试日期相减

	cout << d3 << endl;
	cout << d << endl;
	cout << d3 - d << endl;

	Date dq(2020, 2, 5); Date dqq(2021, 9, 8);
	cout << dq - dqq << endl;

	//测试日期减后是什么时候
	cout << d3 - 20 << endl;
	
	d3 -= 20;
	cout << d3 << endl;
	d3 += 20;
	cout << d3 << endl;
	//测试++
	++d3;
	cout << d3 << endl;
	cout << d3++ << endl;
	return 0;
}

猜你喜欢

转载自blog.csdn.net/zstuyyyyccccbbbb/article/details/120177179