【C++ Primer Plus习题】11.6

news2024/9/21 4:25:29

问题:

这里是引用

解答:
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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2108481.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

C++特殊类设计,

目录 ​编辑 一、不能拷贝的类 二、只能在栈上创建的类&#xff0c; 三、只能在堆上创建的类&#xff0c; 四、不能被继承的类&#xff0c; 五、只有一个对象的类 一、不能拷贝的类 c98&#xff1a;将拷贝构造和运算符重载私有化 C11&#xff1a;delete删除成员函数 /…

[数据集][目标检测]电梯内广告牌电动车检测数据集VOC+YOLO格式2787张4类别

数据集格式&#xff1a;Pascal VOC格式YOLO格式(不包含分割路径的txt文件&#xff0c;仅仅包含jpg图片以及对应的VOC格式xml文件和yolo格式txt文件) 图片数量(jpg文件个数)&#xff1a;2787 标注数量(xml文件个数)&#xff1a;2787 标注数量(txt文件个数)&#xff1a;2787 标注…

2024-09-03升级问题记录:编译打包出错java.lang.OutOfMemoryError: GC overhead limit exceeded

Android工程升级&#xff0c;debug构建apk可以正常生成&#xff0c;但是release构建时出现以上错误。 按字面意思&#xff0c;GC回收时内存不够。 那么&#xff0c;解决方法&#xff1a; 在在项目根目录的gradle.properties中&#xff0c;注意不是Administrator\.gradle 的 …

linux中vim常用命令大全详细讲解

​ 大家好&#xff0c;我是程序员小羊&#xff01; 前言&#xff1a; Vim 是 Linux 系统中功能强大的文本编辑器&#xff0c;广泛用于代码编辑和配置文件的编辑。以下是 Vim 常用命令的详细讲解&#xff0c;包括基本操作、文本编辑、查找与替换、窗口操作等。 一、Vim 基本操…

Epoll 用法

Epoll 监听 EPOLL_CTL_DEL EPOLL_CTL_ADD epoll_event event event.events event.data.fd

初识redis(String,Hash,List,Set,SortedSet)

认识NoSql sql关系型数据库 nosql非关系型数据库 nosql具有非结构化&#xff0c;Key/Value&#xff0c;Document&#xff0c;Draph 无关联的&#xff0c;非sql&#xff0c;BASE&#xff08;原子性&#xff0c;持久性&#xff0c;一致性&#xff0c;隔离性&#xff09; 认识r…

盘点4款超好用的电脑数据恢复工具

如今我们的生活和工作越来越离不开电脑&#xff0c;因此电脑里面也储存了我们大量的数据&#xff1a;图片&#xff0c;文档&#xff0c;工作资料等。但是这些重要数据却会因为误删除、病毒攻击、系统崩溃等原因遭到丢失。所以数据恢复软件就变得十分的重要&#xff0c;今天&…

maya无法导出fbx

很多小伙伴使用Maya软件导出fbx格式文件时会提示“无法识别文件类型”&#xff0c;这主要是因为没有开启fbx插件造成的&#xff0c;只要开启fbx插件就能解决这个问题。 在Maya软件的“Modeling”模式下&#xff0c;点击菜单栏的“Windows”&#xff0c;在弹出的下拉框中选择“S…

Altium AD20差分对走线设置方法

Altium AD20差分对走线设置方法 1、在PCB界面右下角点击panels&#xff0c;选择"PCB"选项&#xff0c;则左下角就会出现PCB的窗口选项 2、在PCB窗口内&#xff0c;将上面的下拉菜单选择Differential Pairs Editor选项&#xff0c;然后按下图操作 3、增加差分网络分…

服务器数据恢复—OneFS文件系统下数据被删除的数据恢复案例

服务器数据恢复环境&故障&#xff1a; EMC NAS&#xff08;Isilon S200&#xff09;&#xff0c;共3个节点&#xff0c;每个节点配置12块STAT硬盘。数据分两部分&#xff1a;一部分数据为vmware虚拟机&#xff08;WEB服务器&#xff09;&#xff0c;通过NFS协议共享到ESX主…

【数据库】MySQL表的基本查询

