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语言数据结构之栈简单操作的相关资料,需要的朋友可以参考下
    2017-06-06
  • 如何判断一个数是否为2的幂次方?若是,并判断出来是多少次方?

    如何判断一个数是否为2的幂次方?若是,并判断出来是多少次方?

    本篇文章是对如何判断一个数是否为2的幂次方?若是,并判断出来是多少次方的实现方法,进行了详细的分析介绍,需要的朋友参考下
    2013-05-05
  • 浅谈时间戳与日期时间互转C语言

    浅谈时间戳与日期时间互转C语言

    下面小编就为大家带来一篇浅谈时间戳与日期时间互转C语言。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-06-06
  • C语言基于graphics.h实现圣诞树

    C语言基于graphics.h实现圣诞树

    这篇文章主要介绍了圣诞树代码,c语言编程,基于graphics.h实现,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-12-12
  • C++项目实战之makefile使用

    C++项目实战之makefile使用

    这篇文章主要介绍了C++项目实战之makefile使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-05-05
  • C++ 使用模板实现一个List的实例

    C++ 使用模板实现一个List的实例

    这篇文章主要介绍了 C++ 使用模板实现一个List的实例的相关资料,需要的朋友可以参考下
    2017-05-05
  • C++文件流读写操作详解

    C++文件流读写操作详解

    本文详细讲解了C++文件流读写操作的方法,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-11-11
  • C++11中的原子量和内存序详解

    C++11中的原子量和内存序详解

    这篇文章主要给大家介绍了关于C++11中原子量和内存序的相关资料,文中通过示例代码介绍地方非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2018-06-06
  • C++中防止头文件重复包含的几种方法

    C++中防止头文件重复包含的几种方法

    在 C/C++ 编程中,当一个项目比较大时,往往都是分文件,这时候有可能不小心把同一个头文件 include 多次,或者头文件嵌套包含,这些会导致一系列的问题,如符号重定义、编译错误等,因此,防止头文件的重复包含是至关重要的,本文给大家介绍了C++中防止头文件重复包含的两种方法
    2024-05-05
  • 浅谈Qt信号槽与事件循环的关系

    浅谈Qt信号槽与事件循环的关系

    本文主要介绍了Qt信号槽与事件循环的关系,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-08-08

最新评论