日期类的创建和一些基本应用。

#include<iostream>
using namespace std;
class Date
{
public:
	Date(int year = 2017, int month = 9, int day = 10)
		: _year(year)
		, _month(month)
		, _day(day)
	{
                //这里是判断如果输入内容不符合日期的格式,就会默认为1990年的1月1日再输出。
		if (!((_year > 0) && (_month > 0) && (_month < 13) && (day>0) && (day <= GetMonthDays(_month))))
		{
			_year = 1990;
			_month = 1;
			_day = 1;
		}              
	
	} 
        //拷贝构造函数,使用类创建新类。
	Date(const Date& d)
		:_year(d._year)
		,_month(d._month)
		,_day(d._day)
	{}
         //输出符号的重载。
	ostream& operator<<(ostream& _cout)
	{
		_cout << _year << '-' << _month << '-' << _day;
		return _cout;
	}
        //赋值的重载。
	Date& operator=(const Date& d)
	{
		_day = d._day;
		_month = d._month;
		_year = d._year;
		return *this;
	}

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

	//days天之后的日期 
	Date operator+(int days)
	{
		Date temp(*this);
		temp._day += days;     //我们在这里先将天数直接加进去
		int daysInmonth = temp.GetMonthDays(temp._month);    //在这里我们可以得到这个月的总天数
		while (temp._day > daysInmonth)
		{
			temp._day -= daysInmonth;   //我们将这个月的总天数减掉,相当于走过了这些天数,日期就到了下个月的相同天数
			temp._month += 1;   //走过了天数,我们将月份加1
			if (temp._month > 12)       //在这里我们判断的是如果月份在12时天数还大于31时的情况
			{
				temp._month = 1;
				temp._year += 1;
			}
			daysInmonth = temp.GetMonthDays(temp._month);//  由于月份改变,每月的天数我们也要改变
		}
		return temp;
	}
	// days天之前的日期 
	Date operator-(int days)     //这个跟上面步骤相似
	{
		Date temp(*this);
		temp._day -= days;
		int daysInmonth = temp.GetMonthDays(temp._month);
		while (temp._day <= 0)
		{
			temp._month--;
			if (temp._month <= 0)
			{
				temp._year--;
				temp._month = 12;
			}
			temp._day += temp.GetMonthDays(temp._month);
		}
		return temp;
	}

	// 两个日期之间的距离 
	int operator-(const Date& d)
	{
		Date max(*this);
		Date min(d);
		if (min > max)
		{
			Date temp = max;
			max = min;
			min = temp;
		}
		int count = 0;
		{
			while (min < max)
			{
				min = min + 1;
				count += 1;
			}
		}
		return count;
	}
	bool operator==(const Date& d)
	{
		if (_year == d._year&&_month == d._month&&_day == d._day)
		{
			return true;
		}
		return false;
	}
	bool operator!=(const Date& d)
	{
		return !(*this == d);
	}
	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;
		}
		return false;
	}
	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;
		}
		return false;
	}
	bool IsLeapYear()
	{
		if ((_year % 4 == 0) && (_year % 100 != 0) || _year % 400 == 0)
		{
			return true;
		}
		return false;
	}
	int GetMonthDays(int month)
	{
		int days[] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
			if (IsLeapYear() && (2 == month))
			{
				days[month] += 1;
			}
		return days[month];
	}
private:
	int _year;
	int _month;
	int _day;
};
int main()
{
	Date a(2016, 2, 28);  
	/*Date b = a++;*/
	Date c(2015, 2, 28);
	Date b;
	b = a;
	b - 100 << cout;
	cout << endl;
	cout << a - c << endl;
	cout << (b > a);
	getchar();
	return 0;
}
//下图为我的编译器显示结果,

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值