【C++】模拟实现日期类

本篇博客完善了日期类:实现了以下操作

class Date 
{ 
//构造函数
//拷贝构造函数

// 当前日期days天后是什么日期? 
Date operator+(int days); 

// 当前日期days天前是什么日期? 
Date operator-(int days); 

// 两个日期之间差了多少天? 
Date operator-(const Date& d); 

// 日期比大小 
bool operator>(const Date& d); 
bool operator<(const Date& d); 
bool operator==(const Date& d); 
bool operator!=(const Date& d); 
Date& operator=(const Date& d); 

// 重载取地址符号 
Date* operator&(); 

// 前置++ 
Date& operator++(); 

// 后置++ 
Date operator++(int); 

// 前置-- 
Date& operator--(); 

// 后置-- 
Date operator--(int); 
//输出<<的重载,输出日期
private: 
int _year; 
int _month; 
int _day; 
}; 

实现:

class Date
{
public:
	//构造函数
	Date(int year=1900,int month=1,int day=1)
		:_year(year),_month(month),_day(day)
	{
		if (!(_year > 1900 &&
			_month > 0 && _month < 13
			&& _day>0 && _day <= GetDaysOfMonths(year, month)))
		{
			_year = 1900;
			_month = 1;
			_day = 1;
		}
	}

	//也可以不用给
	Date(const Date& d)
		:_year(d._year)
		, _month(d._month)
		, _day(d._day)
	{}

	// 当前日期days天后是什么日期? 
	Date operator+(int days)
	{
		Date tmp(*this);
		//如果加上的是一个负数,相当于减去一个正数
		if (days < 0)
			tmp =tmp-(0 - days);
		//加上的是一个正数
		else
		{
			tmp._day += days;
			int DaysOfMonths = GetDaysOfMonths(tmp._year, tmp._month);
			while (tmp._day > DaysOfMonths)
			{
				tmp._day -= DaysOfMonths;
				tmp._month += 1;
				if (tmp._month > 12)
				{
					tmp._year += 1;
					tmp._month = 1;
				}
				DaysOfMonths = GetDaysOfMonths(tmp._year, tmp._month);
			}
		}
			
		return tmp;
	}

	// 当前日期days天前是什么日期?
	Date operator-(int days)
	{
		Date tmp(*this);
		//减去一个负数,相当于加上一个正数
		if (days < 0)
		{
			tmp = tmp + (0 - days);
		}
		else
		{
			tmp._day -= days;
			while (tmp._day <= 0)
			{
				tmp._month -= 1;
				if (tmp._month <= 0)
				{
					tmp._year -= 1;
					tmp._month = 12;
				}
				int DaysOfMonth = GetDaysOfMonths(tmp._year, tmp._month);
				tmp._day += DaysOfMonth;
			}
		}
		
		return tmp;
	}

	// 两个日期之间差了多少天? 
	int operator-(const Date& d)
	{
		Date MinDate(*this);
		Date MaxDate(d);
		if (MinDate > MaxDate)
		{
			MinDate = d;
			MaxDate = *this;
		}

		int _count = 0;
		while (MinDate != MaxDate)
		{
			_count++;
			MinDate++;
		}
		return _count;
	}

	// 日期比大小 
	//大于
	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 operator==(const Date& d)
	{
		if ((_year == d._year)&&(_month == d._month)&&(_day == d._day))
			return true;
		return false;
	}
	//不等于
	bool operator!=(const Date& d)
	{
		if (!(*this == d))
			return true;
		return false;
	}

	//实现连续赋值
	Date& operator=(const Date& d)
	{
		//排除自己给自己赋值,如果两个地址不一样才进行赋值
		if (this != &d)
		{
			_year = d._year;
			_month = d._month;
			_day = d._day;
		}
		return *this;
	}

	// 重载取地址符号 
	Date* operator&()
	{
		return this;
	}

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

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

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

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

	//没必要给,因为没有资源,不需要清理
	~Date()
	{}
	//输出<<的重载,输出日期
	friend ostream& operator<<(ostream& _cout, const Date& d)
	{
		_cout << d._year << "/" << d._month << "/" << d._day;
		return _cout;
	}
private:
	//获取一个月里有几天
	int GetDaysOfMonths(int year, int month)
	{
		int days[] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
		if (month == 2 && IsLeap(year))
			days[2] += 1;
		return days[month];
	}

	//判断是否是闰年
	bool IsLeap(int year)
	{
		if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
			return true;
		return false;
	}
private:
	int _year;
	int _month;
	int _day;
};

猜你喜欢

转载自blog.csdn.net/zimituanzi_/article/details/80804690
今日推荐