C++对日期类的实现

一.实现日期类


  今天算是做一个小练习,尝试以下对日期类的实现
#include<iostream>
using namespace std;

class Date
{
	friend ostream& operator<<(ostream& cout, const Date& d);
public:
	Date(int year = 1990, int month = 1, int day = 1)
	{
		m_year = year;
		m_month = month;
		m_day = day;
	}
	Date(const Date& d)
	{
		m_year = d.m_year;
		m_month = d.m_month;
		m_day = d.m_day;
	}
	~Date()
	{}
//在日期相加之前我们必须要获取每个月的天数才能更准确的得到结果
public:
	bool IsLeap(int year)
	{
		if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
			return true;
		return false;
	}
	//写法一
	/*int GetDayByYearMonth(int year, int month)
	{
		if (month == 1 || month == 3 || month == 5 || month == 7
			|| month == 8 || month == 10 || month == 12)
			return 31;
		else if (month == 2)
		{
			if (IsLeap(year))
				return 29;
			return 28;
		}
		return 30;
	}*/
	//写法二
	//这里定义数组直接返回下标即可
	int GetDayByYearMonth(int year, int month)
	{
		int days[13] = { 29,31,28,31,30,31,30,31,31,30,31,30,31 };
		if (month == 2)
		{
			if (IsLeap(year))
				month = 0;
		}
		return days[month];
	}
//重载日期++
public:
	Date operator+(int n) //日期+n天
	{
		int year = m_day;
		int month = m_month;
		int day = m_day;
		int days = GetDayByYearMonth(year, month);
		while (day + n > days)
		{
			month++;
			if (month > 12)
			{
				year++;
				month = 1;
			}
			n -= days;
			days = GetDayByYearMonth(year, month);
		}
		day += n;
		return Date(year, month, day);
	}

//重载日期-
	Date operator-(int n) //日期-n天
	{
		int year = m_day;
		int month = m_month;
		int day = m_day;
		int days = GetDayByYearMonth(year, month);
		while (day - n > days)
		{
			month--;
			if (month < 12)
			{
				year--;
				month = 1;
			}
			n += days;
			days = GetDayByYearMonth(year, month);
		}
		day -= n;
		return Date(year, month, day);
	}

//重载+=
	//写法一
	/*Date& operator+=(int n)
	{
		int days = GetDayByYearMonth(m_year, m_month);
		while (m_day + n > days)
		{
			m_month++;
			if (m_month > 12)
			{
				m_year++;
				m_month = 1;
			}
			n -= days;
			days = GetDayByYearMonth(m_year, m_month);
		}
		m_day += n;
		return *this;
	}*/
	//写法二
	//这里我们通过调用+的方法进行计算,但是这样的算法效率会比较低,因为调动了多种成员函数方法
	//但是由于这个日期类比较小这样子写也是可以的
	Date& operator+=(int n)
	{
		Date tmp = *this + n;
		*this = tmp;
		return *this;
	}

//重载-=
	Date& operator-=(int n)
	{
		Date tmp = *this - n;
		*this = tmp;
		return *this;
	}

//重载前置++
	Date operator++()
	{
		*this = *this + 1;
		return *this;
	}

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

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

//重载后置--
	Date operator--(int)
	{
		Date tmp = *this;
		--*this;
		return tmp;
	}
public:
//重载日期的大于的关系
	bool operator>(const Date& d)
	{
		if ((m_year > d.m_year)
			|| (m_year == d.m_year && m_month > d.m_month)
			|| (m_year == d.m_year && m_month == d.m_month && m_day > d.m_day))
			return true;
		return false;
	}

//重载日期<=
	bool operator<=(const Date& d)
	{
		return !(*this > d);
	}

//重载日期小于的关系
	bool operator<(const Date& d)
	{
		if ((m_year < d.m_year)
			|| (m_year == d.m_year && m_month < d.m_month)
			|| (m_year == d.m_year && m_month == d.m_month && m_day < d.m_day))
			return true;
		return false;
	}

//重载日期>=
	bool operator>=(const Date& d)
	{
		return !(*this < d);
	}

//重载日期==
	bool operator==(const Date& d)
	{
		return (m_year == d.m_year && m_month == d.m_month && m_day == d.m_day);
	}

//重载日期!=
	bool operator!=(const Date& d)
	{
		return !(*this == d);
	}

//重载两个日期之间的间隔天数
	Date operator-(const Date& d)
	{
		Date maxdate(*this);
		Date mindate(d);
		if (maxdate < mindate)
		{
			maxdate = mindate;
			mindate = *this;
		}
		int count = 0;
		while (mindate < maxdate)
		{
			mindate = mindate + 1;
			++count;
		}
		return count;
	}


private:
	int m_year;
	int m_month;
	int m_day;

};

ostream& operator<<(ostream& out, const Date& d)
{
	out << d.m_year << "年" << d.m_month << "月" << d.m_day << "日";
	return cout;
}

void  main()
{
	Date d1(1900, 2, 28);
	cout << ++d1 << endl;
	cout << --d1 << endl;
	cout << d1 << endl;

	cout << d1-- << endl;
	cout << d1++ << endl;
	cout << d1 << endl;

	Date d2(2017, 5, 1);
	cout << d2 + 100 << endl;
	Date d3(1997, 11, 1);
	cout << d3 - 100 << endl;
}
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值