日期类(C++必学)

相信这个大家都见过,当我们学完C++的类和对象后,我们就可以尝试着写一下。

 方法:通过自定义一个日期类可以来实现简易的日期计算器。

 第一步:我们要定义一个日期类,包括了成员函数的声明和成员变量的定义

 第二步:我们要定义成员函数。

 第三步:对成员函数进行测试和优化。

 根据我们要实现的功能: 

 1.可以输入一个合法的日期(若没有输入,则进行默认赋值)        

 2.在日期上加上一定的天数(减少一定的天数)

 3.将两个日期进行比较以及加减

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
class Date

{

public:

	// 获取某年某月的天数

	int GetMonthDay(int year, int month)
	{ //用static来创建变量(将变量存放到静态区),防止多次调用该函数时创建大量栈帧,消耗大
		static int days[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
		int day = days[month];
		//先进行month的判断,因为运算中除法的效率是最低的

		if (month == 2 && ((year % 4 == 0) && year % 100 != 0) || (year % 400) == 0)
		{
			day++;
		}
		return day;
	}


	// 全缺省的构造函数

	Date(int year = 1900, int month = 1, int day = 1)
	{
		if ((year >= 0) && (month > 0 && month <= 12) && (day <= GetMonthDay(year, month)))
		{
			_year = year;

			_month = month;

			_day = day;
		}
		else
		{
			cout << "非法日期" << endl;
		}
	}


	// 拷贝构造函数

  // d2(d1)

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

	}


	// 赋值运算符重载

  // d2 = d3 -> d2.operator=(&d2, d3)

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



	// 析构函数

	~Date()
	{
		;
	}



	// 日期+=天数

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

		}return *this;
	}


	// 日期+天数

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

		}return tmp;

	}



	// 日期-天数

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


	// 日期-=天数

	Date& operator-=(int day)
	{
		if (day<0)
		{
			return *this += -day;

		}
		_day -= day;
		while (_day<=0)
		{
			--_month;
			if (_month == 0)
			{
				--_year;
				_month = 12;
			}
			_day += GetMonthDay(_year, _month);
		}
		return *this;
	}



	// 前置++

	Date& operator++()
	{
		*this = (*this) + 1;
		return *this;

	}


	// 后置++

	Date operator++(int)
	{
		Date tmp(*this);
		*this = *this + 1;
		return tmp;

	}


	// 后置--

	Date operator--(int)
	{
		Date tmp(*this);
		*this = *this - 1;
		return tmp;

	}


	// 前置--

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



	// ==运算符重载

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

	}




	// <运算符重载

	bool operator < (const Date& d)
	{
		if (_year < d._year)
		{
			return true;
		}
		else if(_year==d._year&&_month<d._month)
		{
			return true;
		}
		else if (_year == d._year && _month == d._month&&_day<d._day)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	// >=运算符重载

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



		// >运算符重载

		bool operator>(const Date& d)
		{
			if (_year > d._year)
			{
				return true;
			}
			else if (_year == d._year && _month > d._month)
			{
				return true;
			}
			else if (_year == d._year && _month == d._month && _day > d._day)
			{
				return true;
			}
			else
			{
				return false;
			}
		}



		// <=运算符重载

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



	// !=运算符重载

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


	// 日期-日期 返回天数

		int operator-(const Date& d)
		{
			Date tmp;
			Date max = *this;
			Date min = d;
			int flag = 1;
			if (*this < d)
			{
				tmp = max;
				max = min;;
				min = tmp;
				flag = -1;
			}
			int count = 0;
			while (min != max)
			{
				min++;
				count++;
			}return count * flag;
		}

		void Print()
		{
			cout << "year=" << _year << ":" << "month=" << _month << ":" << "day=" << _day << endl;
		}

private:

	int _year;

	int _month;

	int _day;

};





int main()
{

	Date d1(2021, 7, 14);
	Date d2(d1);
	d1.Print();
	d2.Print();
	Date d3;
	d3 = d2;
	d3.Print();
	//比较运算符重载
	cout << (d1 < d2) << endl;
	cout << (d1 > d2) << endl;
	cout << (d1 == d2) << endl;
	cout << (d1 != d2) << endl;
	cout << (d1 >= d2) << endl;
	cout << (d1 <= d2) << endl;
	//条件运算符重载,看这个运算符是否有意义
	Date d4 = d1 + 30;
	d1.Print();
	d4.Print();
	Date d5 = d1 - 20;
	d5.Print();
	d1.Print();
	Date d6 = d1 += 10;
	d6.Print();
	d1.Print();
	Date d7 = d1 -= 10;
	d7.Print();
	d1.Print();
	Date d8(2020, 7, 15);
	Date d9(2020, 7, 15);
	cout << (d8 - d9) << endl;
	return 0;
	

}


 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值