c++日期类的实现(基础篇)

基本功能实现:

bool operator>(const Date& d);
operator>=
operator<
operator<=
operator==
operator!= 

Date operator+(int day);
Date operator+=(int day);
Date operator-(int day);
Date operator-=(int day);
Date operator++();
Date operator++(int);
Date operator--();
Date operator--(int); 

#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;
	}
	void Display()
	{
		cout << _year << "-" << _month << "-" << _day << endl;
	}
	bool Isvalue()
	{
		if (_month > 0 && _month<13 &&
			_day<=GetMonthday() && _day>0
			&& _year>0)
			return true;
		else
			return false;
	}
	
	bool operator<(const Date &d2)
	{
		assert(Isvalue());
		if (_year < d2._year)
		{
			return true;
		}
		else if (_month < d2._month)
		{
			return true;
		}
		else if (_day < d2._day)
		{
			return true;
		}
		else
			return false;
	}
	bool operator>(const Date &d2)
	{
		assert(Isvalue());
		if (_year > d2._year)
		{
			return true;
		}
		else if (_month > d2._month)
		{
			return true;
		}
		else if (_day > d2._day)
		{
			return true;
		}
		else
			return false;
	}
	bool operator<=(const Date &d2)
	{
		return !(*this > d2);
	}
	bool operator>=(const Date &d2)
	{
		return !(*this<d2);
	}
	bool operator!=(const Date &d2)
	{
		return ((*this<d2)||(*this>d2));
	}
	bool operator==(const Date &d2)
	{
		return !((*this<d2)||(*this>d2));
	}
	int GetMonthday()
	{
		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;
		else
			return monthDays[_month];
	}
	Date operator+(int day)
	{
		if (day<0)
		{
			return *this - (-day);
		}
		Date tmp(*this);
		tmp._day += day;
		while (tmp._day >tmp.GetMonthday())
		{
			tmp._day -= tmp.GetMonthday();
			tmp._month++;
			if (tmp._month > 12)
			{
				tmp._year++;
				tmp._month = 1;
			}
		}
		return tmp;

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

			tmp._day += tmp.GetMonthday();

		}
		return tmp;

	}
	Date &operator=(const Date &d)
	{
		if ((*this != d))
		{
			_year = d._year;
			_month = d._month;
			_day = d._day;
		}
		return *this;
	}
	Date& operator++()
	{
		*this += 1;
		return *this;
	}
	Date operator++(int)
	{
		Date tmp(*this);
		*this += 1;
		return tmp;
	}
	Date& operator--()
	{
		*this -= 1;
		return *this;
	}
	Date operator--(int)
	{
		Date tmp(*this);
		*this -= 1;
		return tmp;
	}
	Date &operator+=(int day)
	{
		if (day < 0)
		{
			return (*this = *this - day);
		}
		*this = *this + day;
		return *this;
	}
	Date &operator-=(int day)
	{
		if (day < 0)
		{
			return (*this = *this +(-day));
		}
		*this = *this - day;
		return *this;
	}
	int operator-(const Date &d)
	{
		int tag = 1;
		Date big(*this);
		Date less(d);
		if (*this < d)
		{
			big = d;
			less = *this;
			tag = -1;
		}
		int count = 0;
		while (big>less)
		{
			++count;
			less++;
		}
		return count*tag;
	}
		
private:
	int _year;
	int _month;
	int _day;
};

int main()
{
	Date d1(2017, 10, 26);
	Date d2(2017, 2, 14);
	d1.Display();
	d2.Display();
	cout << (d1.operator<(d2)) << endl;
	cout << (d1.operator>(d2)) << endl;
	cout << (d1.operator<=(d2)) << endl;
	cout << (d1.operator>=(d2)) << endl;
	cout << (d1.operator!=(d2)) << endl;
	cout << (d1.operator==(d2)) << endl;
	Date d3 = d1 + 2000;
	d3.Display();
	Date d4 = d1 - 52000;
	d4.Display();
	d1 += 6;
	d1.Display();
	d1 -= 6;
	d1.Display();
	d1++;
	d1.Display();
	++d1;
	d1.Display();
	d1--;
	d1.Display();
	--d1;
	d1.Display();

	d2 = d1 - 100;

	cout<<(d1-d2)<<endl;

}
运行结果:






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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值