Date类实现(c++)


前言

我们本篇内容将会对Date类进行模拟实现

总体框架

class Date
{
public:
private:
	int _year=1;
	int _month=1;
	int _day=1;
};

Date(int year=1, int month=1, int day=1);

拷贝构造,判断传值是否合法

Date::Date(int year, int month, int day)
{
	if (year >= 1 && month >= 1 && month <= 12 && day >= 1 && day <= getMonthDay(year, month))
	{
		_year = year;
		_month = month;
		_day = day;
	}
	else
	{
		assert(false);
	}
}

~Date();

析构函数,这里不涉及资源,我们可以不写

Date::~Date()
{

}

Date(const Date& d);

拷贝构造,浅拷贝

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

Date& operator=(const Date& d);

赋值

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

int getMonthDay(int year, int month);

获取每月的天数,方便计算。

int Date::getMonthDay(int year, int month)
{
	int arr[13] = { 0,31,28,31,30,31,30,31,31,30 ,31,30,31 };
	//闰年:4年一闰年且百年不润年   400年一闰年
	if (month==2&&((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)))
	{
		return 29;
	}
	return arr[month];
}

void Print()const;

进行简单打印,

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

bool operator>(const Date& d)const;


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

bool operator==(const Date& d)const;

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

bool operator<(const Date& d)const;

后面几个接口都是根据前面两个接口进行复用

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

bool operator!=(const Date& d)const;

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

bool operator>=(const Date& d)const;

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

bool operator<=(const Date& d)const;

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

Date operator+(int day);

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

Date& operator+=(int day);

我们这里选用+=复用+,而不是用+复用+=。
可以减少拷贝,提高效率。

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

Date operator-(int day);

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

Date& operator-=(int day);

Date& Date::operator-=(int day)
{
	_day -= day;
	while (_day <= 0)
	{
		//+上个月的
		_month--;
		if (_month == 0)
		{
			_year--;
			_month = 12;
		}
		_day += getMonthDay(_year, _month);
	}
	return *this;
}

int operator-(const Date&d);

两个日期相减,我们直接进行减是不容易操作的。

int Date::operator-(const Date& d)
{
	int flag = 1;
	Date min = *this;
	Date max = d;
	if (*this > d)
	{
		min = d;
		max = *this;
		flag = -1;
	}

	int n = 0;
	while (min != max)
	{
		if (n == 19)
		{
			int i = 0;
		}
		++n;
		++min;
	}
	return n*flag;
}

Date& operator++();

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

Date operator++(int);

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

Date& operator–();

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

Date operator–(int);

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

整体代码

#include <iostream>
using namespace std;
#include <assert.h>
class Date
{
public:
	Date(int year=1, int month=1, int day=1);
	~Date();
	Date(const Date& d);
	Date& operator=(const Date& d);

	int getMonthDay(int year, int month);
	void Print()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;

	Date operator+(int day);
	Date& operator+=(int day);
	Date operator-(int day);
	Date& operator-=(int day);

	int operator-(const Date&d);

	Date& operator++();
	Date operator++(int);
	Date& operator--();
	Date operator--(int);

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


#include "Date.h"

int Date::getMonthDay(int year, int month)
{
	int arr[13] = { 0,31,28,31,30,31,30,31,31,30 ,31,30,31 };
	//闰年:4年一闰年且百年不润年   400年一闰年
	if (month==2&&((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)))
	{
		return 29;
	}
	return arr[month];
}

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

Date::Date(int year, int month, int day)
{
	if (year >= 1 && month >= 1 && month <= 12 && day >= 1 && day <= getMonthDay(year, month))
	{
		_year = year;
		_month = month;
		_day = day;
	}
	else
	{
		assert(false);
	}
}
Date::~Date()
{
}

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

bool  Date::operator>(const Date& d)const
{
	if (_year > d._year)
	{
		return true;
	}
	if (_year == d._year && _month > d._month)
	{
		return true;
	}
	if (_year == d._year && _month > d._month && _day > d._day)
	{
		return true;
	}
	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) || (*this == d);
}
bool  Date::operator!=(const Date& d)const
{
	return !(*this == d);
}
bool  Date::operator<=(const Date& d)const
{
	return !(*this > d);
}

//d1+100
Date Date::operator+(int day)
{
	Date tmp(*this);
	tmp += day;
	return tmp;
}
Date& Date::operator+=(int day)
{
	_day += day;
	while (_day > getMonthDay(_year, _month))
	{
		_day -= getMonthDay(_year, _month);
		_month++;
		if (_month == 13)
		{
			_year++;
			_month = 1;
		}
	}
	return *this;
}
Date Date::operator-(int day)
{
	Date tmp(*this);
	tmp -= day;
	return tmp;
}
Date& Date::operator-=(int day)
{
	_day -= day;
	while (_day <= 0)
	{
		//+上个月的
		_month--;
		if (_month == 0)
		{
			_year--;
			_month = 12;
		}
		_day += getMonthDay(_year, _month);
	}
	return *this;
}

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

int Date::operator-(const Date& d)
{
	int flag = 1;
	Date min = *this;
	Date max = d;
	if (*this > d)
	{
		min = d;
		max = *this;
		flag = -1;
	}
	int n = 0;
	while (min != max)
	{
		if (n == 19)
		{
			int i = 0;
		}
		++n;
		++min;
	}
	return n*flag;
}

总结

以上就是今天要讲的内容。希望对大家的学习有所帮助,仅供参考 如有错误请大佬指点我会尽快去改正 欢迎大家来评论~~ 😘 😘 😘

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lim 鹏哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值