C++系列-日期类的实现

 🌈个人主页:羽晨同学

💫个人格言:“成为自己未来的主人~”  

日期类的实现

首先我们先来看看日期类实现的代码

#pragma once
#include<iostream>
using namespace std;
#include<assert.h>
class Date
{
public:
	Date(int year = 2024, int month = 5, int day = 17);
	void Print();

	int GetMonthDay(int year, int month)
	{
		assert(month > 0 && month < 13);
		static int monthDayArray[13] = { -1,31,28,31,30,31,30,31,31,30,31,30,31 };
		if (month == 2 && (year % 4 == 0 && year % 100 != 0))
		{
			return 29;
		}
		else
		{
			return monthDayArray[month];
		}
	}
	bool CheckDate();
	bool operator<(const Date& d);
	bool operator<= (const Date & d);
	bool operator>(const Date& d);
	bool operator>=(const Date& d);
	bool operator==(const Date& d);
	bool operator!= (const Date & d);
	
	Date& operator+=(int day);
	Date operator+(int day);

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

	int operator-(const Date& d);
	Date& operator++();

	Date operator++(int);

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

private:
	int _year;
	int _month;
	int _day;
};
#define _CRT_SECURE_NO_WARNINGS
#include"Date.h"
bool Date::CheckDate()
{
	if (_month < 1 || _month>12
		|| _day<1 || _day>GetMonthDay(_year, _month))
	{
		return false;
	}
	else
	{
		return true;
	}
}
Date::Date(int year, int month, int day)
{
	_year = year;
	_month = month;
	_day = day;
	if (!CheckDate())
	{
		cout << "日期非法" << endl;
	}
}
void Date::Print()
{
	cout << _year << "-" << _month << "-" << _day << endl;
}
bool Date::operator<(const Date& d)
{
	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 true;
}
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 _year == d._year
		&& _month == d._month
		&& _day == d._day;
}
bool Date::operator>(const Date& d)
{
	return !(*this <= d);
}
bool Date::operator!=(const Date& d)
{
	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)
{
	Date tmp = *this;
	tmp += day;
	return tmp;
}

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

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;
}
int Date::operator-(const Date& d)
{
	Date max = *this;
	Date min = d;
	int flag = 1;
	if (*this < d)
	{
		max = d;
		min = *this;
		flag = -1;
	}
	int n = 0;
	while (min != max)
	{
		++min;
		++n;
	}
	return n*flag;
}
#define _CRT_SECURE_NO_WARNINGS
#include"Date.h"
//class Date
//{
//public:
//	Date(int year = 2024, int month = 5, int day = 17)
//	{
//		_year = year;
//		_month = month;
//		_day = day;
//	}
//	//前置++
//	Date& operator++()
//	{
//		_day += 1;
//		return *this;
//	}
//	//后置++
//	Date operator++(int)
//	{
//		Date tmp(*this);
//		_day += 1;
//		return tmp;
//	}
//	void Print()
//	{
//		cout << _year << "-" << _month << "-" << _day << endl;
//	}
//private:
//	int _year;
//	int _month;
//	int _day;
//};
//int main()
//{
//	Date d1(2024, 1, 1);
//	Date d2(2024, 1, 1);
//	Date d3=d1++;
//	Date d4=++d2;
//	d3.Print();
//	d4.Print();
//
//	return 0;
//}

void TestDate1()
{
	Date d1(2024, 4, 14);
	Date d2 = d1 + 30000;
	d1.Print();
	d2.Print();
	//int n = (d2 - d1);
	//cout << n;
}
void TestDate2()
{
	Date d1(2024, 4, 14);
	Date d2(2034, 4, 14);
	int n = d1 - d2;
	cout << n << endl;

}
void TestDate3()
{
	Date d1(2024, 1, 1);
	Date d2(2024, 2, 4);
	//cout<<(d1 > d2)<<endl;
	d1 < d2;
}

int main()
{
	//TestDate1();
	//TestDate2();
	TestDate3();
	return 0;
}

上面是日期类的全部实现代码,下面我们讲解一下这些功能背后的实现逻辑是什么?

首先,我们先来到,那几个判断大小功能的代码部分

bool Date::operator<(const Date& d)
{
	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 true;
}
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 _year == d._year
		&& _month == d._month
		&& _day == d._day;
}
bool Date::operator>(const Date& d)
{
	return !(*this <= d);
}
bool Date::operator!=(const Date& d)
{
	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)
{
	Date tmp = *this;
	tmp += day;
	return tmp;
}

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

在这几个功能当中,其实实现逻辑十分相似,我们可以总共分为两类,一个是-和-=,一个是+和+=,这两个部分

在这其中更为重要的是-=和+=的实现逻辑,因为要涉及到是否需要进位的操作,

在这前面,我们用数组,在数组当中放入了全年每个月的天数,用来进行比较。

然后后面的+和-,就运用了+=和-=的操作逻辑。

而-=和+=由于是直接对变量进行操作,所以我们可以用&来进行,可以提高效率。

下面的这个部分是前置++和后置++,前置--和后置--,这几个操作,我们在上一篇文章中有详细讲解,大家可以看看。

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

最后一部分是两个日期相减所得到的天数。

int Date::operator-(const Date& d)
{
	Date max = *this;
	Date min = d;
	int flag = 1;
	if (*this < d)
	{
		max = d;
		min = *this;
		flag = -1;
	}
	int n = 0;
	while (min != max)
	{
		++min;
		++n;
	}
	return n*flag;
}

在这个当中,需要关注的重点是flag的设立,大家可以体会一下其中的妙处。

好了,我们本期的文章就到这里。

下一篇我们再见。

 

 

 

 

 

 

 

 

  • 10
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C++线性表是一种数据结构,用于存储和操作一系列元素。而计算工作日天数可以通过线性表来实现。 在C++中,可以使用数组或者向量(vector)来实现线性表。你可以将日期作为元素存储在线性表中,并编写相应的算法来计算工作日天数。 以下是一个简单的示例代码,用于计算两个日期之间的工作日天数: ```cpp #include <iostream> #include <vector> // 判断是否为工作日 bool isWorkday(int year, int month, int day) { // 在这里编写判断逻辑,判断给定的日期是否为工作日 // 返回true表示是工作日,返回false表示不是工作日 } // 计算两个日期之间的工作日天数 int calculateWorkdays(int startYear, int startMonth, int startDay, int endYear, int endMonth, int endDay) { int workdayCount = 0; // 将起始日期和结束日期转换为整数表示,方便计算 int startDate = startYear * 10000 + startMonth * 100 + startDay; int endDate = endYear * 10000 + endMonth * 100 + endDay; // 遍历起始日期到结束日期之间的每一天 for (int date = startDate; date <= endDate; date++) { int year = date / 10000; int month = (date % 10000) / 100; int day = date % 100; // 判断当前日期是否为工作日 if (isWorkday(year, month, day)) { workdayCount++; } } return workdayCount; } int main() { // 示例:计算2022年1月1日到2022年12月31日之间的工作日天数 int workdays = calculateWorkdays(2022, 1, 1, 2022, 12, 31); std::cout << "工作日天数:" << workdays << std::endl; return 0; } ``` 在上述示例代码中,`isWorkday`函数用于判断给定的日期是否为工作日,你可以根据实际需求编写判断逻辑。`calculateWorkdays`函数用于计算两个日期之间的工作日天数,它通过遍历起始日期到结束日期之间的每一天,并调用`isWorkday`函数来判断是否为工作日,最后返回工作日天数。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值