C++学习系列(11)——日期类的完整实现(附源码)

前言

这篇文章是对前面知识的综合应用,加深了类的各种知识点的理解。其中会涉及:构造函数析构函数和拷贝构造函数操作符重载友元const成员this指针等等知识点,如果有不清楚的小伙伴欢迎浏览之前的文章,希望可以给你带来帮助。下面废话不多说,直接附源码。

头文件

#include <iostream>
#include <assert.h>
using namespace std;

//类里面的函数都是内联函数,但是因为是声明和定义分开所以就无法使用内联
//所以代码量比较小的函数建议直接写出来
class Date
{
	//友元:授予该函数访问类的权限
	friend ostream& operator<<(ostream& out, const Date& d);
	friend istream& operator>>(istream& in, Date& d);

public:
	//构造函数
	Date(int year = 2023, int month = 7, int day = 22);
	//拷贝构造
	Date(const Date& d);
	//析构构造
	~Date();
	void Print();
	int GetMonthDay(int year, int day);
	int GetYearDay(int year);
	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);   //d1 + d2  ->  d1.operator+(d2)
	Date& operator+=(int day);
	Date operator-(int day);
	Date& operator-=(int day);
	int operator-(Date& d);
	Date& operator++();
	Date operator++(int);
	Date& operator--();
	Date operator--(int);

private:
	int _year;
	int _month;
	int _day;
};
//cout << d  --> cout.operator(d)
//运算符重载中
//重载运算符函数的参数数量与该运算符作用的运算对象数量一样多。
//一元运算符有一个参数,二元运算符有两个。
//对于二元运算符来说,左侧运算对象传递给第一个参数
//而右侧运算对象传递给第二个参数。

//可以用内联函数,提高效率
inline ostream& operator<<(ostream& out, const Date& d)
{
	out << d._year << "年" << d._month << "月" << d._day << "日" << endl;
	return out;
}

inline istream& operator>>(istream& in, Date& d)
{
	in >> d._year >> d._month >> d._day;
	return in;
}

源文件

#include "Date.h"

//构造函数
Date::Date(int year, int month, int day)
{
	if (year < 0 || (month > 12 || month < 0) || day > GetMonthDay(year, month))
	{
		cout << "输入不符合规范" << endl;
		exit(-1);
	}
	else
	{
		_year = year;
		_month = month;
		_day = day;
	}
}
//拷贝构造
Date::Date(const Date& d)
{
	_year = d._year;
	_month = d._month;
	_day = d._day;
}
//析构函数
Date::~Date()
{
	_year = 0;
	_month = 0;
	_day = 0;
}
//打印
void Date::Print()
{
	cout << _year << "年" << _month << "月" << _day << "日" << endl;
}

int Date::GetMonthDay(int year, int month)
{
	int day[] = { 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 day[month];
	}
}

int Date::GetYearDay(int year)
{
	if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
		return 366;
	else
		return 365;
}

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)
{
	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)
{
	return (*this == d) || (*this > d);
}

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

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

//类 += 天数,返回一个类。
//2023 7 27
//       80
//2023 7 107
Date& Date::operator+=(int day)
{
	if (day < 0)
	{
		*this -= -day;
		return *this;
	}
	_day += day;
	while (_day > GetMonthDay(_year, _month))
	{
		_day -= GetMonthDay(_year, _month);
		_month++;
		if (_month > 12)
		{
			_month = 1;
			_year++;
		}
	}
	return *this;
}

Date Date::operator+(int day)
{
	Date tmp = *this;
	tmp += day;
	return tmp;
}

Date& Date::operator-=(int day)
{
	if (day < 0)
	{
		*this += -day;
		return *this;
	}
	_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;
}


int Date::operator-(Date& d)
{
	Date max = *this;
	Date min = d;
	int flag = 1;
	if(d < *this)  // ---> d.operator<(*this);
	if (*this < d)
	{
		max = d;
		min = *this;
		flag = -1;
	}
	int n = 0;
	while (min != max)
	{
		++min;
		++n;
	}
	return n * flag;
}
//++d  -> ++前置
//++d  -> d1.operator()
Date& Date::operator++()
{
	*this += 1;
	return *this;
}


//d++  -> 后置++
//d++ -> d1.operator(int)
//参数没有实际意义,仅仅是占位,只是为了构成重载
Date Date::operator++(int)
{
	Date tmp = *this;
	*this += 1;
	return tmp;
}

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

//d--  后置--
Date Date::operator--(int)
{
	Date tmp;
	*this -= 1;
	return tmp;
}
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
数据预处理中,概率分布形式常用于对数据进行归一化处理。下面是一个C++带类的完整实现及案例: ```cpp #include <iostream> #include <vector> #include <cmath> using namespace std; class ProbabilityDistribution { private: vector<double> data; // 数据 double mean = 0; // 均值 double variance = 0; // 方差 public: // 构造函数 ProbabilityDistribution(vector<double> data) { this->data = data; calculateMean(); calculateVariance(); } // 计算均值 void calculateMean() { double sum = 0; for (double d : data) { sum += d; } mean = sum / data.size(); } // 计算方差 void calculateVariance() { double sum = 0; for (double d : data) { sum += pow(d - mean, 2); } variance = sum / data.size(); } // 获取均值 double getMean() { return mean; } // 获取方差 double getVariance() { return variance; } // 归一化处理 vector<double> normalize() { vector<double> result; for (double d : data) { result.push_back((d - mean) / sqrt(variance)); } return result; } }; int main() { vector<double> data = {1.0, 2.0, 3.0, 4.0, 5.0}; ProbabilityDistribution pd(data); cout << "Mean: " << pd.getMean() << endl; cout << "Variance: " << pd.getVariance() << endl; vector<double> normalizedData = pd.normalize(); for (double d : normalizedData) { cout << d << " "; } cout << endl; return 0; } ``` 在上面的代码中,ProbabilityDistribution是一个概率分布类,包含了数据、均值、方差和相关的计算方法。在main函数中,我们创建了一个包含五个元素的数据向量,然后使用这个向量创建了一个ProbabilityDistribution对象。我们可以使用getMean和getVariance方法获取均值和方差,使用normalize方法将数据进行归一化处理。 注意,这里的归一化处理使用的是Z-score标准化方法,即将每个数据减去均值,再除以标准差。这样处理后,数据的均值为0,标准差为1。 运行上面的代码,输出结果如下: ``` Mean: 3 Variance: 2 -1.41421 -0.707107 0 0.707107 1.41421 ``` 可以看到,均值为3,方差为2,归一化后的数据已经变成了均值为0,标准差为1的形式。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值