C++日期类,详细!!!

日期类的练习可以巩固提高之前所学习的知识,还可以完成关于的日期练习

下面是关于日期的对应oj题

KY111 日期差值

计算一年的第几天

累加天数

1.Date.h 头文件部分

看看我们要实现那些接口

1.获取对应月份的天数:GetMonthDay

  1. < > += + == 等的比较函数

  2. 日期 += 天数 、日期 + 天数、日期 -=天数 、日期 - 日期

首先,需要声明和定义的分离,分开文件更加方便管理;直接进入到最重要的接口部分

其次是构造函数,写个全缺省构造,因为可以满足传参和不传参的情况;

拷贝构造 在这里可写可不写,因为没有资源申请 ;赋值运算符重载的函数也是,没有资源申请,编译器默认生成的就够用;当然也只限于我这个版本的日期类

1.1GetMonthDay

1.为什么写这个函数? 因为 -= 和 += 用到的非常多,需要做对应运算

2.为这么这个函数写在类里? 因为这个很常用,需要经常调用;写在类里面默认是内联函数可以不用频繁创建函数栈帧;

3.数组设置为静态,用到的也很多;可以减少反复系统申请释放空间,提高运行效率

cpp 复制代码
#pragma once
#include <iostream>
#include <assert.h>
using namespace std;
class Date
{
public:
    Date(int year = 1900, int month = 1, int day = 1)
        :_year(year)    // 这里我使用初始化列表,会在下篇中讲到;初始化列表的功能和在函数体内的方法类似
        , _month(month)
        , _day(day)
    {
        //也可以继续使用之前的方法
        _year = year;
        _month = month;
        _day = day;
    }
    //获取对应月份的天数
    int GetMonthDay(int year, int month)
    {
        assert(month < 13 && month > 0);//必须在括号内设置的范围内,否则报错
        static int tomonth[13] = { 0,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 tomonth[month];
    }
    //拷贝构造  d2(d1)
    Date(const Date& d)
    {
        _year = d._year;
        _month = d._month;
        _day = d._day;
    }
    // 赋值运算符重载
    // d2 = d3 -> d2.operator=(&d2, d3)
    Date& operator=(const Date& d)
    {
        _year = d._year;
        _month = d._month;
        _day = d._day;
        return *this;
    }

    //const 的作用是修饰 const *this ;this指针指向的内容不可修改
    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);

    //日期 -= 天数 或 -
    Date& operator-=(int day);
    Date operator-(int day);

    // ++前置
    Date& operator++();
    // 后置++
    Date operator++(int);

    // --前置
    Date& operator--();
    // 后置--
    Date operator--(int);

    //两个日期相减
    int operator-(Date& d) const;

    bool ChackDate();
    friend ostream& operator<<(ostream& out, const Date& d);
    friend istream& operator>>(istream& in, Date& d);
private:
    int _year;
    int _month;
    int _day;
};

2.Date.CPP 功能实现部分

2.1比较日期大小

1.等于不用说,就是全部相等才相等;

2.满足小于就需要年月日最少有一个小于才行,所以年月日只要有一个小,那么就小

年小那么就小,年相等月小就小,月相等,日小就小;相反除以外就是大于了

cpp 复制代码
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)
		{
			if (_day < d._day)
			{
				return true;
			}
		}
	}
	return false;
}

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

2.2满足了最少两个比较,我选择的是 小于 + 大于

1.剩下比较就很简单,复用就能解决

2.假如是大于,那么只要排除两种情况小于和等于。总共就三种情况;既然如此满足小于和等于,那么就必定只剩下大于了

cpp 复制代码
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 && *this == d);
}

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

2.3日期 += 天数;日期 + 天数;

  1. 如果day输入的是负数,实际上就是减去天数了,直接去调用 日期 -= 天数
  2. 和上面的计算思维一样,相加进位计算;获取当月天数 (这个时候就体现这个函数的用处了),直到小于获取到的月份;每执行一次就进位,如果满12月,就进位年;最后返回*this,当前类的引用
  3. 那么日期 + 天数;就很简单了,直接多实例化一个类,然后复用 日期 +=天数;最后返回tmp,返回的时候会产生拷贝,但是首先解决问题更加重要
cpp 复制代码
// 日期 += 天数
Date& Date::operator+=(int day)
{
	if (day < 0)   // 防止输入的是负数;那么就是 -= 了  假设 day 是负数 -(-71) = 71
	{
		*this -= (-day);
		return *this;
	}
	_day += day;
	while (_day > GetMonthDay(_year, _month))// 如果计算出的日期相等 (31 == 31),那么返回否
	{
		if (_month > 12)
		{
			_year++;
			_month = 1;
		}
		_day -= GetMonthDay(_year, _month);
		_month++;
	}
	return *this;
}
//日期 + 天数
Date Date::operator+(int day)
{
	Date tmp = *this;
	tmp += day;
	return tmp;
}

2.4日期 -= 天数;日期 - 天数;

  1. 这里的 -= 逻辑和 +=非常类似;需要减去的时间到 <= 0 ;这里借位的逻辑和数学差不多
  2. 日期 - 天数的复用逻辑也是类似的
