日期类的实现

#include<stdio.h>
#include<iostream>
using namespace std;
class Date
{
public:
	Date(int year, int month, int day)//自定义全缺省构造函数
	{
		_year = year;
		_month = month;
		_date = day;
	}
	int getday(int year, int month)//声明一个函数,获得每个月份的天数
	{
		static int days[] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };//前面加个0,提高可读性,这样days[1]就是1月的天数。
		int day = days[month];
		if (month == 2&& year % 4 == 0 && year % 100 != 0 || year % 400 == 0)//若当前月份是2月则判断是否是闰年,
		{
			day++;
		}
		return day;
	}
	//返回相加之后的操作数
	Date& operator+=(int day)
	{
		//判断day是否为负数
		if (day < 0)
		{
			return *this -= -day;//加等一个负数即减等一个正数
		}
		_date += day;//将当前类内日期的值与要加的天数相加
		while (_date > getday(_year, _month))//若当前日期的值大于该月份的天数
		{
			_date -= getday(_year, _month);//则将两者相减的值作为当前日期的值
			_month++;//并将月份加一
			if (_month == 13)//如果月份也越界,则将年份加一,月份则成为新一年的一月份
			{
				_year++;
				_month = 1;
			}
		}//循环,直到当前日期的值小于等于该月份的天数
		return *this;//返回改变后的类
	}
	Date& operator-=(int day)//返回相减之后的操作数
	{
		//判断day是否为负数
		if (day < 0)
		{
			return *this += -day;
		} 
		_date -= day;//将当前日期减去天数
		while (_date <= 0)//如果日期小于等于0,
		{
			_month--;//则将月份减一
			if (_month == 0)//如果月份为0,则退到上一年,并将月份置为12月
			{
				_year--;
				_month = 12;
			}
			_date += getday(_year, _month);//将该月份的天数与负的日期相加,得到相减之后的月份的日期
		}
		return *this;
	}
	//返回相加之后的结果,操作数不改变
	Date operator+(int day)
	{
		Date _d1(*this);//拷贝一个类,相加的操作都对这个类进行,这样原来的操作数不会改变
		_d1 += day;//+=的简写.    _d1.operator+=(day);
		return _d1;//返回拷贝的相加后的类
	}
	Date operator-(int day)
	{
		Date _d1(*this);
		_d1 -= day;
		return _d1;
	}
	//前置++
	Date& operator++()
	{
		return *this += 1;//前置则返回相加后的操作数
	}
	Date& operator--()
	{
		return *this -= 1;
	}
	//后置++
	Date operator++(int)
	{
		Date _d1(*this);//后置则只改变操作数,返回相加之前的操作数
		*this +=1;
		return _d1;
	}
	Date operator--(int)
	{
		Date _d1(*this);
		*this -= 1;
		return _d1;
	}
	//==运算符重载
	bool operator==(const Date& d)
	{
		return _year == d._year&&_month == d._month&&_date == d._date;
	}
	bool operator!=(const Date& d)
	{
		return !(*this == d);
	}
	//>运算符重载
	bool operator>(const Date& d)
	{
		if (_year > d._year)
			return true;
		else if (_year == d._year)
		{
			if (_month > d._month)
				return true;
			else if (_month == d._month)
			{
				if (_date > d._date)
					return true;
			}
		}
		return false;
	}
	bool operator<(const Date& d)//return !(*this >= d);
	{
		if (_year < d._year)
			return true;
		else if (_year == d._year)
		{
			if (_month < d._month)
				return true;
			else if (_month == d._month)
			{
				if (_date < d._date)
					return true;
			}
		}
		return false;
	}
	bool operator<=(const Date& d)// return *this < d || *this == d;
	{
		return !(*this > d);
	}
	bool operator>=(const Date& d)// return *this > d || *this == d;
	{
		return !(*this < d);
	}
	//日期相减
	int operator-(const Date& d)
	{
		Date max = *this;//选出较大的日期
		Date min = d;
		int flag = 1;
		if (max < min)
		{
			max = d;
			min = *this;
			flag = -1;
		}
		int day = 0;//日期相减即计算较小日期需要加多少天才能到较大日期
		while (min < max)
		{
			++min;
			++day;
		}
		return flag * day;//若需要比较的日期大于当前日期,则day为负

	}
private:
	int _year;
	int _month;
	int _date;
};
void test()
{
	Date d1(2021, 5, 10);
	Date d2(2020, 5, 10);
	d1 .operator+=(1);//返回相加之后的操作数,简写:  d1 += 1;
	d2 = d1 + 1;//返回相加之后的结果,不改变操作数
	d2 = ++d1;//前置++
	d2 = d1.operator++(12);//后置++,括号内的数字只是为了区别于前置++,不参与程序运行,简写:  d2 = d1++;
	d2.operator-=(1);//返回相减之后的操作数,简写: d2 -= 1;
	d2 = --d1;
	d2 = d1--;                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   
}
void test1()
{
	Date d1(2021, 5, 11);
	Date d2(2020, 5, 11);
	Date d3(2021, 5, 12);
	Date d4(2021, 5, 11);
	bool ret = d1 < d2;
	ret = d1 <= d4;
	ret = d1 >= d4;
	ret = d3 > d2;
}
void test2()
{
	Date d1(2021, 5, 21);
	Date d2(2021, 5, 22);
	int ret = d1 - d2;
}
int main()
{
	test2();
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值