C++ ---- 日期类实现+阅读文档(文档可直接下载)

日期类文档下载(日期类详细介绍)

word文档

MyDate/MyDate/日期类阅读文档.docx · 张喜阳/进阶代码仓库 - Gitee.comhttps://gitee.com/niuniuzxy/advanced-code-warehouse/blob/a25baeee2bd0f0c64f96315bb0d0023308329d92/MyDate/MyDate/%E6%97%A5%E6%9C%9F%E7%B1%BB%E9%98%85%E8%AF%BB%E6%96%87%E6%A1%A3.docxPDFMyDate/MyDate/日期类阅读文档.pdf · 张喜阳/进阶代码仓库 - Gitee.comhttps://gitee.com/niuniuzxy/advanced-code-warehouse/blob/a25baeee2bd0f0c64f96315bb0d0023308329d92/MyDate/MyDate/%E6%97%A5%E6%9C%9F%E7%B1%BB%E9%98%85%E8%AF%BB%E6%96%87%E6%A1%A3.pdf

日期类介绍

 

通过日期类的实现,对类和对象的语法进行熟悉,以练带学。对于运算符重载等知识进行巩固,对于流插入和流提取、前置++和后置++等运算符重载深入理解,让日期类能向内置类型一样进行加减,输入输出等操作。

要点分析(更多内容在文档中有介绍)

前置++和后置++对比

对于内置类型而言,后置++/--比前置++/--多了一次拷贝,在实现上后置的++/--的参数列表要显示的写int,这是和编译器的暗号,用来区分是前置++/--还是后置++/--。

流插入和流提取的重载

流插入和流提取在基础篇介绍过,它们分别是istream和ostream的对象,对于自定义类型而言,流插入和流提取是无法识别的,因为库中只实现了内置类型的<<和>>重载。当我们想要自定义类型的对象像内置类型一样输出和输出时,需要重载<<和>>。

日期加天数分析

 更详细的处理在文档中有介绍,还包括日期类对象-天数等逻辑分析。

整体代码 

Date.h

#pragma once
#include <iostream>
using namespace std;
class Date
{
	friend  ostream& operator<<(ostream& out, const Date& d1);
	friend  istream& operator>>(istream& in,Date& d1);

public:
	//频繁调用,放在类中,默认内联属性
	int GetMonthDay(int year, int month)
	{
		int monthDay[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
		//如果是闰年,2月有29天
		if (month == 2 && ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0))
		{
			return 29;
		}
		else
		{
			return monthDay[month];
		}
	}
	Date() {}
	Date(int year, int month,int day)
	{
		_year = year;
		_month = month;
		_day = day;
		if (month > 12 || day > GetMonthDay(year, month))
		{
			cout << "错误的日期" << endl;
			Show();
		}
	}
	//按一定格式打印日期
	void Show() const;
	//判断两个日期是否相等
	bool operator==(const Date& d1) const;
	//判断两个日期是否不相等
	bool operator!=(const Date& d1) const;
	//*this是否大于d1
	bool operator>(const Date& d1) const;
	//*this是否>=d1
	bool operator>=(const Date& d1) const;
	//*this是否小于d1
	bool operator<(const Date& d1) const;
	//*this是否小于等于d1
	bool operator<=(const Date& d1) const;
	//前置++
	Date& operator++()
	{
		*this += 1;
		return *this;
	}
	//后置++
	Date operator++(int)
	{
		Date temp(*this);
		*this += 1;
		return temp;
	}
	//前置--
	Date& operator--()
	{
		*this -= 1;
		return *this;
	}
	//后置--
	Date operator--(int)
	{
		Date dd(*this);
		*this -= 1;
		return dd;
	}
	
	//日期+=天数
	Date& operator+=(const size_t day);
	//日期+天数
	Date operator+(const size_t day)const;
	//日期-=天数
	Date& operator-=(const size_t day);
	//日期-天数
	Date operator-(const size_t day)const;
	//日期-日期
	int operator-(const Date& d1);

	//赋值重载,默认生成的就可以
	//拷贝构造,内置类型逐字节拷贝,默认的就行
	//析构,不申请资源,不写了
private:
	int _year=1;
	int _month=1;
	int _day=1;
};

//流插入重载
inline ostream& operator<<(ostream& out,const Date& d1)
{
	out << d1._year << "/" << d1._month << "/" << d1._day << endl;
	return out;
}
//流提取重载
inline istream& operator>>(istream& in,Date& d1)
{
	in >> d1._year >> d1._month >> d1._day;
	return in;
}

Date.cpp

#define _CRT_SECURE_NO_WARNINGS 1
#include "Date.h"

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

bool Date::operator==(const Date& d1)const
{
	return _year == d1._year
		&& _month == d1._month
		&& _day == d1._day;
}

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

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

	return false;
}

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

bool Date::operator<(const Date& d1)const
{
	return !(*this >= d1);
}
bool Date::operator<=(const Date& d1)const
{
	//return (*this) < d1 || (*this) == d1;
	return !((*this) > d1);
}

Date& Date::operator+=(const size_t day)
{
	//if (day < 0)
	//{
	//	//加负天等于减去整天
	//	return *this -=  (-day);
	//}
	_day += day;

	while (_day > GetMonthDay(_year, _month))
	{
		_day -= GetMonthDay(_year, _month);
		_month++;

		if (_month > 12)
		{
			_year++;
			_month = 1;
		}
	}
	return *this;
}
Date Date::operator+(const size_t day)const
{
	Date dd(*this);
	dd += day;
	return dd;
}
Date& Date::operator-=(const size_t day)
{
	//if (day < 0)
	//{
	//	//减去一个负天 等价 加上正天
	//	return *this += (-day);
	//}
	_day -= day;
	while (_day < 1)
	{
		_month--;
		if (_month == 0)
		{
			_year--;
			_month = 12;
		}
		_day += GetMonthDay(_year, _month);
	}
	return *this;
}
Date Date::operator-(const size_t day)const
{
	Date dd(*this);
	dd -= day;
	return dd;
}
int Date::operator-(const Date& d1)
{
	Date tmp(d1);
	int flag = 1;
	Date Max = *this;
	Date Min = tmp;

	if (*this < tmp)
	{
		flag = -1;
		Max = tmp;
		Min = *this;
	}

	int daySum = 0;
	while (Max != Min)
	{
		++Min;
		daySum++;
	}
	return daySum * flag;
}

  • 8
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 8
    评论
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值