关于表的增删查改主要分为CRUD&#xff1a;Create(创建), Retrieve(读取)&#xff0c;Update(更新)&#xff0c;Delete&#xff08;删除&#xff09; 目录 1.Creat&#xff08;增加内容&#xff09; 1.1指定列插入 1.2全列插入 1.3多行插入 1.4插入冲突更新 1.5替换 2.R…

Springboot3.x.x使用SpringSecurity6(一文包搞定)

SpringSecurity6 什么是SpringSecurity? Spring Security 是一个强大的、高度可定制的身份验证&#xff08;Authentication&#xff09;和访问控制&#xff08;Authorization&#xff09;框架。它是 Spring 框架家族的一员&#xff0c;主要用于保护基于 Java 的应用程序&#…

鸿蒙开发有必要学吗

在科技飞速发展的今天&#xff0c;新的技术不断涌现&#xff0c;开发者们也面临着众多选择。其中&#xff0c;鸿蒙开发成为了一个备受关注的话题。那么&#xff0c;鸿蒙开发有必要学吗? 一、鸿蒙系统的崛起 鸿蒙操作系统是华为推出的一款面向全场景的分布式操作系统。它的出现…

CPU飙高排查步骤

1&#xff1a;top指令(查看进程信息) top指令&#xff1a;查看进程运行信息 此处1313占用90多CPU 2&#xff1a;ps指令&#xff08;查看进程中每个线程信息&#xff0c;锁定问题线程&#xff09; 查看进程里每个线程的详细占用 ps H -eo pid,tid,%cpu | grep 1313ps H:查看进…

信息学奥赛初赛天天练-83-NOIP2014普及组-基础题2-输入设备、输出设备、操作系统、二进制、整数除法、while、do while循环

1 NOIP 2014 普及组 基础题2 4 以下哪一种设备属于输出设备( ) A 扫描仪 B 键盘 C 鼠标 D 打印机 5 下列对操作系统功能的描述最为完整的是( ) A 负责外设与主机之间的信息交换 B 负责诊断机器的故障 C 控制和管理计算机系统的各种硬件和软件资源的使用 D 将没有程序编译成目…

解决微服务调用失败之 Name or service not known

在微服务启动之后&#xff0c;调用后台接口报错&#xff0c;如下&#xff1a; Name or service not known通过在Eureka控制台查看服务注册情况&#xff0c;发现服务已经注册上来&#xff0c;并且使用了主机名(非Ip)进行了注册。 如下图&#xff1a; 查阅资料得知&#xff0c;…

【时时三省】(C语言基础)指针进阶 例题2

山不在高&#xff0c;有仙则名。水不在深&#xff0c;有龙则灵。 ----CSDN 时时三省 第一个arr 数组名相当于首元素地址 因为他没有放到strlen内部 也没有取地址 strlen是找&#xff3c;0 找不到&#xff3c;0就不会停下来 所以它打印的就是随机值 第二个arr0 首元素地址加零还…

谁还不知道用ChatmoneyAI写短剧脚本,真能火

本文由 ChatMoney团队出品 现在大家打开抖音、小红书&#xff0c;琳琅满目&#xff0c;目光所能及的都是各种吸精剧情的小短剧&#xff0c;虽然这些短剧的制作成本低&#xff0c;但是作为编剧的要写脚本&#xff0c;可不认为这么容易啊......... 接下来我给大家推荐一下使用Ch…

2024年全国大学生数学建模竞赛(B题) 建模解析|生产过程中的决策问题|小鹿学长带队指引全代码文章与思路

我是鹿鹿学长&#xff0c;就读于上海交通大学&#xff0c;截至目前已经帮200人完成了建模与思路的构建的处理了&#xff5e; 本篇文章是鹿鹿学长经过深度思考&#xff0c;独辟蹊径&#xff0c;实现综合建模。独创复杂系统视角&#xff0c;帮助你解决国赛的难关呀。 完整内容可以…

分布式系统漫谈:从一致到共识

0 前言 本文将以**分布式系统的基本组成为出发点&#xff0c;详细探讨分布式系统的发展历程&#xff1b;逐步展开到分布式系统构成的核心要素&#xff0c;分析这些核心要素会对系统造成怎样的影响、以及影响的不同表现形式&#xff1b;最后探讨如何构建基本可用的分布式系统**…