【C++】实现一个日期计算器

  • Post author:
  • Post category:其他


🌇

个人主页



平凡的小苏


📚

学习格言

:命运给你一个低的起点,是想看你精彩的翻盘,而不是让你自甘堕落,脚下的路虽然难走,但我还能走,比起向阳而生,我更想尝试逆风翻盘。

🛸

C++专栏



C++内功修炼基地



家人们更新不易,你们的👍点赞👍和⭐关注⭐真的对我真重要,各位路 过的友友麻烦多多点赞关注。 欢迎你们的私信提问,感谢你们的转发! 关注我,关注我,关注我,你们将会看到更多的优质内容!!

在这里插入图片描述



1、日期计算器的功能


日期计算器具备

:日期+=天数、日期+天数、日期-天数、日期-=天数、前置++、后置++后置–、前置–、 >运算符重载、==运算符重载、>=运算符重载、<运算符重载、<=运算符重载、!=运算符重载、日期-日期 返回天数



2、获取月份和天数的函数

代码如下:

int GetMonthDay(int year, int month)
	{
		static int arr[13] = { 0,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;
		}
		else
		{
			return arr[month];
		}
	}


注:年是有闰年和平年的,需要额外判断



3、日期类的默认成员函数



3.1、默认构造函数

	// 全缺省的构造函数
Date(int year = 1900, int month = 1, int day = 1)
{
    _year = year;
    _month = month;
    _day = day;
    assert(checkDate());
}



3.2、析构函数

	//析构函数
~Date()//可不写
{
    ;
}



3.3、拷贝构造函数

Date(const Date & d)//可不写
{
	_year = d._year;
	_month = d._month;
	_day = d._day;
}



3.4、赋值运算符重载

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


注:析构函数和拷贝构造在日期类中是可以不写的,因为编译器默认生成的够用了,因为只有在处理有资源的对象才需要我们手动写,默认生成的是值拷贝,处理有资源的对象不进行手动开辟,默认生成的会指向同一块空间,同一块空间会被析构两次。



4、日期类的功能实现



4.1、日期+=天数

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 == 13)//更新年和月
		{
			_year++;
			_month = 1;
		}
	}
	return *this;
}



4.2、日期+天数

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


注:因为没有改变成员函数,所以可以用const修饰



4.3、日期-=天数

Date& Date::operator-=(int day)
{
	if (day < 0)//小于0,复用+=运算符重载
	{
		return *this += -day;
	}
	_day -= day;
	while (_day <= 0)
	{
		_month--;//注意:要算减后月份的天数
		_day += GetMonthDay(_year,_month);
		if (_month == 0)//更新月份和天数
		{
			_month = 13;
			_year--;
		}
	}
	return *this;
}



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++(int)//后置++编译器做了函数重载,用一个int占位
{
	Date tmp(*this);
	tmp += 1;
	return tmp;
}



4.6、前置–和后置–

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



4.7、>运算符重载

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


注:我们在写比较运算符重载时,只需要写大于和等于两个运算符重载就好了因为其他的比较运算符都可以复用了,但是注意不要互相复用



4.8、==运算符重载

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



4.9、>=、<、<=、!=运算符重载

// >=运算符重载
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) || (*this == d);
}
// !=运算符重载
bool Date::operator != (const Date& d) const
{
	return !(*this == d);
}



4.10、日期-日期 (返回天数)

// 日期-日期 返回天数
int Date::operator-(const Date& d)
{
	int flag = 1;
	Date max = *this;
	Date min = d;
	if (*this < d)//找到小的日期
	{
		flag = -1;
		max = d;
		min = *this;
	}
	int n = 0;
	while (max != min)//利用计数方法实现相差天数
	{
		++min;
		n++;
	}
	return n * flag;
}



4.11、>>和<<流提取和流插入运算符重载

bool checkDate()//判断日期合法性函数
{
    if (_year >= 1 && _month > 0 && _month < 13 && _day > 0 && _day < GetMonthDay(_year, _month))
    {
        return true;
    }
    else
    {
        return false;
    }
}
inline ostream& operator<<(ostream& out, const Date& d)
{
	out << d._year << '-' << d._month << '-' << d._day << endl;
	return out;
}
inline istream& operator>>(istream& in, Date& d)
{
	in >> d._year >> d._month >> d._day;
	assert(d.checkDate());//判断输入日期的合法性
	return in;
}



5、日期类源码

//Date.h
#include<iostream>
#include<cassert>
#include<ostream>
using namespace std;
class Date
{
	friend inline ostream& operator<<(ostream& out, const Date& d);
	friend inline istream& operator>>(istream& in, Date& d);
public:
	void Print()
	{
		cout << _year << '-' << _month << '-' << _day << endl;
	}
	// 获取某年某月的天数
	int GetMonthDay(int year, int month)
	{
		static int arr[13] = { 0,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;
		}
		else
		{
			return arr[month];
		}
	}
	bool checkDate()
	{
		if (_year >= 1 && _month > 0 && _month < 13 && _day > 0 && _day < GetMonthDay(_year, _month))
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	// 全缺省的构造函数
	Date(int year = 1900, int month = 1, int day = 1)
	{
		_year = year;
		_month = month;
		_day = day;
		assert(checkDate());
	}
	//析构函数
	~Date()
	{
		;
	}
	// 赋值运算符重载
	Date& operator=(const Date& d);
	// 日期+=天数
	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);

private:
	int _year;
	int _month;
	int _day;
};
inline ostream& operator<<(ostream& out, const Date& d)
{
	out << d._year << '-' << d._month << '-' << d._day << endl;
	return out;
}
inline istream& operator>>(istream& in, Date& d)
{
	in >> d._year >> d._month >> d._day;
	assert(d.checkDate());
	return in;
}
//Date.cpp
#include"Date.h"
Date& 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 == 13)
		{
			_year++;
			_month = 1;
		}
	}
	return *this;
}
// 日期+天数
Date Date::operator+(int day) const
{
	Date tmp(*this);
	tmp += day;
	return tmp;
}

// 日期-=天数
Date& Date::operator-=(int day)
{
	if (day < 0)
	{
		return *this += -day;
	}
	_day -= day;
	while (_day <= 0)
	{
		_month--;
		_day += GetMonthDay(_year,_month);
		if (_month == 0)
		{
			_month = 13;
			_year--;
		}
	}
	return *this;
}
// 日期-天数
Date Date::operator-(int day) const
{
	Date tmp(*this);
	tmp -= day;
	return tmp;
}
// 前置++
Date& Date::operator++()
{
	*this += 1;
	return *this;
}
// 后置++
Date Date::operator++(int)
{
	Date tmp(*this);
	tmp += 1;
	return tmp;
}

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

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

// 日期-日期 返回天数
int Date::operator-(const Date& d)
{
	int flag = 1;
	Date max = *this;
	Date min = d;
	if (*this < d)
	{
		flag = -1;
		max = d;
		min = *this;
	}
	int n = 0;
	while (max != min)
	{
		++min;
		n++;
	}
	return n * flag;
}



版权声明:本文为VHhhbb原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。