【C++】日期计算器(类class)

1.代码

1.Date.h

#pragma once
#include <iostream>
using namespace std;

class Date
{
public:
	int& GetMonthDay(int& year, int& month)//获得该月天数
	{
		static int a[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
		static int b = 29;
		if ((month == 2) && ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0))
		{
			return b;
		}
		return a[month];
	}
	Date(int year = 1, int month = 1,int day=1);//构造函数
	void Print();
	bool operator==(const Date& d);//判断天数大小
	bool operator>(const Date& d);
	bool operator>=(const Date& d);
	bool operator<(const Date& d);
	bool operator<=(const Date& d);

	Date operator+(int day);//加天数
	Date operator-(int day);

	Date& operator=(Date& d);//赋值
	Date& operator+=(int day);
	Date& operator-=(int day);
	
	Date& operator++();//前置++和--
	Date& operator--();

	Date operator++(int);//后置++和--
	Date operator--(int);
private:
	int _year;
	int _month;
	int _day;
};

2.Date.cpp

#include "Date.h"

Date::Date(int year, int month, int day)//构造函数
{
	if (!(year >= 1 && (month >= 1 && month <= 12) && (day >= 1 && day <= GetMonthDay(year, month))))
	{
		cout << "日期非法" << endl;
	}
	else
	{
		_year = year;
		_month = month;
		_day = day;
	}
}

//打印
void Date::Print()
{
	cout << _year << "-" << _month << "-" << _day << endl;
}

//判断天数大小
bool Date::operator==(const Date& d)
{
	return this->_day == d._day && this->_month == d._month && this->_year == d._year;
}
bool Date::operator>(const Date& d)
{
	if (this->_year > d._year)
		return true;
	else
	{
		if (this->_year == d._year && this->_month > d._month)
		{
			return true;
		}
		else
		{
			if (this->_year == d._year && this->_month == d._month && this->_day > d._day)
			{
				return true;
			}
			else
				return false;
		}
	}
}
bool Date::operator>=(const Date& d)
{
	return *this > d || *this == d;
}
bool Date::operator<(const Date& d)
{
	return !(*this >= d);
}
bool Date::operator<=(const Date& d)
{
	return !(*this > d);
}

//赋值
Date& Date::operator=(Date& d)
{
	if (!(d._year >= 1 && (d._month >= 1 && d._month <= 12) && (d._day >= 1 && d._day <= GetMonthDay(d._year, d._month))))
	{
		cout << "日期非法" << endl;
	}
	else
	{
		_year = d._year;
		_month = d._month;
		_day = d._day;
	}
	return *this;
}
Date& Date::operator+=(int day)
{
	this->_day += day;
	while (this->_day > GetMonthDay(this->_year, this->_month))
	{
		this->_day -= GetMonthDay(this->_year, this->_month);
		this->_month++;
		if (this->_month == 13)
		{
			this->_month = 1;
			this->_year++;
		}
	}
	return *this;
}
Date& Date::operator-=(int day)//5.12 - 18
{
	this->_day -= day;
	while (this->_day <= 0 )
	{
		this->_month--;
		if (this->_month == 0)
		{
			this->_month = 12;
			this->_year--;
		}
		this->_day += GetMonthDay(this->_year, this->_month);
		
	}
	return *this;
}

//加天数
Date Date::operator+(int day)
{
	Date ret(*this);
	ret += day;
	return ret;
}
Date Date::operator-(int day)
{
	Date ret(*this);
	ret -= day;
	return ret;
}
//前置++和--
Date& Date::operator++()
{
	*this += 1;
	return *this;
}
Date& Date::operator--()
{
	*this -= 1;
	return *this;
}
//后置++和--
Date Date::operator++(int)
{
	Date ret(*this);
	*this += 1;
	return ret;
}
Date Date::operator--(int)
{
	Date ret(*this);
	*this -= 1;
	return ret;
}

3.main.cpp

#include "Date.h"

int main()
{
	Date d1(2023, 1, 1);
	Date d2(2023, 1, 1);
	Date d3(2021, 1, 1);
	(d1++).Print();
	(d2--).Print();
	/*(++d1).Print();
	(--d2).Print();*/
	/*d1 = d2;
	d1.Print();
	d1 = d3;
	d1.Print();*/
	//d1 += 1000;
	//d2 -= 1000;

	//d2.Print();
	//(d1+1000).Print();
	//(d2-1000).Print();
	/*d1.Print();
	d2.Print();*/
}

  • 5
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 6
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_hhc_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值