C++之日期类

#include<iostream>
using namespace std;
//主要函数及其含义
//1.0计算当前日期day天之后日期Date operator+(const Date&d1, int day);
//2.0计算当前日期day天之前日期Date operator-(const Date&d1, int day);
//3.0计算两个日期之间差距int operator-(const Date&d1, const Date& d2);
//4.0重载输出运算符 friend ostream& operator<<(ostream& _cout, const Date& d);
//5.0重载赋值运算符Date& operator=(const Date& d)
class Date
{
public:
	bool IsLeapYear(int year)
	{
		return year % 400 == 0 || (year % 4 == 0 && year % 100 != 0);
	}
	int GetDayInMonth(int year, int month)
	{
		int months[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
		if (IsLeapYear(year))
		{
			months[2] = 29;
		}
		return months[month];
	}
	Date(int year, int month, int day)
		: _year(year)
		, _month(month)
		, _day(day)
	{}
	//拷贝构造函数
	Date(const Date& d)
		: _year(d._year)
		, _month(d._month)
		, _day(d._day)
	{}
	Date& operator=(const Date& d)
	{
		if (this!=&d)
		{
			_year = d._year;
			_month = d._month;
			_day = d._day;
		}
		return *this;
	}
	// 计算当前日期day天之后日期 
	friend Date operator+(const Date&d1, int day);
	// 计算当前日期day天之前日期 
	friend Date operator-(const Date&d1,int day);
	// 计算两个日期之间差距 
	friend int operator-(const Date&d1,const Date& d2);
	//前置++ 返回新值
	Date& operator++()
	{
		_day += 1;
		if (_day > GetDayInMonth(_year, _month))
		{
			_month += 1;
			if (_month > 12)
			{
				_year += 1;
				_month = 1;
			}
			_day = 1;
		}
		return *this;
	}
	// 后置++ 返回旧值
	Date operator++(int)
	{ 
		Date temp = *this;
			_day += 1;
			return temp;
	}
	//前置-- 返回新值
	Date& operator--()
	{
		_day -= 1;
		if (_day==0)
		{
			_month -= 1;
			if (_month == 0)
			{
				_year -= 1;
				_month =12;
				_day = GetDayInMonth(_year - 1, 12);
			}
			else{
				_day = GetDayInMonth(_year, _month);
			}
		}
		return *this;
	}
	// 后置-- 返回旧值
	Date operator--(int)
	{
		Date temp = *this;
		_day -= 1;
		return temp;
	}
    /*一般情况下不会重载成类的成员函数,因为不符合输出习惯 
	void operator<<(ostream& _cout) 
    { 
		 _cout<<_year<<"-"<<_month<<"-"<<_day; 
	}*/
	bool operator>(const Date& d)
	{
		while (_year > d._year || (_year == d._year&&_month > d._month) ||
			(_year == d._year && _month == d._month && _day > d._day))
		{
			return true;
		}
		return false;
	}
	bool operator<(const Date& d)
	{
		while (_year <d._year || (_year == d._year&&_month < d._month) ||
			(_year == d._year && _month == d._month && _day < d._day))
		{
			return true;
		}
		return false;
	}
	bool operator==(const Date& d)
	{
		if (_year == d._year && _month == d._month && _day == d._day)
			return true;
		return false;
	}
	bool operator!=(const Date& d)
	{
		if (_year != d._year || _month != d._month || _day != d._day)
			return true;
		return false;
	}
	// 重载输出运算符 
	friend ostream& operator<<(ostream& _cout, const Date& d);
private:
	int _year;
	int _month;
	int _day;
};
 ostream& operator<<(ostream& _cout, const Date& d)
{
	_cout <<d._year<< "-" << d._month << "-" <<d._day;
	return _cout;
}
 int operator-(const Date&d1,const Date& d2)
 {
	 Date Max = d1;
	 Date Min = d2;
	 int days = 0;
	 if (Max< Min)
	 {
		 Max = d2;
		 Min = d1;
	 }
	 while (1)
	 {
		 Date m = Min + days;
		 if (Max == m)
			 break;
		 else
			 days++;
	 }
	 return days;
 }
 Date operator-(const Date&d1, int day)
 {
	 Date temp = d1;
	 temp._day -= day;
	 while (temp._day<0)
	 {
		 --temp._month;
		 if (temp._month == 0)
		 {
			 temp._month = 12;
			 temp._year--;
		 }
		 temp._day += temp.GetDayInMonth(temp._year, temp._month);
	 }
	 if (temp._day == 0)
	 {
		 temp._month -= 1;
		 if (temp._month == 0)
		 {
			 temp._month = 12;
			 temp._year--;
		 }
		 temp._day = temp.GetDayInMonth(temp._year, temp._month - 1);
	 }
	 return temp;
 }
 Date operator+(const Date&d1,int day)
 {
	 Date temp = d1;
	 temp._day += day;
	 while (temp._day > temp.GetDayInMonth(temp._year, temp._month))
	 {
		 ++temp._month;
		 if (temp._month == 13)
		 {
			 temp._month = 1;
			 ++temp._year;

		 }
		 temp._day -= temp.GetDayInMonth(temp._year, temp._month);
	 }
	 return temp;
 }
 void test()
 {
	 Date d1(2017, 1, 31);
	 Date d2(2017, 1, 1);
	 Date d3(2017, 10, 27);
	 //1.0赋值运算符重载的测试代码
	 d1 = d2;
	 //2.0 << 运算符重载的测试代码
	 //d1<<cout; //这样是可以的但是不符合C++输出习惯cout>> >>endl;而且不能连续输出
	 //3.0 改进
	 cout << d1 << " " << d2 << endl;
	 //4.0前置++和后置++的重载
	 cout << ++d1 << endl;
	 cout << d1++ << endl;
	 //5.0前置--和后置--的重载
	 cout << --d2 << endl;
	 cout << d2-- << endl;
	 //6.0计算当前日期day天之前日期测试代码

	 cout << d3 - 27 << endl;
	 //7.0计算当前日期day天之后日期测试代码	
	 //返回值为bool类型判断两个日期a是否大于b
	 //以及返回值为bool类型判断两个日期a是否小于b
	 //以及返回值为bool类型判断两个日期a是否等于b
	 //以及返回值为bool类型判断两个日期a是否不等于b
	 cout << d3 + 5 << endl;
	 cout << "d1-" << d1 << endl;
	 cout << "d2-" << d2 << endl;
	 cout << "d3-" << d3 << endl;
	 cout << (d1>d2) << endl;
	 cout << (d1<d2) << endl;
	 cout << (d1 == d2) << endl;
	 cout << (d1 != d2) << endl;
 }
 void test1()
 {
	 Date d1(2017, 1, 4);
	 Date d2(2017, 1, 1);
	 //8.0计算两个日期之间差距 
	 cout << d1 - d2 << endl;
 }
int main()
{
	//test();
	test1();
	system("pause");
	return 0;
}



test结果

test1结果


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值