C++ Primer Plus 第11章 习题5 答案

本文介绍了一个名为Stonewt的C++类的实现细节,该类用于表示和处理重量单位,支持石头(stone)和磅(pound)之间的转换及运算。通过不同的构造函数初始化重量,并支持加法、减法和乘法等操作符重载。此外,还展示了如何设置显示样式并输出重量的不同格式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#ifndef STONEWT_H_
#define STONEWT_H_

#include <iostream>

class Stonewt
{
public:
	enum class Style{STONE, POUNDS, FLOATPOUNDS};

private:
	static const int LBS_PER_STN{ 14 };
	int stone;
	double pds_left;
	double pounds;
	Style style;

public:
	Stonewt(double lbs);
	Stonewt(int stn, double lbs);
	Stonewt();
	~Stonewt();

	void Set_Style(Style m);
	Stonewt operator+(const Stonewt& s) const;
	Stonewt operator-(const Stonewt& s) const;
	Stonewt operator* (double multi) const;

	friend Stonewt operator*(double multi, const Stonewt& s);
	friend std::ostream& operator<<(std::ostream& os, const Stonewt& stone);
};

#endif


#include <iostream>
#include "ex11-5.h"

Stonewt::Stonewt(double lbs)
{
	stone = int(lbs) / LBS_PER_STN;
	pds_left = int(lbs) % LBS_PER_STN + lbs - int(lbs);
	pounds = lbs;
	style = Style::POUNDS;
}


Stonewt::Stonewt(int stn, double lbs)
{
	stone = stn;
	pds_left = lbs;
	pounds = stn * LBS_PER_STN + lbs;
	style = Style::FLOATPOUNDS;
}


Stonewt::Stonewt()
{
	stone = 0;
	pounds = 0;
	pds_left = 0;
	style = Style::STONE;
}


Stonewt::~Stonewt()
{

}

void Stonewt::Set_Style(Style m)
{
	style = m;
}


Stonewt Stonewt::operator+(const Stonewt& s) const
{
	Stonewt temp;
	temp.pounds = pounds + s.pounds;
	temp.stone = temp.pounds / LBS_PER_STN;
	temp.pds_left = int(temp.pounds) % LBS_PER_STN + temp.pounds - int(temp.pounds);
	temp.style = this->style;
	return temp;
}

Stonewt Stonewt::operator-(const Stonewt& s) const
{
	Stonewt temp;
	temp.pounds = pounds - s.pounds;
	temp.stone = temp.pounds / LBS_PER_STN;
	temp.pds_left = int(temp.pounds) % LBS_PER_STN + temp.pounds - int(temp.pounds);
	temp.style = this->style;
	return temp;
}


Stonewt Stonewt::operator* (double multi) const
{
	Stonewt temp;
	temp.pounds = pounds * multi;
	temp.stone = temp.pounds / LBS_PER_STN;
	temp.pds_left = int(temp.pounds) % LBS_PER_STN + temp.pounds - int(temp.pounds);
	temp.style = this->style;
	return temp;
}


Stonewt operator*(double multi, const Stonewt& s)
{
	return s * multi;
}


std::ostream& operator<<(std::ostream& os, const Stonewt& s)
{

	if (s.style == Stonewt::Style::POUNDS)
	{
		os << s.pounds << " stone " << std::endl;
	}

	if (s.style == Stonewt::Style::STONE)
	{
		double st = s.stone + s.pds_left / Stonewt::LBS_PER_STN;
		os << st << " stone " << std::endl;
	}

	if (s.style == Stonewt::Style::FLOATPOUNDS)
	{
		os << s.stone << " stone " << s.pds_left << " pds_left " << std::endl;
	}

	return os;
}


#include <iostream>
#include "ex11-5.h"

int main()
{
	Stonewt tiger = 275;
	std::cout << "tiger: " << tiger << std::endl;
	Stonewt wolfe(285.7);
	std::cout << "wolfe: " << wolfe << std::endl;
	Stonewt taft(21, 8);
	std::cout << "taft: " << taft << std::endl;
	std::cout << "**********************"<< std::endl;

	tiger = 276.8;
	std::cout << "tiger: " << tiger << std::endl;
	std::cout << "**********************" << std::endl;

	std::cout << "wolfe * 2.3: " << wolfe * 2.3 << std::endl;
	taft = tiger + wolfe + 200;
	std::cout << "taft: " << taft << std::endl;
	wolfe.Set_Style(Stonewt::Style::FLOATPOUNDS);
	wolfe = wolfe * 2.3;

	std::cout << "wolfe * 2.3: " << wolfe << std::endl;

	wolfe =  10 * wolfe;
	std::cout << "10 * wolfe: " << wolfe << std::endl;


	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值