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

本文介绍了一个日期类的实现,包括日期的加减操作、比较运算及流输入输出等成员函数。通过这些功能,用户可以轻松地进行日期计算。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目录

一、头文件

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值