C++实现日期计算器详细代码示例

 更新时间:2024年03月05日 09:09:30   作者:蒋志昂  
这篇文章主要给大家介绍了关于C++实现日期计算器的相关资料,基于C++编写的简单的日期计算器,供大家参考,文中通过代码介绍的非常详细,需要的朋友可以参考下

概要

本篇主要探究C++ 实现日期计算器

Date类的规划

class Date
{
public:
	int GetMonthDay(int year, int month);
	Date(int year = 2024 , int month = 2, int day = 6);
	Date(const Date& d);
	Date& operator=(const Date& d);
	~Date();
	Date& operator+=(int day);
	Date operator+(int day);
	Date operator-(int day);
	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;
	void Print();

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

Date类的实现

Date 构造函数

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

构造函数,他是在创建类的时候调用进行初始化操作,我们这里声明与定义分离,所以它的参数不需要填写缺省值,缺省值在声明的时候定义即可。

Date 拷贝构造函数

Date::Date(const Date& x)
	:_year(x._year)
	, _month(x._month)
	, _day(x._day)
{ }

拷贝构造函数和构造函数作用相似,拷贝构造函数是将已有的类的对象拷贝给新创建的类的对象,如上所示。

~Date 析构函数

Date::~Date()
{
	_year = _month = _day = 0;
}

构析函数是在对象即将销毁前所调用的函数,常用来清理数据空间,这里我们的内存都在栈上申请的,所以影响不大。

GetMonthDay 求某年某月的天数

int Date::GetMonthDay(int year,int month)
{
	int a[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;
	}
	return a[month];
}

该函数是用来求某年某月的月份天数,通过创建一个大小为13的数组a, 再对二月份进行判断闰年来确定是否为特殊情况,否则返回已设定好的月份天数。

operator= 赋值操作符重载

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

该函数用于对象与对象之间的赋值操作,在对象的年月日进行逐一复制后并返回this指针的解引用。

operator+= 加等操作符重载

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

该函数用于日期与天数的相加,在实现该函数时先将this->_day加上想加的day。然后再通过月份的天数以及月与年的关系进行调整,如上所示。

operator+ 加号操作符重载

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

通过复用加等操作符重载,在不改变*this的情况下,返回相加后的对象

operator-= 减等操作符重载

Date& Date::operator(int day)
{
	_day-=day;
	int time =GetMonthDay(_year,_month)
	while(_day>=0)
	{
		_day+=time;
		_month--;
		if(_month<=1)
		{
			_month+=12;
			_year--;
		}
		time=GetMonthDay(_year,_month);
	} 
	return *this;
}

该函数和加等操作符重载类似,先将this->_day减去day,然后进行月份的天数和年与月之间的调整即可。

operator- 减法操作符重载 (日期 - 天数)

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

通过复用减等操作符重载,在不改变*this的情况下,返回相加后的对象

operator++ 前置自增操作符重载

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

operator++ 后置自增操作符重载

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

operator-- 前置自减操作符重载

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

operator-- 后置自减操作符重载

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

operator< 小于操作符重载

bool Date::operator<(const Date& d)
{
	if(_year<d._year)
	{
		return 1;
	}
	else if(_year==d._year)
	{
		if(_month<d._month)
		{
			return 1;
		}
		else if(_month==d._month)
		{
			if(_day<d._day)
			{
				return 1;
			}
		}
	}
	return 0;
}

operator== 相等操作符重载

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

operator!= 不等操作符重载

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

operator<= 小于或等于操作符重载

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

operator> 大于操作符重载

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

operator>= 大于或等于操作符重载

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

operator- 减法操作符重载(日期 - 日期)

int operator-(const Date& date) const
{
	Date max = *this,min = date;
	int flag=(max>=min);
	if(!flag)
	{
		max=date;
		min=*this;
		flag=-1;
	}
	int count=0;
	while(max>min)
	{
		min++;
		count++;
	}
	return flag*count;
}

通过比较选出最大的日期max与最小的日期min,让最小的日期min和记录数的count进行循环自增,直到max与min相等为止,此时刻的count就是相差的天数,而flag先前用于比较的就是它的符号,所以返回flag*count。

Print 打印函数

void Date::Print()
{
	printf("%d %d %d\n",_year,_month,_day);
}

Date 类的测试

测试操作符重载(+,+=,- ,-=,x++,++x,x–,–x)

void Test_1()
{
	std::cout << "Test_1:\n";
	Date a(2024, 2, 6);
	std::cout << "a ||";
	a.Print();
	Date b = a;
	std::cout << "b ||";
	b.Print();
	b += 30;
	std::cout << "b += 30 ||";
	b.Print();
	a -= 20;
	std::cout << "a -= 20 ||";
	a.Print();
	Date c = b - 30;
	std::cout << "c 或 b - 30 ||";
	c.Print();
	Date d = a + 20;
	std::cout << "d 或 a + 20 ||";
	d.Print();
	d++;
	std::cout << "++d ||";
	d.Print();
	c--;
	std::cout << "++c ||";
	c.Print();
	Date e = a++;
	std::cout << "e = a++ -> e||";
	e.Print();
	std::cout << "e = a++ -> a||";
	a.Print();
	Date f = a++;
	std::cout << "f = b-- -> f||";
	f.Print();
	std::cout << "f = b-- -> b||";
	b.Print();

	std::cout << "\n";
}

运行结果如下:

测试操作符重载(>,>=,==,<,<=,!=)

void Test_2()
{
	std::cout << "Test_2:\n";
	Date a(2024, 2, 6);
	std::cout << "a ||";
	a.Print();
	Date b = a + 999;
	std::cout << "b ||";
	b.Print();
	Date c = a - 999;
	std::cout << "c ||";
	c.Print();
	Date d = a;
	std::cout << "d ||";
	d.Print();

	std::cout << "a < b  ->" << (a < b) << std::endl;
	std::cout << "a > b  ->" << (a > b) << std::endl;
	std::cout << "a == b ->" << (a == b) << std::endl;
	std::cout << "a != b ->" << (a != b) << std::endl;

	std::cout << "a < c  ->" << (a < c) << std::endl;
	std::cout << "a > c  ->" << (a > c) << std::endl;
	std::cout << "a == c ->" << (a == c) << std::endl;
	std::cout << "a != c ->" << (a != c) << std::endl;

	std::cout << "a == d ->" << (a == d) << std::endl;
	std::cout << "a != d ->" << (a != d) << std::endl;
	std::cout << "a >= d ->" << (a >= d) << std::endl;
	std::cout << "a <= d ->" << (a <= d) << std::endl;
	std::cout << "a < d  ->" << (a < d) << std::endl;
	std::cout << "a > d  ->" << (a > d) << std::endl;

	std::cout << "\n";
}

运行结果如下:

测试操作符重载(日期 - 日期)

void Test_3()
{
	std::cout << "Test_3:\n";
	Date a(2024, 2, 6);
	Date b(2025, 2, 6);
	Date c = a + 99;
	std::cout << "c ||";
	c.Print();

	std::cout << "a ||";
	a.Print();
	std::cout << "b ||";
	b.Print();

	std::cout << "a - b ||" << (a - b) << std::endl;
	std::cout << "b - b ||" << (b - b) << std::endl;
	std::cout << "b - a ||" << (b - a) << std::endl;

	std::cout << "c - a ||" << (c - a) << std::endl;
	std::cout << "a - c ||" << (a - c) << std::endl;

	std::cout << "\n";
}

运行结果如下:

测试完毕,综合上述代码及运行结果,可得代码正确,可以正常模拟日期计算器。

结语

到此这篇关于C++实现日期计算器的文章就介绍到这了,更多相关C++日期计算器内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • C语言算法练习之数组求素数

