类和对象(下)— Date类

#include<iostream>
#include<assert.h>
using namespace std;
class Date
{
public:
	Date(int year = 1900, int month = 1, int day = 1)
		:_year(year)
		, _month(month)
		, _day(day)
	{
		if (!IsInvalid()) // this->IsInvalid(this)
		{
			assert(false);
			//cout<<"非法日期"<
			//exit(-1);
		}
	}

	Date(const Date& d) {
		*this = d;
	}

	Date& operator=(const Date& d) {
		_year = d._year;
		_month = d._month;
		_day = d._day;

		return *this;
	}

	~Date() {
		cout << "~Date()" << endl;
	}


	bool IsInvalid() {
		if ((_year >= 1900) && (_year <= 3000)) {
			if (_month >= 1 && _month <= 12) {
				if (_day > 0 && _day <= GetMonthDay(_year, _month)) {
					return true;
				}
			}
		}
		return false;
	}

	static bool isLeapYear(int year) {
		if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
			return true;
		}
		return false;
	}

	static int GetMonthDay(int year, int month) {
		int Day[13] = { 0, 31, 28, 31, 30, 31, 30 ,31, 31, 30, 31, 30, 31 };
		if (month == 2) {
			if (isLeapYear(year)) {
				return 29;
			}
		}
		return Day[month];
	}
	
	void Show() {
		cout << this->_year << "-" << this->_month << "-" << this->_day << endl;
	}

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

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

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

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

	// d1 < d2
	bool operator<(const Date& d) {
		if (*this >= d) {
			return false;
		}
		return true;
	}

	//加法进制转换
	void AddConversion(Date& d) {
		while (_day > GetMonthDay(_year, _month)) {
			if (_month >= 13) {
				_month = 1;
				_year++;
			}
			_day -= GetMonthDay(_year, _month);
			_month++;
		}
	}

	// d1 + 10   
	//用Date返回 因为 d1+10 并不会改变d1的值
	Date operator+(int day) {
		_day += day;
		AddConversion(*this);
		return *this;
	}

	//用Date&返回 因为 d1+=10 会改变d1的值
	Date& operator+=(int day) {
		*this = *this + day;
		return *this;
	}

	//减法进制转换
	void SubConversion(Date& d) {
		while (d._day <= 0) {
			if (d._month == 0) {
				assert(_year > 1900);
				d._year--;
				d._month = 12;
			}
			d._day += GetMonthDay(d._year, d._month);
			d._month--;
		}
	}

	Date operator-(int day) {
		Date tmp = *this;
		tmp._day -= day;
		SubConversion(tmp);
		return tmp;
	}

	Date& operator-=(int day) {
		*this = *this - day;  //*this.operator-(this, day);
		return *this;
	}

	//*this - d = 多少天 ?
	int operator-(const Date& d) {
		//注释掉这一部分利用之前的代码完成,但是每一次循环会创建和销毁一个对象,效率低
		//int ret = 0;
		//while ((*this - ret) > d) {
		//	ret++;
		//}
		//return ret;

		int ret = 0;
		for (int i = d._year; i < this->_year; i++) {
			ret += 365;
			if (isLeapYear(i)) {
				ret += 1;
			}
		}
		
		for (int i = 1; i <= this->_month; i++) {
			ret += GetMonthDay(this->_year, i);	
		}
		ret += this->_day;

		for (int i = 1; i <= d._month; i++) {
			ret -= GetMonthDay(d._year, i);
		}
		ret -= d._day;
		
		return ret;
	}

	//++d1   // 前置
	const Date& operator++() {
		++this->_day;
		AddConversion(*this);
		return *this;
	}

	//d1++    // 后置
	const Date& operator++(int) {
		this->_day++;
		return *this;
	}

	//--d1
	const Date& operator--() {
		--this->_day;
		SubConversion(*this);
		return *this;
	}
	
	//d1++
	Date& operator--(int) {
		this->_day--;
		return *this;
	}
	
private:
	int _year;
	int _month;
	int _day;
};

void Test() {
	//对于输入日期合法性的检查函数的测试
	//Date d1(1899, 1, 1);
	//Date d2(1900, 1, 1);
	//Date d3(3001, 1, 1);
	//Date d4(3000, 1, 1);
	//Date d6(1900, 13, 1);
	//Date d7(1900, 1, 31);
	//Date d8(2008, 2, 29);

	//对于 构造函数的测试  构造 拷贝构造  赋值 Show()
	//Date d9(2018, 7, 20);
	//d9.Show();
	//Date d10(d9);
	//d10.Show();
	//Date d11 = d10;
	//d11.Show();
	//Date d12(2018, 7, 21);

	////Date 对象的比较  ==   !=   >   <   >=  <=
	//cout << (d9 == d10) << endl;    //1
	//cout << (d9 != d10) << endl;    //0
	//cout << (d12 > d11) << endl;   //1
	//cout << (d12 < d11) << endl;   //0
	//cout << (d12 >= d11) << endl;  //1
	//cout << (d12 <= d11) << endl;  //0
	//cout << (d9 >= d10) << endl;    //1
	//cout << (d9 <= d10) << endl;    //1

	//Date d13(2018, 7, 20);
	//d13 = d13 + 365;
	//d13.Show();
	//d13 += 365;
	//d13.Show();
	//d13 -= 365;  //d13.operator-=(&d13, 365);
	//d13.Show();

	//Date d14 = d13;
	//d14 = d13 - 1000;
	//d14.Show();
	//cout << (d13 - d14)<< endl;


	//对  ++   -- 的测试
	Date d15(2018, 7, 21);
	d15.Show();
	--d15;
	d15.Show();
	d15--;
	d15.Show();
	++d15;
	d15.Show();
	d15++;
	d15.Show();

}

int main()
{ 
	Test();

	system("pause");
	return 0;
}

猜你喜欢

转载自blog.csdn.net/luhaowei0066/article/details/81147353