[C++系列] 24. 日期类实现

你想算的日期我都有


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

class Date
{
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;
	}
	Date& operator=(const Date& d) {
		if (this != &d) {
			_year = d._year;
			_month = d._month;
			_day = d._day;
		}
		return *this;             // 连续赋值
	}

	int getDay(int year, int month) {
		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+=(int day) {
		if (day < 0)
			return *this -= -day;
		_day += day;
		// 判断当前天数是否大于当月天数
		while (_day > getDay(_year, _month)) {
			// 减去当月天数
			_day -= getDay(_year, _month);
			// 月份进位
			++_month;
			// 判断月份是否合理
			if (_month == 13) {
				_month = 1;
				// 年份进位
				++_year;
			}
			cout << _year <<"年" << setw(4) << _month << "月" << setw(4) << _day << "日" << setw(4) << endl;
		}
		cout << _year  << "年" << setw(4) << _month << "月" << setw(4) << _day << "日" << setw(4) << endl;
		return *this;
	}
	Date& operator-=(int day) {
		if (day < 0)
			return *this += -day;
		_day -= day;
		// 日期小于0,非法,更新日期
		while (_day<0) {
			// 回退到上一个月
			--_month;
			// 判断回退之后的月份是否合理
			if (_month == 0) {
				// 如果_month为0,说明是从1月份回退到上一年的12月份
				_month = 12;
				--_year;
			}
			// 用更新之后的日期更新_day
			_day += getDay(_year, _month);
		}
		return *this;
	}
	// 返回引用连续赋值
	// Date& operator=(const Date& d);  
	Date operator+(int days){
		Date ret(*this);
		ret += days;
		return ret;                // 返回新创建的对象,原来的对象的值没有改变
	}
	// d - day    返回天数之差,两个减法操作数不能改变,即不能传引用
	Date operator-(int days) {
		Date ret(*this);           // *this-->d  d本身不能够发生变化
		ret -= days;			   // ret.operator -= (&ret, days)   operator-=(Date* const this, days) ret相当于Date* const this
		return ret;
	}
	int operator-(const Date& d) {
		Date d1(*this);
		Date d2(d);
		// 写法1 
		/*int day = 0;*/
		//if (d1 > d2) {
		//	while (d1 > d2) {
		//		--d1;
		//		++day;
		//	}
		//	return day;
		//}
		//else {
		//	while (d1 < d2) {
		//		++d1;
		//		++day;
		//	}
		//	return -day;
		//}
		// 写法2
		int flag = 1;
		if (d1 < d2) {
			d1 = d2;
			d2 = *this;
			flag = -1;
		}
		while (d2 < d1) {
			++d2;
			++_day;
		}
		return _day * flag;
	}
	// ++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;
	}
	
	// d1 > d2
	bool operator>(const Date& d)const {
		if (this->_year > d._year)
			return true;
		else if (this->_year == d._year) {
			if (this->_month > d._month)
				return true;
			else if (this->_month == d._month) {
				if (this->_day > d._day)
					return true;
			}
		}
		return false;
	}
	bool operator>=(const Date& d)const {
		return (*this > d) || (*this == d);
	}
	bool operator<(const Date& d)const {
		return !(*this >= d);
	}
	bool operator<=(const Date& d)const {
		return (*this < d) || (*this == d);
	}
	bool operator==(const Date& d)const {
		return this->_year == d._year
			&& this->_month == d._month
			&& this->_day == d._day;
	}
	bool operator!=(const Date& d)const {
		return !(*this == d);
	}
	void display() {
		cout << _year << "年" << setw(4) << _month << "月" << setw(4) << _day << "日" << setw(4) << endl;
	}
private:
	int _year;
	int _month;
	int _day;
};



void testDate1() {
	Date d1(2018, 5, 5);
	Date d2(2019, 3, 14);
	cout << "d1(2018, 5, 5) += 60" <<endl;
	d1 += 60;
	d1.display();
	cout << "d2(2019, 3, 14)" << endl;
	d2 -= 364;
	d2.display();
	cout << "d1(2018, 5, 5) += 60 - d2(2019, 3, 14) -= 364" << endl;
	cout << (d1 - d2) << endl;

	cout << (d1 > d2) << endl;    // 1
	cout << (d1 >= d2) << endl;   // 1
	cout << (d1 != d2) << endl;	  // 1
	cout << (d1 == d2) << endl;   // 0
	cout << (d1 < d2) << endl;    // 0
	cout << (d1 <= d2) << endl;   // 0
}

int main() {
	testDate1();
	system("pause");
}

 较难点已经进行了注释,若是有疑惑的地方,可以底下评论一起讨论解决。或许有些地方还不够完善,希望能够批评指出!

猜你喜欢

转载自blog.csdn.net/yl_puyu/article/details/88991633