c++类和对象实现日期的相关操作

介绍 :

要求:创建一个Date的日期类,实现日期的+、-、++、--、判断是否相等或大小的操作

 首先我们需要明确日期具有的特性,年有闰年366天和平年365天。月有大月31天、小月30天和二月29或28天。所以在判断一个日期前需要先判断润平年和月的天数(IsLeapyear()和GetMonthDay())

日期操作代码实现:

 1.Date类的建立:

class Date{
public :
    Date(int year = 1, int month = 1,int day = 1)//Date构造函数
	{
		_year = year;
		_month = month;
		_day = day;
		if (!CheckDate())
		{
			Print();
			cout << "日期不合法" << endl;
		}
	}
    
    bool IsLeapyear(int year){}//判断润平年
    int GetMonthDay(int year,int month){}//求某月的天数
    Date& operator+=(int day){}//获取某个日期加day天后的日期,原日期改变
    Date operator+(int day){}//某个日期+day的日期但原日期不改变
    Date& operator-=(int day){}//获取某个日期减day天后的日期,原日期改变
    Date operator-(int day){}//某个日期-day的日期但原日期不改变
	int operator-(const Date& d){}//日期减某一个日期,原日期不改变
    Date& operator++(){}//日期前置++
    Date operator++(int){}//日期后置++;
    Date operator--(int){}//日期后置--
    Date& operator--(){}//日期前置--
//两个日期大小、相等比较
	bool operator>(const Date& d){}
	bool operator==(const Date& d){}
	bool operator>=(const Date& d){}
	bool operator<(const Date& d){}
	bool operator<=(const Date& d){}
	Date& operator=(const Date& d){}
	bool operator!=(const Date& d){}
    
	bool CheckDate(){}//判断该日期是否合法
	void Print(){}//打印
private:
	int _year;
	int _month;
	int _day;
};

2.IsLeapyear()、GetMonthDay()实现:

闰年:能被4整除且不能被100整除或能被400整除

获取月的天数:可以用数组存放每个月的天数,除了二月需要根据润平年变化,其余月均不变,所以加一个if判断某年2月的天数。

//判断润平年
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)
	{
		static int days[13] = { 31,31,28,31,30,31,30,31,31,30,31,30,31 };
		if (month == 2 && IsLeapyear(year))
		{
			return 29;
		}
		else
			return days[month];			
	}

3.+=、+(日期加天数):

+=:某一个日期加天数后得到一个新日期

+:得到某一个日期加天数后的日期,但原日期不会改变 

        +在+=写好后可利用+=来实现,只要创建一个临时的ret类获取+=某天数后的日期再return ret即可。

	Date& operator+=(int day)
	{
		if (_day < 0)
		{
			return *this -= -day;
		}
		_day += day;
		while (_day > GetMonthDay(_year, _month))
		{
			_day -= GetMonthDay(_year, _month);
			++_month;
			if (_month == 13)
			{
				_month = 1;
				_year++;
			}
		}
		return *this;
	}

	Date operator+(int day)
	{
		Date ret(*this);
		ret+=day;
		return ret;
	}

3.-、-=(日期减天数或日期减日期)

-:可以得到日期减一个天数后的日期,原日期不变(在-=实现后的基础上更容易实现,同+);也可以通过函数重载实现日期减日期,得到天数差。

-=:得到日期减天数后的一个新日期。

Date operator-(int day)//日期减天数
	{
		Date ret = *this;
		ret -= day;
		return ret;
	}
int operator-(const Date& d)//日期减日期
	{
		int flag = 1;
		Date max = *this;
		Date min = d;
		if ((*this) < d)
		{
			max = d;
			min = *this;
			flag = -1;
		}
		int n = 0;
		while (min != max)
		{
			++min;
			++n;
		}
		return n;
	}

	// 日期-=天数
Date& operator-=(int day)
	{
		if (day < 0)
			return *this += -day;
		_day -= day;
		while (_day <= 0)
		{
			_month--;
			if (_month <= 0)
			{
				--_year;
				_month = 12;
			}
			_day += GetMonthDay(_year, _month);
		}
		return *this;
	}

4.前、后置++:

前置++:返回的是++后的值,可以复用+=函数,逻辑是一样的。

后置++:返回的是++前的值,不能传引用返回,为了和前置++赋值重载函数构成函数重载,需要加个int ,不需要传实参,因为没用,如果不加参数那么前置++和后置++函数就一样了,们在进行前置++或者后置++操作时,编译器就不知道调用哪一个了

//前置++
	Date& operator++()
	{
		return *this += 1;
	}
	// 后置++
	Date operator++(int)
	{
		Date ret(*this);
				*this += 1;
				return ret;
	}

