• 【C++】日期类的实现


    👉前言👈

    在前三篇的类和对象的博客中,本人已经讲解了类和对象的全部知识点。这些知识点都是通过我们比较熟悉的日期类Date、栈Stack和队列MyQueue来讲解的,那么本篇文章就将日期类的实现总结一下。

    👉Date.h👈

    头文件Date.h里主要放着头文件的包含、类型的声明、函数的声明。日期类主要需要实现的函数有构造函数(注:析构函数和拷贝构造函数不需要自己写,编译器默认生成的就够用了)、运算符重载、计算日期是一年中的第几天以及将一年中的第几天转换成日期。

    主要:在类中定义的成员函数会被当做内联函数处理,我们可以将被频繁调用的小函数在类中定义,不经常被调用的函数在类外定义。因为流插入<<和流提取>>的重载在类中定义,可读性不高,不符合使用习惯,所以我们利用友元将这两个函数重载在类外定义。

    #pragma once
    #include 
    using namespace std;
    
    class Date
    {
    public:
    	// 友元声明(类的任意位置)
    	friend ostream& operator<<(ostream& out, const Date& d);
    	friend istream& operator>>(istream& in, Date& d);
    
    	int GetMonthDay(int year, int month)
    	{
    		static int monthDayArray[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;
    		}
    		else
    		{
    			return monthDayArray[month];
    		}
    	}
    
    	Date(int year = 1, int month = 1, int day = 1)
    	{
    		_year = year;
    		_month = month;
    		_day = day;
    
    		// 检查日期是否合法
    		if (!(year >= 1
    			&& (month >= 1 && month <= 12)
    			&& (day >= 1 && day <= GetMonthDay(year, month))))
    		{
    			cout << "非法日期" << endl;
    		}
    	}
    
    	void Print() const
    	{
    		cout << _year << '/' << _month << '/' << _day << endl;
    	}
    
    	bool operator==(const Date& d) const;
    	// d1 > d2
    	bool operator>(const Date& d) const;
    	// d1 >= d2
    	bool operator>=(const Date& d) const;
    	bool operator<=(const Date& d) const;
    	bool operator<(const Date& d) const;
    	bool operator!=(const Date& d) const;
    
    	// d1 += 100
    	Date& operator+=(int day);
    
    	// d1 + 100
    	Date operator+(int day) const;
    
    	// d1 -= 100
    	Date& operator-=(int day);
    
    	// d1 - 100
    	Date operator-(int day) const;
    
    	// 前置
    	Date& operator++();
    
    	// 后置
    	Date operator++(int);
    
    	// 前置
    	Date& operator--();
    
    	// 后置
    	Date operator--(int);
    
    	// d1 - d2
    	int operator-(const Date& d) const;
    
    	Date* operator&()
    	{
    		return this;
    		// 要求这个类的对象不让取地址
    		//return nullptr;
    	}
    
    	const Date* operator&() const
    	{
    		return this;
    		//return nullptr; 
    	}
    
    	// 计算日期是一年中的第几天
    	int DayOfYear() const;
    
    private:
    	int _year;
    	int _month;
    	int _day;
    };
    
    // operator<<(cout, d1) cout<
    inline ostream& operator<<(ostream& out, const Date& d)
    {
    	out << d._year << "年" << d._month << "月" << d._day << "日" << endl;
    	return out;
    }
    // cin >> d1  operator(cin, d1)
    inline istream& operator>>(istream& in, Date& d)
    {
    	in >> d._year >> d._month >> d._day;
    	return in;
    }
    
    // 将一年中的第几天转换成日期
    void DaysToDate();
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117

    👉Date.cpp👈

    日期类需要实现的函数大多数是运算符重载,这些运算符重载在类和对象(中)这篇文章里讲过。如果还有不懂,可以点击跳转学习一下。运算符重载最重要的就是学会赋用已经写好的运算符重载,这样可以达到事半功倍的效果。

    #include "Date.h"
    
    bool Date::operator==(const Date& d) const
    {
    	return _year == d._year
    		&& _month == d._month
    		&& _day == d._day;
    }
    
    bool Date::operator>(const Date& d) const
    {
    	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) const
    {
    	return *this > d || *this == d;
    }
    
    bool Date::operator<=(const Date& d) const
    {
    	return !(*this > d);
    }
    
    bool Date::operator<(const Date& d) const
    {
    	return !(*this >= d);
    }
    
    bool Date::operator!=(const Date& d) const
    {
    	return !(*this == d);
    }
    
    Date& Date::operator+=(int day)
    {
    	if (day < 0)
    	{
    		return *this -= -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 ret(*this);
    	ret += day;
    	return ret;
    }
    
    Date& Date::operator-=(int day)
    {
    	if (day < 0)
    	{
    		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) const
    {
    	Date ret(*this);
    	ret -= day;
    	return ret;
    }
    
    // 前置
    Date& Date::operator++()
    {
    	*this += 1;
    	return *this;
    }
    
    // 后置 -- 多一个int参数主要是为了根前置区分
    // 构成函数重载
    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;
    }
    
    // 日期差值 d1 - d2
    int Date::operator-(const Date& d) const
    {
    	Date max = *this;
    	Date min = d;
    	int flag = 1;
    
    	if (*this < d)
    	{
    		max = d;
    		min = *this;
    		flag = -1;
    	}
    
    	int n = 0;
    	while (min != max)
    	{
    		++n;
    		++min;
    	}
    
    	return n * flag;
    }
    
    int Date::DayOfYear() const
    {
    	static int Day[13] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 };
    
    	int n = Day[_month - 1] + _day;
    
    	if (_month > 2 && ((_year % 4 == 0 && _year % 100 != 0) || (_year % 400 == 0)))
    		++n;
    
    	return n;
    }
    
    void DaysToDate()
    {
    	int year = 0;
    	int days = 0;
    	cin >> year >> days;
    
    	static int Day[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
    	if (((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
    		&& (days > 60))
    	{
    		--days;
    	}
    
    	int month = 1;
    	while (days > Day[month])
    	{
    		days -= Day[month];
    		month++;
    	}
    
    	printf("%04d-%02d-%02d\n", year, month, days);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196

    计算日期是一年中的第几天

    • 定义一个数组,数组里存储的是到这个月的月底是该年的第几天
    • 计算日期是一年中的第几天n = Day[_month - 1] + _day
    • 如果月份month大于 2 且年份year为闰年,则n++
    int Date::DayOfYear() const
    {
    	static int Day[13] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 };
    
    	int n = Day[_month - 1] + _day;
    
    	if (_month > 2 && ((_year % 4 == 0 && _year % 100 != 0) || (_year % 400 == 0)))
    		++n;
    
    	return n;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    将一年中的第几天转换成日期

    • 定义一个数组,数组中存储着平年每个月份的天数
    • 如果年份year为闰年且天数days大于 60,则--days
    • 定义月份month = 1,当days > Day[month]时,进入 while 循环,days -= Day[month] month++;当 while 循环结束时,就能求出monthdays
    • 注:1 <= days <= 366
    void DaysToDate()
    {
    	int year = 0;
    	int days = 0;
    	cin >> year >> days;
    
    	static int Day[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
    	if (((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
    		&& (days > 60))
    	{
    		--days;
    	}
    
    	int month = 1;
    	while (days > Day[month])
    	{
    		days -= Day[month];
    		month++;
    	}
    
    	printf("%04d-%02d-%02d\n", year, month, days);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    👉Test.cpp👈

    Test.cpp源文件主要负责测试函数接口实现得是否正确。验证函数接口都正确后,就可以写过菜单来和用户交互了。不过,本人不太推荐写菜单,所以我就没有写菜单了。如果大家有需求的话,也可以自己写一写菜单。

    #include "Date.h"
    
    void TestDate1()
    {
    	Date d1(2022, 11, 14);
    	Date d2(2022, 11, 15);
    	
    	cout << (d1 == d2) << endl;
    	cout << (d1 > d2) << endl;
    	cout << (d1 >= d2) << endl;
    	cout << (d1 <= d2) << endl;
    	cout << (d1 < d2) << endl;
    	cout << (d1 != d2) << endl;
    
    	cout << (d1 += 100);
    	cout << (d2 -= 100);
    
    	cout << d1.DayOfYear() << endl;
    
    	DaysToDate();
    }
    
    int main()
    {
    	TestDate1();
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    在这里插入图片描述

    在这里插入图片描述
    看完本篇博客,大家可以做一下下面几道 OJ 题来巩固自己所学到的知识哦。

  • 相关阅读:
    JavaScript强制转换数字类型并设置默认值为0
    新的centos7.9安装jenkins—(一)
    python可视化分析(五)-绘制边缘箱线图
    Linux命令(86)之touch
    css 特别样式记录
    联想电脑去掉开机广告 关闭开机广告 ideapad 去掉开机广告
    1.5、计算机网络的性能指标(1)
    PHP 做 Mysql 数据统计,通过时间戳 统计 每分钟多少条 每十分钟多少条?
    Kotlin Flow啊,你将流向何方?
    下载stm32f10x标准外设库
  • 原文地址:https://blog.csdn.net/m0_63639164/article/details/127842310