    C语言算法练习之数组求素数

    这篇文章主要为大家介绍了C语言算法练习中数组求素数的实现方法,文中的示例代码讲解详细,对我们学习C语言有一定帮助,需要的可以参考一下
    2022-09-09
  • c++判断文件是否存在的方法汇总

    c++判断文件是否存在的方法汇总

    这篇文章主要介绍了c++判断文件是否存在的方法汇总,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-08-08
  • C++回调函数的理解和使用教程

    C++回调函数的理解和使用教程

    这篇文章主要给大家介绍了关于C++回调函数的理解和使用的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-12-12
  • Qt QWidget实现图片旋转动画

    Qt QWidget实现图片旋转动画

    这篇文章主要为大家详细介绍了如何使用了Qt和QWidget实现图片旋转动画效果,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下
    2024-12-12
  • 7种排序算法的实现示例

    7种排序算法的实现示例

    这篇文章主要介绍了7种排序算法的实现示例,需要的朋友可以参考下
    2014-05-05
  • C++ vector扩容解析noexcept应用场景

    C++ vector扩容解析noexcept应用场景

    这篇文章主要介绍了C++ vector扩容解析noexcept应用场景,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-09-09
  • C++中IO多路复用(select、poll、epoll)的实现

    C++中IO多路复用(select、poll、epoll)的实现

    I/O多路复用是一种并发处理多个I/O操作的机制,本文主要介绍了C++中IO多路复用(select、poll、epoll)的实现,具有一定的参考价值,感兴趣的可以了解一下
    2024-03-03
  • C++ 读文件 将文件内容读入到字符串string中的方法

    C++ 读文件 将文件内容读入到字符串string中的方法

    今天小编就为大家分享一篇C++ 读文件 将文件内容读入到字符串string中的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-07-07
  • C++中std::construct()与std::destroy()的使用

    C++中std::construct()与std::destroy()的使用

    std::construct()和std::destroy()是C++ STL中的函数模板,用于在已分配的存储区域中构造或销毁对象,本文主要介绍了C++中std::construct()与std::destroy()的使用,感兴趣的可以了解一下
    2024-02-02
  • C语言编程中函数的基本学习教程

    C语言编程中函数的基本学习教程

    这篇文章主要介绍了C语言编程中函数的基本学习教程,其中着重讲到了传值调用与参数,需要的朋友可以参考下
    2015-12-12

最新评论