5.前、后置--:

原理和前后置++一样

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

6.日期的比较:

日期的比较有>、<、==、>= 、<=、!=,几类基本比较,但其实只要实现>或<一种和==两种比较符,就可以在此基础上实现其他的比较符。

 >,==的实现:

//>运算符重载
bool operator>(const Date& d)
	{
		if ((_year > d._year)
			|| (_year == d._year && _month > d._month)
			|| (_year == d._year && _month == d._month && _day > d._day))
			return true;
		else
			return false;
	}
	// ==运算符重载
bool operator==(const Date& d) {
	return _month == d._month && _year == d._year && _day == d._day;
}

在实现了>和==后,可利用这两种比较实现其他比较符:

   //>=
     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) || *this == d;
	}
	bool operator!=(const Date& d) {
		return !((*this) == d);
	}

7.CheckDate()、Print()(判断日期非法性和打印)

	bool CheckDate()
	{
		if (_year >= 1
			&& _month > 0 && _month < 13 && _day>0 && _day <= GetMonthDay(_year, _month))
		{
			return true;
		}
		else
			return false;

	}

	void Print()
	{
		cout << _year << "/" << _month << "/" << _day << endl;
	}


如上代码的实现算法应该都是很简单的,所以没有写注释,如果有什么没看懂的点欢迎提问!!!

总代码:

#include<stdio.h>
#include<iostream>

using namespace std;

class Date
{
public:
	Date(int year = 1, int month = 1,int day = 1)
	{
		_year = year;
		_month = month;
		_day = day;
		if (!CheckDate())
		{
			Print();
			cout << "日期不合法" << endl;
		}
	}
	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)
	{
		static int days[13] = { 31,31,28,31,30,31,30,31,31,30,31,30,31 };
		if (month == 2 && IsLeapyear(year))
		{
			return 29;
		}
		else
			return days[month];			
	}

	Date& operator+=(int day)
	{
		if (_day < 0)
		{
			return *this -= -day;
		}
		_day += day;
		while (_day > GetMonthDay(_year, _month))
		{
			_day -= GetMonthDay(_year, _month);
			++_month;
			if (_month == 13)
			{
				_month = 1;
				_year++;
			}
		}
		return *this;
	}
	// 日期+天数
	Date operator+(int day)
	{
		Date ret(*this);
		ret._day += day;
		while (ret._day > GetMonthDay(ret._year, ret._month))
		{
			ret._day -= GetMonthDay(ret._year, ret._month);
			++ret._month;
			if (ret._month == 13)
			{
				ret._month = 1;
				ret._year++;
			}
		}
		return ret;
	}
	// 日期-天数
	Date operator-(int day)
	{
		Date ret = *this;
		ret -= day;
		return ret;
	}
	//日期减日期
	int operator-(const Date& d)
	{
		int flag = 1;
		Date max = *this;
		Date min = d;
		if ((*this) < d)
		{
			max = d;
			min = *this;
			flag = -1;
		}
		int n = 0;
		while (min != max)
		{
			++min;
			++n;
		}
		return n;
	}

	// 日期-=天数
	Date& operator-=(int day)
	{
		if (day < 0)
			return *this += -day;
		_day -= day;
		while (_day <= 0)
		{
			_month--;
			if (_month <= 0)
			{
				--_year;
				_month = 12;
			}
			_day += GetMonthDay(_year, _month);
		}
		return *this;
	}
	// 前置++
	Date& operator++()
	{
		return *this += 1;
	}
	// 后置++
	Date operator++(int)
	{
		Date ret(*this);
				*this += 1;
				return ret;
	}
	// 后置--
	Date operator--(int)
	{
		Date ret(*this);
		*this -= 1;
		return ret;
	}
	// 前置--
	Date& operator--()
	{
		return *this-=1;
	}
	// >运算符重载
	bool operator>(const Date& d)
	{
		if ((_year > d._year)
			|| (_year == d._year && _month > d._month)
			|| (_year == d._year && _month == d._month && _day > d._day))
			return true;
		else
			return false;
	}
	// ==运算符重载
	bool operator==(const Date& d) {
		return _month == d._month && _year == d._year && _day == d._day;

	}
	//>=
	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) || *this == d;
	}

	//检查日期合法性
	bool CheckDate()
	{
		if (_year >= 1
			&& _month > 0 && _month < 13 && _day>0 && _day <= GetMonthDay(_year, _month))
		{
			return true;
		}
		else
			return false;

	}

	void Print()
	{
		cout << _year << "/" << _month << "/" << _day << endl;
	}
private:
	int _year;
	int _month;
	int _day;
};

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值