实现日期类

日期类的实现主要是去学习使用operator
日期类就是计算日期之间的天数,日期与(日期,天数)的相加减
比如日常生活中我们可以计算日期加天数,日期减天数,日期减日期,
没有日期加日期的说法
在这里插入图片描述

1.日期的比较

日期的比较,当写出大于和等于两个函数的时候,其他的比较函数就都可以复用这两个函数了

//先写大于的
//一定是先比较年,再比较月,再比较日
bool Date::operator>(const Date& d)
{
	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)
{
	return _year == d._year && _month == d._month && _day == d._day;
}

剩下的复用这两个函数就行了

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 || *this == d;
} 

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

2.日期的计算

日期的加法

日期的加减
日期的加天数我们要考虑到是否会进入到下一个月,下一年,当前年是否是闰年,二月有几天,所以我们还应该有一个计算当前月天数的函数
我们还要考虑代码运行时的效率,返回值可以用引用就用引用,可以减少调用拷贝构造的次数,+=操作返回的*this在函数结束后没有被销毁,所以可以返回引用

int GetMonthDay(int year,int month)
{
	assert(month > 0 && month < 13);
	//这个函数会经常调用所以我们可以把数组定义为静态类形
	static int MonthDayArr[13] ={-1,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;
	}
	else
	{
		return MonthDayArr[month];
	}
}

//先写+=,日期只有加天数
//没有日期加日期,例如2024.5.13+2024.5.1这样的写法没有意义
Date& Date::operate+=(int day)
{
	_day += day;

	while(_day > GetMonthDay(_year,_month))
	{
		_day -= GetMonthDay(_year,_month);
		++_month;
		if(_month == 13)
		{
			_month = 1;
			++_year;
		}
	}
	return *this;
}
//+=可以复用+
Date Date::operator+(int day)
{
	Date tmp = *this;
	tmp += day;
	return tmp;
}

日期的减法

日期的减法有两种一种是日期减天数,一种是日期减日期
第一种需要注意的是如果天数day是负数的情况,我们要加的是上个月的天数

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

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

//日期减日期----日期减日期计算的是他们之间相差多少天
int Date::operator-(const Date& d)
{
	Date max = *this;
	Date min = d;
	int flag = 1;
	if(max < min)
	{
		max = d;
		min = *this;
		flag = -1;
	}
	int n  =  0;
	
	while(max != min)
	{
		min++;
		n++;
	}
	return n;
}      

前置后置++,与- -

这个不难写,但要注意的一点四是如何区分前置与后置,c++规定后置++,- -的形参列表要有一个int类型


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

3.日期的输入输出

首先这个>>,<<不能写为成员函数,因为我们调用时要写成,对象.成员函数,所以我们写在类外面,然后用友员函数在类中声明

ostream& operator<<(ostream& out, const Date& d)
{
	cout << d._year << "-" << d._month << "-" << d._day << endl;
	return out;
}

istream& operator>>(istream& in, Date& d)
{
	cout << "请依次输入年月日";
	in >> d._year >> d._month >> d._day;
	return in;
}

日期类完整代码

Date.h

#pragma once

#include<iostream>
#include<assert.h>
using namespace std;

class Date
{
	friend ostream& operator<<(ostream& out, const Date& d);
	friend istream& operator>>(istream& in, Date& d);

public:
	int GetMonthDay(int year, int month)
	{
		assert(month > 0 && month < 13);

		static int Month[13] = { -1,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 Month[month];
	}

	//构造函数
	Date(int year = 1, int month = 1, int day = 1);

	
	void Print() 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;
	bool operator!=(const Date& d) const;
	
	Date& operator+=(int day);
	Date operator+(int day) const;
	Date& operator-=(int day);
	Date operator-(int day) const;
	
	Date& operator++();
	Date operator++(int);

	Date& operator--();
	Date operator--(int);

	int operator-(Date& d) const;

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

ostream& operator<<(ostream& out, const Date& d);
istream& operator>>(istream& in, Date& d);

Date.cpp

#define _CRT_SECURE_NO_WARNINGS

#include"Date.h"

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

void Date::Print() const
{
	cout << _year << " " << _month << " " << _day << endl;
}

bool Date::operator>(const Date& d) const
{
	if (_year > d._year)
	{
		return true;
	}
	else if (_year == d._year)
	{
		if (_month > d._month)
		{
			return true;
		}
		else if (_month == d._month)
		{
			return _day > d._day;
		}
	}
	return false;
}

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

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
{
	return *this < d || *this == d;
} 

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

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)
	{
		if (_month == 1)
		{
			--_year;
			_month = 12;
		}
		else
		{
			--_month;
		}
		_day += GetMonthDay(_year, _month);
	}
	return *this;
}

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

int Date::operator-(Date& d) const
{
	Date max = *this;
	Date min = d;
	int flag = 1;

	if (max < min)
	{
		max = d;
		min = *this;
		flag = -1;
	}

	int n = 0;

	while (max > min)
	{
		min++;
		n++;
	}
	return n * flag;
}

ostream& operator<<(ostream& out, const Date& d)
{
	cout << d._year << "-" << d._month << "-" << d._day << endl;
	return out;
}

istream& operator>>(istream& in, Date& d)
{
	cout << "请依次输入年月日";
	in >> d._year >> d._month >> d._day;
	return in;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

专科在努力!

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

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

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

打赏作者

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

抵扣说明:

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

余额充值