六、日期类的实现

注意:

对于前置++/--或后置++/--的运算符重载函数,为了区分,构成重载,给后置++,强⾏增加了⼀个int形参,这⾥不需要写形参名,因为接收值是多少不重要,也不需要⽤这个参数,仅仅是为了跟前置++构成重载区分。

//Date.h
#pragma once
#include<iostream>
#include<assert.h>
using namespace std;

class Date
{
	//友元函数
	friend ostream& operator<<(ostream& out, const Date& d);
	friend istream& operator>>(istream& in, Date& d);
public:
	Date(int year = 2024, int month = 7, int day = 13);
	void Print() const;

	//获取某年某月的总天数,频繁调用,定义在类里面
	int Get_Month_Day(int year, int month) const
	{
		assert(month > 0 && month < 13);

		static int monthday[13]={-1, 31, 28, 31, 30, 31, 30,
								31, 31, 30, 31, 30, 31 };

		//闰年2月是29天
		if ((month == 2) && ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)))
		{
			return 29;
		}
		else
		{
			return monthday[month];
		}
	}

	//判断日期是否合法
	bool CheckDate() 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;
	bool operator!=(const Date& d) const;

	//d1+=天数
	Date& operator+=(int day);
	Date operator+(int day) const;

	//d1-=天数
	Date& operator-=(int day);
	Date operator-(int day) const;

	//++d1
	//为了区分,构成重载,给后置++,强⾏增加了⼀个int形参
	// 这个参数仅仅是为了跟前置++构成重载区分
	Date& operator++();
	//d1++
	Date operator++(int) const;

	//--d1
	Date& operator--();
	//d1--
	Date operator--(int) const;

	//d1 - d2
	int operator-(const Date& d) const;
private:
	int _year;
	int _month;
	int _day;
};

// 重载
ostream & operator<<(ostream & out, const Date & d);
istream& operator>>(istream& in, Date& d);
//Date.cpp
#define _CRT_SECURE_NO_WARNINGS 1
#include"Date.h"

Date::Date(int year, int month, int day)
{
	_year = year;
	_month = month;
	_day = day;

	if (!CheckDate())
	{
		cout << "日期非法" << endl;
	}
}

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

bool Date::CheckDate() const
{
	if (_month < 1 || _month>12 || _day<1 || _day>Get_Month_Day(_year, _month))
	{
		return false;
	}
	else
	{
		return true;
	}
}

//d1 < d2
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)
		{
			return _day < d._day;
		}
	}
	
	return false;
}

//d1 <= d2
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);
}

//d1+=天数
Date& Date::operator+=(int day)
{
	if (day < 0)
	{
		return *this -= (-day);
	}

	_day += day;
	if (_day > Get_Month_Day(_year, _month))
	{
		_day -= Get_Month_Day(_year, _month);
		++_month;
		if (_month == 13)
		{
			++_year;
			_month = 1;
		}
	}

	return *this;
}

Date Date::operator+(int day) const
{
	Date tmp = *this;
	tmp += day;
	return tmp;
}

//d1-=天数
Date& Date::operator-=(int day)
{
	if (day < 0)
	{
		return *this += (-day);
	}

	_day -= day;
	while (_day <= 0)
	{
		--_month;
		if (_month == 0)
		{
			--_year;
			_month = 12;
		}
		//借用上一个月的天数
		_day += Get_Month_Day(_year, _month);
	}

	return *this;
}

Date Date::operator-(int day) const
{
	Date tmp = *this;
	tmp -= day;
	return tmp;
}

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

//d1++
Date Date::operator++(int) const
{
	Date tmp = *this;
	tmp += 1;
	return tmp;
}

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

//d1--
Date Date::operator--(int) const
{
	Date tmp = *this;
	tmp -= 1;
	return tmp;
}

//d1 - d2
int Date::operator-(const Date& d) const
{
	Date max = *this;
	Date min = d;
	int flag = 1;

	if (*this < d)
	{
		max = d;
		min = *this;
		flag = -1;
	}

	int count = 0;
	while (min != max)
	{
		++min;
		++count;
	}

	return count * flag;
}

// 重载
ostream& operator<<(ostream& out, const Date& d)
{
	out << d._year<< "年" << d._month << "月" << d._day << "日" << endl;
	return out;
}

istream& operator>>(istream& in, Date& d)
{
	while (1)
	{
		cout << "请依次输入年月日:>";
		in >> d._year >> d._month >> d._day;

		if (!d.CheckDate())
		{
			cout << "输入日期非法:";
			d.Print();
			cout << "请重新输入!!!" << endl;
		}
		else
		{
			break;
		}
	}

	return in;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值