C++日期运算符重载实现日期计算器

目录

一、头文件

二、test.cpp

三、获取每个月天数

四、逻辑运算符函数

五、算数运算符函数

六、类对象的流输入、输出


一、头文件

#pragma once
#include <iostream>
using namespace std;
enum {
	Exit,
	Add,
	SubDay,
	Tomorrow,
	Yesterday,
	DaySub
};
class Date
{
public:
	Date(int year=2022, int month=10, int day=9)
	{
		_year = year;
		_month = month;
		_day = day;
	 }
	void print()
	{
		cout << _year<<" "<< _month <<" "<< _day << endl;
	}
	//逻辑运算
	bool operator==(const Date& d) const; 
	bool operator!=(const Date& d)const;
	Date& operator=(const Date& d);
	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)const;
	Date& operator-=(int day);
	Date operator-(int day)const;
	int operator-(const Date& d)const;
	//前置
	Date& operator++();
	//后置
	Date operator++(int);
	//前置
	Date& operator--();
	//后置
	Date operator--(int);
	//类流输出
    friend ostream& operator<<(ostream& out,const Date&d);
	//类流输入
	friend istream& operator>>(istream& in, Date& d);

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

二、test.cpp

#define _CRT_SECURE_NO_WARNINGS 1
#include"Date.h"
int main()
{
	cout << "***************************************************" << endl;
	cout << "********* 1.日期加天数      2.日期减天数   ********" << endl;
	cout << "********* 3.计算后一天      4.计算前一天   ********" << endl;
	cout << "********* 5.两日期相减      0.退出         ********" << endl;
	cout << "***************************************************" << endl;
	int input;
	do
	{
		cout << "请输入要实现功能:" ;
		cin >> input;
		switch (input)
		{
		case Exit:break;
		case Add:
		{
			Date d;
			cout << "请输入日期:";
			cin >> d;
			int day;
			cout <<"请输入要加天数:" ;
			cin >> day;
			cout<<d+day;
			break;
		}
		case SubDay:
		{
			Date d;
			cout << "请输入日期:" ;
			cin >> d;
			int day;
			cout << "请输入要减天数:" ;
			cin >> day;
			cout<< d-day;
			break;
		}
		case Tomorrow:
		{
			Date d;
			cout << "请输入日期:" ;
			cin >> d;
			cout << ++d;
			break;
		}
		case Yesterday:
		{
			Date d;
			cout << "请输入日期:";
			cin >> d;
			cout << --d;
			break;
		}
		case DaySub:
		{
			Date d1, d2;
			cout << "请输入第一个日期:";
			cin >> d1;
			cout<< "请输入第二个日期:" ;
			cin >> d2;
			cout << d1 - d2<<endl;
			break;
		}
		default:
		{
			cout << "输入错误,请重新输入"<<endl;
			break;
		}
		}
	} while (input);
	return 0;
}

三、获取每个月天数

int Getmonthday(int year, int month)
{
    //静态避免每次调用重复开辟空间
	static int MonthDay[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
	if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
	{
		MonthDay[2] = 29;
	}
	return MonthDay[month];
}

四、逻辑运算符函数

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

五、算数运算符函数

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)
{
	_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)
{
	_day -= day;
	while (_day <= 0)
	{
		_day += Getmonthday(_year, _month);
		_month--;
		if (_month == 0)
		{
			_year--;
			_month = 12;
		}
	}
	return *this;
}
int Date::operator-(const Date& d)const
{
	Date max = *this;
	Date min = d;
	int flag = 1;
	if (d<(*this))
	{
		max = d;
		min = *this;
		flag = -1;
	}
	int n = 0;
	while (max != min)
	{
		n++;
		min++;
	}
	return n * flag;
}
Date Date::operator-(int day)const
{
	Date tmp(*this);
	tmp -= day;
	return tmp;
}
//前置
Date& Date::operator++()
{
	return *this += 1;
}
//后置
Date Date::operator++(int)
{
	Date tmp(*this);
	*this += 1;
	return tmp;
}
//前置--
Date& Date::operator--()
{
	return *this -= 1;
}
//后置--
Date Date::operator--(int)
{
	Date tmp(*this);
	*this -= 1;
	return tmp;
}

六、类对象的流输入、输出

ostream& operator<<(ostream& out, const Date&d )
{
	out << d._year << "/" << d._month << "/" << d._day << endl;
	return out;
}
istream& operator>>(istream& in, Date&d)
{
	in >>d._year >> d._month >> d._day;
	return in;
}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
(1) 测试日期类成员函数,在主函数中列出菜单选项,可以完成日期的加减比较等测试功能。 (2) 完善程序功能,在日期相加的菜单选项中增加日期加天数,结果为新日期日期家月份,结果为新日期,要考虑闰年情况。 (3) 完善程序功能,在日期相减的菜单选项中增加日期减天数,结果为新日期日期减月份,结果为新日期,要考虑闰年情况。 (4) 显示日期时增加显示星期及英文形式的月份的功能。 (5) 增加输入的甄别功能,即输入非法数据(如负数、日期超过31天、时间超过24小时等情况)的识别显示功能。 (1) 仿照日期类编写时间类CTime_t,可以完成时间的设置、运算、比较等功能。 (2) 增加时间的输入功能,既可以选择输入格式,可以输入hh:mm:ss格式的信息。 (3) 增加时间的输出格式,可以输出12小时的时间格式。 (4) 编写时间和日期的派生类CDati,完成日期与时间的联合设置、运算、比较等功能,要求该派生类可以完成:日期时间加天数或时间等于新的日期时间,日期时间减天数或等于新的日期时间,两个日期时间相减等于天数或时间等工作,在程序中考虑闰年等具体情况,并重载各种运算符。 (5) 增加输入的甄别功能,即输入非法数据,即输入非法数据(如负数、日期超过31天、时间超过24小时等情况)的识别显示功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值