C++日期类

写了个日期类,大家看看吧。。。
头文件Date.h
#pragma once
class Date
{
	//重载输入输出操作符
	friend ostream& operator<<(ostream& _cout, const Date& d);
	friend istream& operator>>(istream& _cin, Date& d);
public:
	//构造函数
	Date(int year = 2019, int month = 11, int day = 12);
	//拷贝构造函数
	Date(const Date& d);
	//检测是否是润年,是就将2月改为29天
	static void Check_leap_year(const int year);
	//恢复2月为28天
	static void RecoverDate();
	//检测是否是有效日期
	bool Check_Date_valid() const;
	//获取日期(打印),这是在重载输出操作符之前写的呀呀
	void GetDate() const;
	//重载赋值运算符
	Date& operator=(const Date& d);
	//重载日期加天数运算符
	Date operator+(int days);
	//重载日期减天数运算符
	Date operator-(int days);
	//日期减去日期剩余的天数
	int operator-(const Date& d);
	//前置++
	Date& operator++();
	//后置++
	Date operator++(int);
	//前置--
	Date& operator--();
	//后置--
	Date operator--(int);
	//重载>
	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;
private:
	int _year;
	int _month;
	int _day;
	static int arr[];
};
int Date::arr[] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
主要代码
#include"Date.h"
ostream& operator<<(ostream& _cout, const Date& d)
{
	_cout << d._year << "-" << d._month << "-" << d._day;
	return _cout;
}
istream& operator>>(istream& _cin, Date& d)
{
	_cin >>d._year>>d._month>>d._day;
	return cin;
}
	Date::Date(int year, int month, int day)
		:_year(year)
		,_month(month)
		,_day(day)
	{}
	Date::Date(const Date& d)
	{
		_year = d._year;
		_month = d._month;
		_day = d._day;
	}
	void Date::Check_leap_year(const int year)
	{
		if ((year % 400 == 0) || (year % 100 != 0 && year % 4 == 0))
			Date::arr[2] += 1;
	}
	void Date::RecoverDate()
	{
		Date::arr[2] = 28;
	}
	bool Date::Check_Date_valid() const
	{
		Check_leap_year(_year);
		if (_month<=12 && _month >= 1)
		{
			if(_day <=arr[_month] && _day>=1)
				return true;
		}
		return false;
	}
	void Date::GetDate() const
	{
		cout << "Date is:" << _year << "-" << _month << "-" << _day<<endl;
	}
	Date& Date::operator=(const Date& d)
	{
		if (d.Check_Date_valid())
		{
			_year = d._year;
			_month = d._month;
			_day = d._day;
		}
		else
			cout << "输入的不是有效日期!" << endl;
		return *this;
	}
	Date Date::operator+(int days)
	{
		_day += days;
		Check_leap_year(_year);
		//注意加的天数很大
		while(arr[_month] < _day)
		{
			_day -= arr[_month];
			_month++;
			while(_month > 12)
			{
				_month = 1;
				_year++;
				//每年都要检测是否为闰年
				RecoverDate();
				Check_leap_year(_year);
			}
		}
		return *this;
	}
	Date Date::operator-(int days)
	{
		Check_leap_year(_year);
		while(_day < days)
		{
			_month--;
			//如果_mont为0,则要让_month=12月,
			//否则少算12月。
			if (_month == 0)
				_month = 12;
			_day += arr[_month];
			while(_month < 1)
			{
				_month = 12;
				_year--;
				RecoverDate();
				Check_leap_year(_year);
			}
		}
		_day -= days;
		return *this;
	}
	int Date::operator-(const Date& d)
	{
		if (!d.Check_Date_valid() || !Check_Date_valid())
		{
			cout << "有无效日期!" << endl;
			return false;
		}
			Check_leap_year(_year);
			int Sum_day = 0;
			int one_year = 0;
			if (_year > d._year)
			{
				if (_day < d._day)
				{
					_month--;
					if (_month == 0)
						_month = 12;
					_day += arr[_month];
					if (_month < 1)
					{
						_month = 12;
						_year--;
						RecoverDate();
						Check_leap_year(_year);
					}
				}
				_day -= d._day;
				Sum_day += _day;
				if (_month < d._month)
				{
					_month += 12;
					_year--;
					RecoverDate();
					Check_leap_year(_year);
				}
				_month -= d._month;
				for (int i = 0; i < _month + 1; i++)
					Sum_day += arr[i];
				for (int i = d._year; i < _year; i++)
				{
					RecoverDate();
					Check_leap_year(i);
					for (int i = 0; i < 13; i++)
						one_year += arr[i];
					Sum_day += one_year;
					one_year = 0;
				}
				return Sum_day;
			}
			else
				return -1;
	}
	//前置++
	Date& Date::operator++()
	{
		Check_leap_year(_year);
		_day++;
		if (_day > arr[_month])
		{
			_day -= arr[_month];
			_month++;
			if (_month > 12)
			{
				_month = 1;
				_year++;
			}
		}
		return *this;
	}
	//后置++
	Date Date::operator++(int)
	{
		Check_leap_year(_year);
		Date temp(*this);
		_day++;
		if (_day > arr[_month])
		{
			_day -= arr[_month];
			_month++;
			if (_month > 12)
			{
				_month = 1;
				_year++;
			}
		}
		return temp;
	}
	//前置--
	Date& Date::operator--()
	{
		Check_leap_year(_year);
		if (_day - 1 < 1)
		{
			--_month;
			_day += arr[_month];
			if (_month < 1)
			{
				_month = 12;
				_year--;
			}
		}
		--_day;
		return *this;
	}
	//后置--
	Date Date::operator--(int)
	{
		Check_leap_year(_year);
		Date temp(*this);
		if (_day - 1 < 1)
		{
			--_month;
			_day += arr[_month];
			if (_month < 1)
			{
				_month = 12;
				_year--;
			}
		}
		--_day;
		return temp;
	}
	bool Date::operator>(const Date& d)const
	{
		if (!d.Check_Date_valid()||!Check_Date_valid())
		{
			cout << "有无效日期!" << endl;
			return false;
		}
			if (_year > d._year)
				return true;
			else if (_year == d._year)
			{
				if (_month > d._month)
					return true;
				else if (_month == d._month)
				{
					if (_day > d._day)
						return true;
					else
						return false;
				}
				else
					return false;
			}
			else
				return false;
		
	}
	bool Date::operator>=(const Date& d)const
	{
		if (!d.Check_Date_valid() || !Check_Date_valid())
		{
			cout << "有无效日期!" << endl;
			return false;
		}
		if (_year > d._year)
			return true;
		else if (_year == d._year)
		{
			if (_month > d._month)
				return true;
			else if (_month == d._month)
			{
				if (_day >= d._day)
					return true;
				else
					return false;
			}
			else
				return false;
		}
		else
			return false;

	}
	bool Date::operator<(const Date& d)const
	{
		if (!d.Check_Date_valid() || !Check_Date_valid())
		{
			cout << "有无效日期!" << endl;
			return false;
		}
		return !(*this>=d);
	}
	bool Date::operator<=(const Date& d)const
	{
		if (!d.Check_Date_valid() || !Check_Date_valid())
		{
			cout << "有无效日期!" << endl;
			return false;
		}
		return !(*this > d);
	}
	bool Date::operator==(const Date& d)const
	{
		if (!d.Check_Date_valid() || !Check_Date_valid())
		{
			cout << "有无效日期!" << endl;
			return false;
		}
		if (_year == d._year&&
			_month == d._month&&
			_day == d._day)
			return true;
		else
			return false;
	}
	bool Date::operator!=(const Date& d)const
	{
		if (!d.Check_Date_valid() || !Check_Date_valid())
		{
			cout << "有无效日期!" << endl;
			return false;
		}
		return !(*this == d);
	}
测试
int main()
{
	Date d1(2019,2,3);
	cout << "d1     :" << d1 << endl;
	Date d2;
	cout << "d2     :" << d2 << endl;
	Date d3(2003, 6, 7);
	cout << "d3     :" << d3 << endl;
	d1 = d1 + 9999;
	cout << "d1+9999:" << d1 << endl;
	d1 = d2;
	cout << "d1 = d2:" << d1 << endl;
	d2=d1++;
	cout << "d2=d1++:" << d2 << endl;
	d2=++d1;
	cout << "d2=++d1:" << d2 << endl;
	d1=d2--;
	cout << "d1=d2--:" << d1 << endl;
	d1=--d2;
	cout << "d1=--d2:" << d1 << endl;
	int day = d1 - d3;
	cout << "d1 - d3:" << day << endl;
	if (d1 > d3)
		cout << "d1>d3" << endl;
	else if(d1==d3)
		cout << "d1=d3" << endl;
	else
		cout << "d1<d3" << endl;
	return 0;
}
结果

在这里插入图片描述
while(1)一起加油啊;

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值