日期类初步实现

咱们简洁一点,以下是初步结构:

class Date
{
public:
    // 获取某年某月的天数
    int GetMonthDay(int year, int month);

    // 打印函数
    void Print();

    // 全缺省的构造函数
    Date(int year = 1900, int month = 1, int day = 1)

    // 拷贝构造函数
  // d2(d1)
    //Date(const Date& d);

    // 赋值运算符重载
  // d2 = d3 -> d2.operator=(&d2, d3)
    //Date& operator=(const Date& d);

    // 析构函数(这里不需要)
    //~Date();

    // 日期+=天数
    //Date& operator+=(int day);

    // 日期+天数
    //Date operator+(int day)const;

    // 日期-天数
    //Date operator-(int day)const;

    // 日期-=天数
    //Date& operator-=(int day);

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

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

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

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

    // >运算符重载
    //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;

    // 日期-日期 返回天数
    //int operator-(const Date& d)const;

private:
    int _year;
    int _month;
    int _day;
};

在写函数之前,注意不希望被修改的函数最好用const维护一下

0.CheckDate函数 

判断是否为非法日期

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

1.GetMonthDay函数

创建一个数组去列出12个月的天数,再额外判断一下闰年的2月(这里写成成员函数,因为调用频繁,作内联)

// 获取某年某月的天数
int GetMonthDay(int year, int month)
{
	assert(month > 0 && month < 13);

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

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

	return MonthDay[month];
}

2.构造和拷贝构造函数

本质上没有很大的区别,拷贝构造就是构造的一个重载,只不过拷贝构造的参数必须得是类类型对象的引用 

//构造
Date::Date(int year = 1900, int month = 1, int day = 1)
{
	_year = year;
	_month = month;
	_day = day;
}

//拷贝构造
Date(const Date& d)
{
	_year = d._year;
	_month = d._month;
	_day = d._day;
}

3.赋值运算符重载

 和拷贝构造是一个作用,但是多了个返回值

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

4.日期运算符重载

4.1日期+=

一开始加上要加的天数,如果超过月数天,就进位,将天数减去,年也是如此

Date& Date::operator+=(int day)
{
	_day += day;

	while (_day > GetMonthDay(_year,_month))
	{
		_day -= GetMonthDay(_year, _month);
		++_month;
		
		if (_month > 12)
		{
			_month = 1;
			++_year;
		}
	}

	return *this;
}

!由于担心day是负数,这里需要加几句代码 

if (day < 0)
{
	return *this -= -day;
}

4.2日期+

这里复用我们写的+=运算符

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

	return tmp;
}

4.3 日期-=

相反于日期+=

Date& Date::operator-=(int day)
{
	_day -= day;

	while (_day <0)
	{
		_day += GetMonthDay(_year, _month);
		--_month;

		if (_month <1)
		{
			_month = 12;
			--_year;
		}
	}

	return *this;
}

同+= ,加上几句代码

if (day < 0)
{
	return *this += -day;
}

4.4日期-

相反于日期+,复用-=

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

	return tmp;
}

4.5前置后置加减

用复用的方法非常简单

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

	return *this;
}

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

	return *this;
}

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

	return tmp;
}

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

	return tmp;
}

4.6日期比较大小

依旧是只要写其中一个大于或者小于,其他的就可以用服用完成,==也是一样


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;
}

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 || *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 !(*this >= d);
}

4.7日期相减

这里的思路是运用计数将小的加到与大的相等

int Date::operator-(const Date& d)const
{
	//设定一个值来确定大小
	int flag = 1;

	Date max = *this;
	Date min = d;
	if (*this < d)
	{
		min = *this;
		max = d;
		flag = -1;
	}
	
	//创建一个值来计数
	int n = 0;
	while (max != min)
	{
		++min;
		++n;
	}

	return n*flag;
}

4.8流运算符重载

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

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

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

	return in;
}

5.整体代码

可以参考我的gitee网址:王大先生/bit_class - Gitee.com

希望能帮到你

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值