C++ _Date类

 

1.

Date类功能实现

#include <iostream>
#include <cstdbool>
using namespace std;

class Date
{
public:
	Date(int year = 2000, int month = 1, int day = 1); //构造函数
	Date(const Date&d); //拷贝构造函数
	int GetDay(int month, int year); //某月份的天数
	Date& operator = (const Date& d);//赋值运算符重载
	Date& operator += (int days);
	Date& operator -= (int days);
	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;
	void DisPlay();
private:
	int _year;
	int _month;
	int _day;
};

int Date::GetDay(int month, int year)
{
	static int days[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
	int day = days[month - 1];
	if (month == 2)
	{
		if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
		{
			++day;
		}
	}
	return day;
}

Date::Date(int year, int month, int day)
{
	if (year <= 0 || month <= 0 || month > 12 || day <= 0 || day > GetDay(month, year))
	{
		cout << "输入不合法,已设置为默认值: 2000-1-1" << endl;
		_year = 2000;
		_month = 1;
		_day = 1;
	}
	_year = year;
	_month = month;
	_day = day;
}

Date::Date(const Date&d)
{
	_year = d._year;
	_month = d._month;
	_day = d._day;
}

Date& Date::operator = (const Date& d)
{
	if (this != &d)
	{
		_year = d._year;
		_month = d._month;
		_day = d._day;
	}
	return *this;
}

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

Date& Date::operator -= (int days)
{
	if (days<0)
	{
		return *this += -days;
	}
	_day -= days;
	while (_day <= 0)
	{
		--_month;
		if (_month == 0)
		{
			_month = 12;
			--_year;
		}
		_day += GetDay(_month, _year);
	}
	return *this;
}

Date Date::operator + (int days)
{
	Date d(*this);
	d += days;
	return d;
}


Date Date::operator - (int days)
{
	Date d(*this);
	d -= days;
	return d;
}

int Date::operator - (const Date& d)
{
	int count = 0;
	Date D(*this);
	if (D< d)
	{
		while (D < d)
		{
			++D;
			count++;
		}
		return count;
	}
	else
	{
		while (D > d)
		{
			D--;
			count++;
		}
		return -count;
	}
}

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

//后置++
Date Date:: operator ++ (int)
{
	Date  d(*this);
	++*this;
	return d;
}

Date& Date::operator -- ()
{
	return *this -= 1;
}

Date Date::operator -- (int)
{
	Date  d(*this);
	--*this;
	return d;
}

bool Date::operator > (const Date& d)const
{
	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;
}

bool Date::operator >= (const Date& d)const
{
	return (*this > d) || (*this == d);
}

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

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

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

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

void Date::DisPlay()
{
	cout << _year << "-" << _month << "-" << _day << endl;
}

2.

测试函数

#include <iostream>
#include <cstdlib>
#include "Date.h"
using namespace std;
void test()
{
	Date d1(2018, 5, 5);
	Date d2(2019, 3, 14);
	Date d3(d1);
	d3.DisPlay();

	d1 += 60;
	cout << "d1(2018, 5, 5) += 60: " << endl;
	d1.DisPlay();

	d2 -= 364;
	cout << "d2(2019, 3, 14) -= 364:" << endl;
	d2.DisPlay();

	cout << "(d1(2018, 5, 5) += 60)  -  (d2(2019, 3, 14) -= 364)" << endl;
	cout << (d1 - d2) << endl;

	cout << (d1 > d2) << endl;
	cout << (d1 >= d2) << endl;
	cout << (d1 != d2) << endl;
	cout << (d1 == d2) << endl;
	cout << (d1 < d2) << endl;
	cout << (d1 <= d2) << endl;

	d2 = d1;
	d2.DisPlay();

}

int main()
{
	test();
	system("pause");
	return 0;
}

3.

运行结果

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值