【C++ Primer Plus习题】11.6

2024-09-06 01:28
文章标签 c++ plus 习题 primer 11.6

本文主要是介绍【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

这篇关于【C++ Primer Plus习题】11.6的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1140610

相关文章

C++ Primer 标准库vector示例详解

《C++Primer标准库vector示例详解》该文章主要介绍了C++标准库中的vector类型,包括其定义、初始化、成员函数以及常见操作,文章详细解释了如何使用vector来存储和操作对象集合,... 目录3.3标准库Vector定义和初始化vector对象通列表初始化vector对象创建指定数量的元素值

C++实现回文串判断的两种高效方法

《C++实现回文串判断的两种高效方法》文章介绍了两种判断回文串的方法:解法一通过创建新字符串来处理,解法二在原字符串上直接筛选判断,两种方法都使用了双指针法,文中通过代码示例讲解的非常详细,需要的朋友... 目录一、问题描述示例二、解法一:将字母数字连接到新的 string思路代码实现代码解释复杂度分析三、

springboot3.4和mybatis plus的版本问题的解决

《springboot3.4和mybatisplus的版本问题的解决》本文主要介绍了springboot3.4和mybatisplus的版本问题的解决,主要由于SpringBoot3.4与MyBat... 报错1:spring-boot-starter/3.4.0/spring-boot-starter-

C++一个数组赋值给另一个数组方式

《C++一个数组赋值给另一个数组方式》文章介绍了三种在C++中将一个数组赋值给另一个数组的方法:使用循环逐个元素赋值、使用标准库函数std::copy或std::memcpy以及使用标准库容器,每种方... 目录C++一个数组赋值给另一个数组循环遍历赋值使用标准库中的函数 std::copy 或 std::

C++使用栈实现括号匹配的代码详解

《C++使用栈实现括号匹配的代码详解》在编程中,括号匹配是一个常见问题,尤其是在处理数学表达式、编译器解析等任务时,栈是一种非常适合处理此类问题的数据结构,能够精确地管理括号的匹配问题,本文将通过C+... 目录引言问题描述代码讲解代码解析栈的状态表示测试总结引言在编程中,括号匹配是一个常见问题,尤其是在

使用C++实现链表元素的反转

《使用C++实现链表元素的反转》反转链表是链表操作中一个经典的问题,也是面试中常见的考题,本文将从思路到实现一步步地讲解如何实现链表的反转,帮助初学者理解这一操作,我们将使用C++代码演示具体实现,同... 目录问题定义思路分析代码实现带头节点的链表代码讲解其他实现方式时间和空间复杂度分析总结问题定义给定

mybatis和mybatis-plus设置值为null不起作用问题及解决

《mybatis和mybatis-plus设置值为null不起作用问题及解决》Mybatis-Plus的FieldStrategy主要用于控制新增、更新和查询时对空值的处理策略,通过配置不同的策略类型... 目录MyBATis-plusFieldStrategy作用FieldStrategy类型每种策略的作

C++初始化数组的几种常见方法(简单易懂)

《C++初始化数组的几种常见方法(简单易懂)》本文介绍了C++中数组的初始化方法,包括一维数组和二维数组的初始化,以及用new动态初始化数组,在C++11及以上版本中,还提供了使用std::array... 目录1、初始化一维数组1.1、使用列表初始化(推荐方式)1.2、初始化部分列表1.3、使用std::

C++ Primer 多维数组的使用

《C++Primer多维数组的使用》本文主要介绍了多维数组在C++语言中的定义、初始化、下标引用以及使用范围for语句处理多维数组的方法,具有一定的参考价值,感兴趣的可以了解一下... 目录多维数组多维数组的初始化多维数组的下标引用使用范围for语句处理多维数组指针和多维数组多维数组严格来说,C++语言没

c++中std::placeholders的使用方法

《c++中std::placeholders的使用方法》std::placeholders是C++标准库中的一个工具,用于在函数对象绑定时创建占位符,本文就来详细的介绍一下,具有一定的参考价值,感兴... 目录1. 基本概念2. 使用场景3. 示例示例 1:部分参数绑定示例 2:参数重排序4. 注意事项5.