C++实现日期类

VS2013

#define _CRT_SECURE_NO_WARNINGS 1

#include<assert.h>
#include <iostream>
using namespace std;

//在实现日期之间的运算之前,要先进行日期是否非法的检查
//实现日期间大小的比较
//计算两个日期间相差的天数
//计算一个日期加或减上day天后的日期

class Date
{
public:
	Date(int year,int month,int day)                  //构造函数
	{
		if (year >= 2000 && month > 0 && month<13 && day>0 < GetMonthDay(year, month))          //判断日期是否非法
		{
			_year = year;
			_month = month;
			_day = day;
		}
		else
		{
			cout << "日期非法" << endl;
			assert(false);
		}
	}

	Date(const Date& d)                  //拷贝构造
	{
		_year = d._year;
		_month = d._month;
		_day = d._day;
	}

	void Display()
	{
		cout << _year << "-" << _month << "-" << _day << endl;
	}

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

	//运算符重载
	bool operator==(const Date &d)
	{
		return (_year == d._year) && (_month == d._month) && (_day == d._day);
	}
	bool operator>(const Date &d)
	{
		if (_year > d._year)
		{
			return true;
		}
		else if (_year == d._year&&_month > d._month)
		{
			return true;
		}
		else if (_year == d._year&&_month == d._month&&_day > d._day)
		{
			return true;
		}
		else
			return false;
	}
	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);
	}
	Date operator=(const Date &d)       //赋值运算符重载
	{
		if (this != &d)
		{
			this->_year = d._year;
			this->_month = d._month;
			this->_day = d._day;
		}
		return *this;
	}

private:
	bool IsLeapYear(int year)             //是否闰年
	{
		if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0))
			return true;
		else
			return false;
	}

	int GetMonthDay(int year, int month)           //根据已知年月获取该月的天数
	{
		int monthArray[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

		int day = monthArray[month];

		if (month == 2 && IsLeapYear(year))
		{
			day += 1;
		}

		return day;
	}

public:
	Date operator+(int day)              //给一个日期加day天
	{
		if (day < 0)
		{
			return operator-(-day);
		}
		Date tmp = *this;
		int sumDays = tmp._day + day;
		while (sumDays > GetMonthDay(tmp._year, tmp._month))
		{
			sumDays -= GetMonthDay(tmp._year, tmp._month);
			tmp._month++;
			if (tmp._month > 12)
			{
				tmp._year++;
				tmp._month %= 12;
			}
			else
			{
				tmp._day = sumDays;
			}
         }
		return tmp;
	}
	Date & operator+=(int day)         //加上相应的天数后还要进行赋值
	{
		*this = operator+(day);
		return *this;
	}
	Date operator-(int day)         //给一个日期减去day天
	{
		if (day < 0)
		{
			return operator+(-day);
		}
		Date tmp = *this;
		while (day >= tmp._day)
		{
			day -= tmp._day;
			if (tmp._month == 1)
			{
				tmp._year--;
				tmp._month = 12;
			}
			else
			{
				tmp._month--;
			}
			tmp._day = GetMonthDay(tmp._year, tmp._month);
		}
		tmp._day -= day;
		return tmp;
}
	
	Date & operator-=(int day)         //减去相应的天数后赋值
	{
		*this = operator-(day);
		return *this;
	}
	Date & operator++()                 //日期加1
	{
		++_day;
		if (_day > GetMonthDay(_year, _month))
		{
			_day -= GetMonthDay(_year, _month);
			++_month;
			if (_month > 12)
			{
				++_year;
				_month = 1;
			}
		}
		return *this;
	}
	Date & operator++(int)        //后置++
	{
		Date tmp = *this;
		*this = operator++();
		return tmp;
	}
	Date & operator--()
	{
		if (_day > 1)
		{
			--_day;
		}
		else
		{
			if (_month == 1)
			{
				--_year;
				_month = 12;
				_day = GetMonthDay(_year, _month);
			}
			else
			{
				--_month;
				_day = GetMonthDay(_year, _month);
			}
		}
		return *this;
	}
	Date & operator--(int)
	{
		Date tmp = *this;
		*this = operator--();
		return tmp;
	}

	//计算两个日期相差的天数
	int operator-(Date & d)
	{
		if (_year <d. _year)
		{
			Date tmp =* this;
			*this = d;
			d = tmp;
		}
		else if (_year == d._year&&_month < d._month)
		{
			Date tmp = *this;
			*this = d;
			d = tmp;
		}
		else if (_year ==d. _year&&_month == d._month&&_day < d._day)
		{
			Date tmp = *this;
			*this = d;
			d = tmp;
		}
			Date tmp1(*this);
			Date tmp2(d);
			int ret = 0;
			while (!(tmp2 == tmp1))
			{
				tmp2.operator++();
				ret++;
			}
			return ret;
	}

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

void Test1()
{
	/*Date d1(2016, 1, 15);
	d1.Display();
	Date d2 = d1;
	d2.Display();
	Date d3;
	d3 = d1;
	d2.Display();*/

	Date d1(2016, 1, 15);
	Date d2(2016, 1, 16);
	d1.Display();
	d2.Display();
	//  ==   0
	//bool ret = d1 == d2;
	//cout << ret << endl;
	//  >    1
	//bool ret = d1 >d2;
	//cout << ret << endl;
	//  <     0
	//bool ret = d1 < d2;
	//cout << ret << endl;
	//  >=   1
	//bool ret = d1 >= d2;
	//cout << ret << endl;
	//  <=    0
	//bool ret = d1 <= d2;
	//cout << ret << endl;
	//   =
	 //d1 = d2;
	 //d1.Display();
	 //d2.Display();
}
void Test2()
{
	Date d1(2016, 2,1);
	d1.Display();
	//Date ret = d1+1;
	//ret.Display();
	//Date ret=d1+70;
	//ret.Display();
	//Date ret=d1-25;
	//ret.Display();
	Date ret = d1 += 70;
	ret.Display();
	d1.Display();
	//Date ret = d1 -= 70;
	//ret.Display();
	//d1.Display();
	//d1++;
	//d1.Display();
	//++d1;
	//d1.Display();
	//--d1;
	//d1.Display();
	//d1--;
	//d1.Display();
	Date d2(2016, 2, 29);
	d2.Display();
	int ret = d1 - d2;
	cout << ret << endl;
}
int main()
{
	//Test1();
	Test2();
	system("pause");
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值