日期类的实现

用c++实现日期类。Date.h 的代码如下:

#pragma once

#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;

		assert(IsInvalid());
	}

	void Print()
	{
		cout << this->_year << "-" << _month << "-" << _day << endl;
	}

	// 防御式编程
	bool IsInvalid()
	{
		if (_year >= 1900
			&& _month > 0 && _month < 13
			&& _day > 0 && _day <= GetMonthDay())
		{
			return true;
		}

		return false;
	}

	int GetMonthDay()
	{
		assert(_month > 0 && _month < 13);

		static int monthDays[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

		if (_month == 2 &&
			((_year % 4 == 0 && _year % 100 != 0) || _year % 400 == 0))
		{
			return 29;
		}

		return monthDays[_month];
	}

	// d1 > d2
	bool operator>(const Date& d)
	{
		return (_year > d._year) || (_year == d._year&&_month > d._month) || (_year == d._year&&_month == d._month&&_day > d._day);
	}

	//d1==d2
	bool operator==(const Date& d)
	{
		return _year == d._year
			&& _month == d._month
			&& _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);
	}

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

	// d1 + 10
	Date operator+(int day)
	{
		if (day < 0)      //当day为负数时,复用operator-
		{
			return *this - (-day);
		}

		Date tmp(*this);
		tmp._day += day;
		while (tmp._day > tmp.GetMonthDay())
		{
			tmp._day -= tmp.GetMonthDay();
			tmp._month++;
			if (tmp._month > 12)   //加完月份后大于12月
			{
				tmp._year++;
				tmp._month = 1;
			}
		}
		return tmp;
	}

	//d1-10
	Date operator-(int day)
	{
		if (day < 0)
		{
			return *this + (-day);
		}
		Date tmp(*this);
		tmp._day -= day;
		while (tmp._day <= 0)
		{
			if (tmp._month == 1)
			{
				tmp._year--;
				tmp._month = 12;
			}
			else
			{
				--tmp._month;
			}
			tmp._day += tmp.GetMonthDay();
		}
		return tmp;
	}

	// d1 += 10 -> d1.operator+=(&d1, day)
	Date& operator+=(int day)
	{
		*this = *this + day;    //复用operator+
		return *this;
	}

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

	// --d1 -> d1.operator--(&d1)
	Date& operator--() // 前置
	{
		*this -= 1;     //复用operator-=
		return *this;
	}

	Date operator--(int) //后置
	{
		Date tmp(*this);
		*this -= 1;      //复用operator-=
		return tmp;
	}

	Date& operator++()
	{
		*this += 1;      //复用operator+=
		return *this;
	}

	Date operator++(int)// 后置    
	{
		Date tmp(*this);      //复用operator+=
		*this += 1;
		return tmp;
	}

	// d1 - d2两日期相减
	int operator-(const Date& d)
	{
		int tag = 1;
		Date greate(*this);
		Date less(d);   //将两个日期存起来
		if (*this < d)      //若d1小于d2,交换,让less始终指向小日期
		{
			greate = d;
			less = *this;
			tag = -1;     //标记符号
		}
		int count = 0;
		while (less < greate)
		{
			++count;
			++less;
		}
		return count*tag;
	}

private:
	int _year;
	int _month;
	int _day;
};

void TestDate1()
{
	Date d1(2017, 11, 2);
	Date d2 = d1 + 50000;
	d2.Print();

	/*Date d3 = d1 + -50000;
	d3.Print();*/

	Date d4 = d1 - 50000;
	d4.Print();
	--d4; //d4.operator--(&d4);
	d4.Print();
	d4++; //d4.operator--(&d4, int());
	d4.Print();
}

//void TestDate2()
//{
//	Date d1(2017, 11, 2);
//	Date d2 = d1 - 500;
//	Date d3(2018, 9, 1);
//	cout << "相差的天数" << d1 - d2 << endl;
//	cout << "相差的天数" << d2 - d1 << endl;
//	cout << "相差的天数" << d1 - d3 << endl;
//	cout << "相差的天数" << d3 - d1 << endl;
//}
Date.cpp 的代码如下:
#include"date.h"
int main()
{
	TestDate1();
	//TestDate2();
	system("pause");
	return 0;

}





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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值