cpp 复制代码
//日期 -= 天数
Date& Date::operator-=(int day)
{
	if (day < 0) //假设 day 正数直接调用 日期 += 天数
	{
		return *this += day;
	}
	_day -= day;
	while (_day <= 0)
	{
		_month--;
		if (_month == 0)
		{
			_year--;
			_month = 12;
		}
		//
		_day -= GetMonthDay(_year, _month);
	}
	return *this;
}

//日期 - 天数
Date Date::operator-(int day)
{
	Date tmp = *this;
	tmp -= day;
	return tmp;
}

2.5 日期++ 和++日期 ;日期-- 、++日期

  1. 重置运算符时 这里要注意区分++前置和后置++;--前置和后置-- 也是如此;这么区分呢?在传参数的地方使用int 来标记后置++
  2. 这里的前置就是模拟++的过程,++前置 先++ ,后返回;那么就直接用 复用 += 然后返回*this
  3. 后置++ :先返回 后++****,实例化一个临时对象 先给*this 加上;但是tmp没有改变;直接返回对应结果
  4. 对于 前置--的逻辑类似
cpp 复制代码
Date& Date::operator++()
{
	*this += 1;
	return *this;
}
Date Date::operator++(int)
{
	Date tmp = *this;
	*this += 1;
	return tmp;
}

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

2.5两日期相减 日期 - 日期

  1. 最开始用假设法,判断那个更大;一定是大的 减去 小的;为后面循环更方便
  2. 这里flag的作用是当min大于max时交换了,拿来判断正负数;
  3. 假设 max的日期小于 min,那么就需要交换;竟然min值更大,最开始的max - min肯定是负数;最后乘一个负数就合理了
  4. max != min ,ret 是最后的返回差的天数,那么 min++主要让其和max相等计算出的差值
cpp 复制代码
//两个日期相减
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 ret = 0;
	while (max != min)
	{
		ret++;
		++min;
	}
	ret *= flag;
	return ret;
}

2.6输入输出重载

1.bool Date::ChackDate();防止月份和天数超过最大值和最小值

2.对于输入输出重载,根据现在的知识简单看;要是深入了解需要的知识不够;

cpp 复制代码
//输出重载
ostream& operator<<(ostream& out,const Date& d)
{
	out << d._year << "-" << d._month << "-" << d._day;
	return out;
}
istream& operator>>(istream& in,Date& d)
{
	while (1)
	{
		cout << "输入时请用空格分割" << endl;
		in >> d._year>> d._month >> d._day;
		if (!d.ChackDate())
		{
			cout << "非法输入" << endl;
			cout << d << endl;
		}
		else
		{
			break;
		}
	}
	return in;
}

bool Date::ChackDate()
{
	//不能大于当前月份
	if (_day < 1 || _day > GetMonthDay(_year,_month) || _month > 12 || _month < 1)
	{
		return false;
	}
	else
	{
		return true;
	}
}

3.test.cpp 测试接口

1.自己可根据接口测试对应功能,对于最后的程序很重要;

cpp 复制代码
#include "Date.h"
//int main()
//{
//	Date d1(2024, 9, 17);
//	Date d2(2024, 1, 1);
//
//	cout << d1 - d2 << endl;
//
//	cout << (d1++) << endl;
//
//	cout << d1 << endl;
//	cout << d1.GetMonthDay(2024,33) << endl;
//	return 0; 
//}

int main()
{
	//Date d1(2023,12,12);
	//Date d2(2024,9,19);
	//Date tmp(2004,12,10);
	//cout << d1 << endl;

	两个已存在对象,赋值重载
	//d1 = d2;

	//cout << d1 << endl;

	赋值拷贝
	//Date d3 = tmp;
	//cout << d3 << endl;

	cin >> d3;
	cout << d3 << endl;


	//Date* dd1 = new Date();
	//Date* dd2 = new Date(2022,2,12);

	//dd1 = dd2;
	//cout << *dd1 << endl;

	Date dd3(2023, 12, 1);
	cout << dd3 + 30 << endl;
	return 0;
}

抛下懒惰,重振旗鼓;继续坚持

相关推荐
循环渐进Forward13 分钟前
【C++笔试强训】如何成为算法糕手Day2
开发语言·数据结构·c++·算法·哈希算法·笔试·牛客
无名之逆1 小时前
《探索云原生与相关技术》
大数据·c++·git·云原生·面试·职场和发展
qmx_071 小时前
MFC - 复杂控件_2
c++·mfc
湫兮之风2 小时前
C++:使用tinyxml2获取节点下元素
xml·c++
向上爬的卓卓卓2 小时前
C++【类和对象】(构造函数与析构函数)
java·开发语言·c++·visual studio
DXCcn2 小时前
「DAOI R1」Magic
c++
@liu6662 小时前
码蹄集 数树
数据结构·c++·算法
小米里的大麦3 小时前
【C++】关键字auto详解
c++·笔记·auto·自动识别类型
好想有猫猫3 小时前
【Git】远程仓库
linux·c++·git·ubuntu
program-learner3 小时前
日志系统第二弹:设计模式介绍,C和C++不定参函数的介绍
c++·设计模式·项目·日志系统