日期类的简单实现

                                     日期类的简单实现
1、对于一些运算符的重载
      以下是我们建立一个日期类及最基本的函数声明。
#include<iostream>
using namespace std;

class Date
{
public:
	Date(int year = 2010, int month = 1, int day = 1)
		: _year(year)
		, _month(month)
		, _day(day)
	{}

	Date& operator=(const Date& date);

	Date& operator++();
	Date operator++(int);

	Date& operator--();
	Date operator--(int);


	//days天之后的日期 
	Date operator+(int days);
	// days天之前的日期 
	Date operator-(int days);

	// 两个日期之间的距离 
	int operator-(const Date& date);

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

	int IsLeapyear(int year);                //检查是否是闰年
	int MonthDays(int year, int month);       //得到月的天数
	void display();                          //对日期的打印
private:
	int _year;
	int _month;
	int _day;
};

      我们先来考虑简单的自增自减,一个日期增一减一无非就是成员day+1或者-1,这就需要考虑到每个月的天数,月初减一可能会到上个月,月末同理;对于特殊的月份2月我们还需一个判断闰年的函数。
如下:
int  Date::IsLeapyear(int year)                 //检查是否是闰年
{
	if ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0))
	{
		return 1;
	}
	return -1;
}

int Date::MonthDays(int year, int month)       //得到月的天数
{
	int days[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
	if (IsLeapyear(year))
	{
		if (month == 2)
			return 29;
		else
			return days[month - 1];
	}
	return days[month - 1];
}
Date& Date:: operator++()                          //前置++
{
	*this = *this + 1;
	return *this;
}

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


Date& Date::operator--()                           //前置--
{
	*this = *this - 1;
	return *this;
}

Date Date::operator--(int)                       //后置--
{
	Date tmp(*this);
	*this = *this - 1;
	return tmp;
}
     接下来考虑两个日期是否相等,以及两个日期的比较。
程序如下:
//判断两个日期是否相等
bool Date::operator==(const Date& date)
{
	if ((_year == date._year) && (_month == date._month) && (_day == date._day))
		return true;
	else
		return false;
}
bool Date::operator!=(const Date& date)
{
	if ((_year != date._year) || (_month != date._month) || (_day != date._day))
		return true;
	else
		return false;
}

bool Date::operator>(const Date& date)
{
	if ((_year > date._year) || ((_year == date._year)&&(_month > date._month)) ||
		((_year == date._year)&&(_month == date._month)&&(_day > date._day)))
		return true;
	else
		return false;
}

bool Date::operator<(const Date& date)
{
	if ((_year < date._year) || ((_year == date._year) && (_month < date._month)) ||
		((_year == date._year) && (_month == date._month) && (_day < date._day)))
		return true;
	else
		return false;
}

最后来考虑比较难的部分,比如2016年5月20,30天后的日期怎么算呢?我们可以这样考虑,5.20,30天后就是5.50,减去5月的天数,就是6月19日;我们据此设一个循环,直到剩下的天数小于下月的总天数。
days天前的天数同理。
对于 重载+时,若传的参数day为负数,就相当于day天前的日期,如下文,可以直接用'-',因为我们文中会重载它;
同理
Date Date::operator+(int day)                    //day天后的日期
{
	Date tmp(*this);
	if (day < 0)                                 //防止出现day是负数的操作
	{
		day = -day;           
		return tmp - day;                       
	}
	else
	{
		tmp._day += day;
		while (tmp._day>MonthDays(tmp._year, tmp._month))
		{
			tmp._day -= MonthDays(tmp._year, tmp._month);
			if (tmp._month == 12)
			{
				tmp._year++;
				tmp._month = 1;
			}
			else
			{
				tmp._month++;
			}
		}
		return tmp;
	}
}

Date Date:: operator-(int day)                     //day天前的日期
{
	Date tmp(*this);
	if (day < 0)
	{
		day = -day;
		return tmp + day;
	}
	else
	{
		tmp._day -= day;
		while (tmp._day <= 0)
		{
			if (tmp._month == 1)
			{
				tmp._year--;
				tmp._month = 12;
			}
			else
			{
				tmp._month--;
			}
			tmp._day = tmp._day + MonthDays(tmp._day, tmp._month);
		}
		return tmp;
	}
}
对于两个日期相差天数的问题,文中已重载好‘+’等运算符,我们可以直接利用SmaDate+days<BigDate来计算。
int Date::operator-(const Date& date)                      //两个日期之间的天数
{
	int days = 0;
	Date BigDate(*this);
	Date SmaDate(date);
	if (BigDate<SmaDate)
	{
		BigDate = date;
		SmaDate = *this;
	}
	while ((SmaDate + days) < BigDate)
	{
		days++;		
	}
	return days;
}

我们可以测试一下:

void Date::display()
{
	cout << _year << "年" << _month << "月" << _day << "日" << endl;
}

void test1()
{
	int days = 0;
	Date d1(2017, 9, 29);
	Date d2(2017, 10, 1);
	Date d3;

	d2++;
	d2.display();
	++d2;
	d2.display();
	
	d2--;
	d2.display();

	d3 = d2+20;
	d3.display();

	d3 = d3-20;
	d3.display();

	days = d2 - d1;
	cout << days;
}

int main()
{
	test1();

	system("pause");
	return 0;
}



还可以扩展,比如,加入星期,像日历一样打印出来,翻页等等,读者可以自己尝试。本文会不断更新继续完善,如有不足请多指正。





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值