【C++】日期类

#include <iostream>
#include <assert.h>

using namespace std;

class Date
{
	friend istream& operator>>(istream& is, Date& d);
	friend ostream& operator<<(ostream& os, const Date& d);

public:
	Date(int year = 1900, int month = 1, int day = 1)
		:_year(year)
		,_month(month)
		,_day(day)
	{
		assert(year >1900);
		assert(month > 0 && month < 13);
		assert(day > 0 && day <= GetMonthDay(year, month));
	}

	void Display()
	{
		cout<<_year<<"-"<<_month<<"-"<<_day<<endl;
	}

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

	Date operator-(int day)
	{
		if(day < 0)
		{
			return *this + (-day);
		}
		Date tmp(*this);
		tmp._day -= day;
		while(tmp._day <= 0)
		{
			if(1 == tmp._month)
			{
				tmp._year--;
				tmp._month = 12;
			}
			else
			{
				--tmp._month;
			}
			tmp._day += GetMonthDay(tmp._year, tmp._month);
		}
		return tmp;
	}

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

	Date& operator-=(int day)
	{
		*this = *this - day;
		return *this;
	}

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

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

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

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

	//void _Swap(Date& d)//交换日期
	//{
	//	Date tmp = *this;
	//	*this = d;
	//	d = tmp;
	//}

	int operator-(Date & d)//计算日期差
	{
		//if(_year < d._year)
		//{
		//	_Swap(d);
		//}
		//else if(_year == d._year && _month < d._month)
		//{
		//	_Swap(d);
		//}
		//else if(_year == d._year && _month == d._month && _day <d._day)
		//{
		//	_Swap(d);
		//}
		int flag = 1;
		if(*this < d)
		{
			std::swap(_year, d._year);
			std::swap(_month, d._month);
			std::swap(_day, d._day);
			flag = -1;
		}
		int days = 0;
		while(*this != d)
		{
			++d;
			++days;
		}
		return days*flag;
	}

	bool operator== (const Date& d)
	{
		return _year == d._year && _month == d._month && _day == d._day;
	}

	bool operator!=(const Date& d)
	{
		return !(*this == d);
	}

	bool operator>(const Date& d)
	{
		//bool operator>(const Date& d)
		//{
		//	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;
		//			}
		//		}
		//	}
		//	return false;
		//}
		return (_year > d._year) || (_year == d._year && _month > d._month) || (_year == d._year && _month == d._month && _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);
	}
private:
	int GetMonthDay(int year, int month)
	{
		assert(year >= 1900);
		assert(month > 0 && month <13);

		static int monthArray[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
		int day = monthArray[month];
		if((month == 2) && (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)))
		{
			day += 1;
		}
		return day;
	}
private:
	int _year;
	int _month;
	int _day;
};

istream& operator>>(istream& is, Date& d)//输入运算符重载
{
	is>>d._year>>d._month>>d._day;
	return is;
}

ostream& operator<<(ostream& os, const Date& d)//输出运算符重载
{
	os<<d._year<<"-"<<d._month<<"-"<<d._day;
	return os;
}

void PrintMenu()
{
	cout<<"**********日期计算器************"<<endl;
	cout<<"********** 请选择: ************"<<endl;
	cout<<"****** 1.推算几天后的日期 ******"<<endl;
	cout<<"****** 2.计算日期差 ************"<<endl;
	cout<<"****** 0.退出 ******************"<<endl;
}

void Test()
{
	Date d1(1901, 1, 1);
	Date d2(1901, 1, 1);
	int day = 0;
	PrintMenu();
	while(1)
	{
		int input = 0;
		cin>>input;
		switch(input)
		{
		case 0:
			exit(0);
		case 1:
			cout<<"* 推算几天后的日期 *"<<endl;
			cout<<"请输入日期(年 月 日):";
			cin>>d1;
			cout<<"请输入相差天数(负数则往前计算):";
			cin>>day;
			cout<<"结果:"<<d1 + day<<endl;
			break;
		case 2:
			cout<<"* 计算日期差 *"<<endl;
			cout<<"请输入第一日期:(年 月 日):";
			cin>>d1;
			cout<<"请输入第二日期:(年 月 日):";
			cin>>d2;
			cout<<"相差"<<d1 - d2<<"天"<<endl;
			break;
		default:
			break;
		}
	}
}
int main()
{
	Test();
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值