【C++ Primer Plus习题】11.6

大家好,这里是国中之林!
❥前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家。点击跳转到网站。有兴趣的可以点点进去看看←

问题:

这里是引用

解答:
main.cpp

#include <iostream>
#include "Stonewt.h"
using namespace std;
const int SIZE = 6;

int main()
{
	Stonewt stone_arr[SIZE] = { 253.6,Stonewt(8,0.35),Stonewt(23,0) };
	double input;
	Stonewt eleven = Stonewt(11, 0.0);
	Stonewt max = stone_arr[0];
	Stonewt min = stone_arr[0];
	int num = 0;

	for (int i = 3; i < SIZE; i++)
	{
		cout << "请输入第" << i + 1 << "个元素的信息,用磅:";
		cin >> input;
		stone_arr[i] = Stonewt(input);
	}

	for (int i = 0; i < SIZE; i++)
	{
		if (max < stone_arr[i])max = stone_arr[i];
		if (min > stone_arr[i])min = stone_arr[i];
		if (stone_arr[i] > eleven)num++;
	}
	cout << "max: " << max<<endl;
	cout << "min: " << min << endl;
	cout << "大于11的数量为:" << num << endl;

	return 0;
}


Stonewt.h

#pragma once
#include <iostream>
using namespace std;
class Stonewt
{
public:
	enum Mode { YIN, INT, FLOAT };
private:
	enum { Lbs_per_stn = 14 };
	int stone;
	double pds_left;
	double pounds;
	Mode mode;

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

	void setMode(Mode m);
	Stonewt operator+(const Stonewt& s);
	Stonewt operator-(const Stonewt& s);
	Stonewt operator*(double n);

	bool operator>(const Stonewt& s);
	bool operator>=(const Stonewt& s);
	bool operator<(const Stonewt& s);
	bool operator<=(const Stonewt& s);
	bool operator!=(const Stonewt& s);
	bool operator==(const Stonewt& s);

	friend ostream& operator<<(ostream& os, const Stonewt& s);
	friend Stonewt operator*(double m, Stonewt& s) { return s * m; }
};




Stonewt.cpp

#include "Stonewt.h"
#include <iostream>
using namespace std;

Stonewt::Stonewt(double lbs)
{
	stone = int(lbs) / Lbs_per_stn;
	pds_left = int(lbs) % Lbs_per_stn + lbs - int(lbs);
	pounds = lbs;
	mode = INT;
}
Stonewt::Stonewt(int stn, double lbs)
{
	stone = stn;
	pds_left = lbs;
	pounds = stn * Lbs_per_stn + lbs;
	mode = FLOAT;
}
Stonewt::Stonewt()
{
	stone = pounds = pds_left = 0;
	mode = YIN;
}
Stonewt::~Stonewt()
{

}

void Stonewt::setMode(Mode m)
{
	this->mode = m;
}
Stonewt Stonewt::operator+(const Stonewt& s)
{
	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.mode = this->mode;
	return temp;
}
Stonewt Stonewt::operator-(const Stonewt& s)
{
	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.mode = this->mode;
	return temp;
}
Stonewt Stonewt::operator*(double n)
{
	Stonewt temp;
	temp.pounds = pounds * n;
	temp.stone = temp.pounds / Lbs_per_stn;
	temp.pds_left = int(temp.pounds) % Lbs_per_stn + temp.pounds - int(temp.pounds);
	temp.mode = this->mode;
	return temp;
}

ostream& operator<<(ostream& os, const Stonewt& s)
{
	if (s.mode == Stonewt::YIN)
	{
		double st = s.stone + s.pds_left / Stonewt::Lbs_per_stn;
		os << st << " stone\n";
	}
	if (s.mode == Stonewt::INT)
	{
		os << s.pounds << " pounds\n";
	}
	if (s.mode == Stonewt::FLOAT)
	{
		os << s.stone << " stone, " << s.pds_left << " pounds\n";
	}
	return os;
}

bool Stonewt::operator>(const Stonewt& s)
{
	return pounds > s.pounds;
}
bool Stonewt::operator>=(const Stonewt& s)
{
	return pounds >= s.pounds;
}
bool Stonewt::operator<(const Stonewt& s)
{
	return pounds < s.pounds;
}
bool Stonewt::operator<=(const Stonewt& s)
{
	return pounds <= s.pounds;
}
bool Stonewt::operator!=(const Stonewt& s)
{
	return pounds != s.pounds;
}
bool Stonewt::operator==(const Stonewt& s)
{
	return pounds == s.pounds;
}

运行结果:
在这里插入图片描述

考查点:

  • 比较运算符重载

2024年9月5日20:19:29

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值