日期类的完善

Data.h 文件

函数的声明

包括日期加、日期减、大于、大于等于、小于、小于等于、判断等于、不等于、以及前置++、--,后置++、--

#include <iostream>
using namespace std;
typedef unsigned int uint;
class Date
{
	int m_year;
	uint m_month;
	uint m_day;
public:
	Date(int y, uint m, uint d) :
		m_year(y),
		m_month(m),
		m_day(d)
	{

	}
	Date operator +(const uint delay) const;
	Date operator -(const uint delay) const;
	bool operator >(const Date & d) const;
	bool operator <(const  Date & d) const;
	bool operator <=(const  Date & d) const;
	bool operator >=(const  Date & d) const;
	bool operator ==(const  Date & d) const;
	bool operator !=(const  Date & d) const;
	Date operator ++();
	Date operator ++(int);
	Date operator --();
	Date operator --(int);

	friend ostream &operator << (ostream & os, Date & d);
	//void showDate();
};

Date.cpp

函数的实现

#include "Date.h"
static uint getMonthDay(int y,uint m)
{
	if (m > 12 || m == 0)
	{
		return 0;
	}
	if (m == 4 || m == 6 || m == 9 || m == 11)
	{
		return 30;
	}
	if (m == 2)
	{
		return 28 + ((y % 400 == 0) || (y % 4 == 0 && y % 100));
	}
	else
	{
		return 31;
	}
}


	Date Date:: operator +(uint delay) const
	{
		Date res = *this;
		uint tmp;
	tmp = getMonthDay(res.m_year, res.m_month);
	while (delay >= tmp )//不能用delay - tmp == 0来作为判断条件,
		                 //因为delay - tmp 的类型为unsigned int;
	{
		delay -= tmp;
		res.m_month++;
		if (res.m_month > 12)
		{
			res.m_month = 1;
			res.m_year++;
		}
		tmp = getMonthDay(res.m_year, res.m_month);
	}
	res.m_day += delay;
	if (res.m_day > tmp)
	{
		res.m_month++;
		if (m_month > 12)
		{
			res.m_month = 1;
			res.m_year++;
		}
		res.m_day -= tmp;
	}
	return res;
}

	Date Date:: operator -(uint delay) const
	{
		Date res = *this;
		uint tmp;
		if (res.m_month == 0)
		{
			res.m_year--;
			res.m_month = 12;
		}
		else
			res.m_month--;
		tmp = getMonthDay(res.m_year, res.m_month);
		while (delay >= tmp)
		{
			delay -= tmp;
			res.m_month--;
			if (res.m_month == 0)
			{
				res.m_month = 12;
				res.m_year--;
			}
			tmp = getMonthDay(res.m_year, res.m_month);
		}
		int tmpday = res.m_day;
		tmpday = res.m_day - delay;
		if (tmpday <= 0)
		{
			tmpday += tmp;
		}
		else
		{
			res.m_month++;
			if (res.m_month > 12)
			{
				res.m_month = 1;
				res.m_year++;
			}
		}
		res.m_day = tmpday;
		return res;
	}



ostream &operator << (ostream & os, Date & d) //输出流
{
	os << d.m_year << '-' << d.m_month << '-' << d.m_day;
	return os;
}

bool Date ::operator <(const Date & d) const
{
	if (m_year < d.m_year)
	{
		return true;
	}
	else if (m_year == d.m_year && m_month < d.m_month)
	{
		return true;
	}
	else if (m_year == d.m_year && m_month == d.m_month && m_day < d.m_day)
	{
		return true;
	}
		return false;
}
bool Date ::operator <=(const  Date & d) const
{
	return !(*this > d);
}
bool Date ::operator >(const  Date & d) const
{
	if (m_year > d.m_year)
	{
		return true;
	}
	else if (m_year == d.m_year && m_month > d.m_month)
	{
		return true;
	}
	else if (m_year == d.m_year && m_month == d.m_month && m_day > d.m_day)
	{
		return true;
	}
		return false;
}

bool Date ::operator >=(const  Date & d) const
{
	return !(*this < d);
}
bool Date ::operator ==(const  Date & d) const
{
	if (m_year == d.m_year &&
		m_month == d.m_month &&
		m_day == d.m_day)
	{
		return true;
	}
	
		return false;
	
}
bool Date ::operator !=(const  Date & d) const
{
	return !(*this == d);
}

Date Date:: operator ++()
{
	int d = getMonthDay(m_year, m_month);
	m_day++;
	if (m_day > d)
	{
		m_day = 1;
		m_month++;
		if (m_month > 12)
		{
			m_month = 1;
			m_year++;
		}
	}
	return *this;
}
Date Date:: operator ++(int)
{
	Date tmp = *this;
	int d = getMonthDay(m_year, m_month);
	m_day++;
	if (m_day > d)
	{
		m_day = 1;
		m_month++;
		if (m_month > 12)
		{
			m_month = 1;
			m_year++;
		}
	}
	return tmp;
}
Date Date::operator --()
{
	m_day--;
	if (m_day < 1)
	{
		m_month--;
		if (m_month < 1)
		{
			m_month = 12;
			m_year--;
		}
		m_day = getMonthDay(m_year, m_month);
	}
	return *this;
}
Date Date::operator --(int)
{
	Date tmp = *this;
	m_day--;
	if (m_day < 1)
	{
		m_month--;
		if (m_month < 1)
		{
			m_month = 12;
			m_year--;
		}
		m_day = getMonthDay(m_year, m_month);
	}
	return tmp;
}

main.cpp

测试函数

日期加减

int main()
{
    Date test(2019, 9, 7);
	Date test1(2059,8,28);
	cout << (test + 14600) << endl;
	cout << (test1 - 14600) << endl;
  
	system("pause");
	return 0;
}

前置++、--,后置++、--

int main()
{
	Date d1(2000, 12, 31);
	Date d2(2012, 3, 1);

	cout << d1.operator++() << endl;
	cout << d1 << endl;
	cout << d2.operator--(0) << endl;
	cout << d2 << endl;
	system("pause");
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值