虫子 日期类 上 太子语言

举报
虫子VV 发表于 2022/06/23 00:18:39 2022/06/23
【摘要】 日期类 Date.h Date.cpp test.cpp 测试情况 功能添加 日期加天数后那个日期 日期加天数 日期++,++日期 前置++ 后置++ 日期减天数后那个日期 日期减天数 日期–,--日期 前置– 后置– 日期类我们要有一个习惯就是写代码或者是写项目的时候我们要==写一点编一点==,要养成良好的习惯,万万不可以写完了再编,然后出了一堆错误自己都不想看,我们先自己编个最小项目或...

日期类

我们要有一个习惯就是写代码或者是写项目的时候我们要==写一点编一点==,要养成良好的习惯,万万不可以写完了再编,然后出了一堆错误自己都不想看,我们先自己编个最小项目或者系统框架在加其他功能


Date.h

Date.h就是一些头文件,声明啥的

image-20220120135217626

#pragma once
#include <iostream>
using std::cout;
using std::cin;
using std::endl;

class Date
{
public:
	Date(int year = 0, int month = 1, int day = 1);
	void Print();
	//像析构,拷贝构造,赋值重载可以不需要写,因为默认生成的就够用了,
	//像Stack才需要自己写这三个

	//日期加 减天数
	Date operator+(const int& day);
	Date operator-(const int& day);
private:
	int _year;
	int _month;
	int _day;
};

Date.cpp

Date.cpp这里面就是写日期类的函数了

image-20220120140010670

#include "Date.h"

Date::Date(int year, int month, int day)
{
	_year = year;
	_month = month;
	_day = day;
}
void Date::Print()
{
	cout << _year << "年" << _month << "月" << _day << "日" << endl;
}

Date Date::operator+(const int& day)
{
	return *this;
}
Date Date::operator-(const int& day)
{
	return *this;
}

test.cpp

test.cpp

image-20220120140259451

#include "Date.h"

int main()
{
	Date d(2022,1,1);
	d.Print();
	return 0;
}

测试情况

image-20220120140650926


功能添加

当我们能基本丐版跑出来了那我们接下来的路还是要走的,我们要考虑到数据是否合理啊什么的

image-20220120141407902

==所以我们需要检查日期的合法性==

image-20220120191252815

==上面的确是排除了百分之99的滤掉了,但是闰年2月是特殊考虑的反而没有代码实现==

image-20220120200401862

image-20220120205730088

==我们可不可以优化一下,比如我就输错了一个日期你就直接程序死了是不是有点太霸道了,你应该抛出异常,或隔一下啥的==

image-20220120213917747

inline int GetMonthDay(int year, int month)
{
	//数组存放平年每个月的天数  刚好对应的下标是月 里面的元素是天
	static int dayArray[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
	//该月天数
	int day = dayArray[month];
	//闰年是4年一润百年不润或者四百年一润
	if (month == 2 
	&& (year % 4 == 0 && year % 100 != 0) 
	|| year % 400 == 0)
	{
		//闰年的2月是29天
		day = 29;
	}
	return day;
}
Date::Date(int year, int month, int day)
{
	//检查日期的合法性
	if (year > 0
	 && month > 0 && month <13
	 && day > 0 && day <= GetMonthDay(year,month))
	{
		_year = year;
		_month = month;
		_day = day;
	}
	else
	{
		cout << endl;
		cout << "非法日期" << endl;
		cout << year << "年" << month << "月" << day << "日" << endl;
	}
}
void Date::Print()
{
	cout << _year << "年" << _month << "月" << _day << "日" << endl;
}

日期加天数后那个日期

image-20220120224532776

image-20220120225026789

Date& Date::operator+=(const int& day)
{
	//我们先不管,我们先直接把天加上去
	_day += day;
	//然后再判断合不合法
	while (_day > GetMonthDay(_year,_month))
	{
		//先把当月的天数减掉
		_day -= GetMonthDay(_year, _month);
	    //然后月++
		_month++;
		//假如月也过了就年++
		if (_month > 12)
		{
			_year++;
			_month = 1;
		}
	}
	return *this;
}

日期加天数

==仅仅就是加没有赋值过去,要和上面那个分清了,所以我需要一个中间临时变量(这个我们叫临时对象)来存储运算后的数据==

image-20220120231955244

//日期加天数 不赋回去
Date Date::operator+(const int& day)
{
	//首先创建一个临时对象  先把之前的拷贝复制给他
	Date ret(*this);
	//复用+=
	ret += day;
	return ret;
}

日期++,++日期

==日期加加和加加日期都是++,但是operator运算符重载怎么区分呢,==

前置++

==前置++返回运算以后的值==

image-20220121112821927

//++d  日期前置++ 被转换成d.operator++(&d)
Date& Date::operator++()
{
	//返回运算后的值
	*this += 1;
	return *this;
}

后置++

==后置++返回运算前的值==

image-20220121113138932

//d++  日期后置++ 被转换成d.operator++(&d,0)
//这里的int仅仅是占位,不需要给实参,起到函数重载的作用
Date& Date::operator++(int)
{
	//后置++返回运算前的值
	//所以需要一个临时对象先存起来
	Date tmp(*this);
	*this += 1;
	return tmp;
}

日期减天数后那个日期

image-20220121005942011

image-20220121004438291

//日期减天数同时赋回去
Date& Date::operator-=(const int& day)
{
	//啥也不多说,先把天减掉
	_day -= day;
	//不合法就等他合法
	while (_day <= 0)
	{
		//先把月减了
		_month--;
		//先判断月是不是零,是的话就操作年了
		if (_month <= 0)
		{
			_year--;
			_month = 12;
		}
		//然后让他加上正确的月的天数
		_day += GetMonthDay(_year,_month);		
	}
	return *this;
}

日期减天数

==复用上面那个函数就行==

image-20220121091205743

//日期减天数 不赋回去
Date Date::operator-(const int& day)
{
	Date ret(*this);
	//-=的复用
	ret -= day;
	return ret;
}


日期–,--日期

==日期减减和减减日期都是–,但是operator运算符重载怎么区分呢,==

前置–

==前置–返回运算以后的值==

image-20220121114232992

//前置减减
Date& Date::operator--()
{
	*this -= 1;
	return *this;
}

后置–

==后置–返回运算前的值==

image-20220121114447971

//后置减减
Date& Date::operator--(int)
{
	Date tmp(*this);
	*this -= 1;
	return tmp;
}
【版权声明】本文为华为云社区用户原创内容,未经允许不得转载,如需转载请自行联系原作者进行授权。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。