当前位置: 首页 > news >正文

【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

http://www.lryc.cn/news/430788.html

相关文章:

  • Redis八种数据结构简介
  • 数据治理策略:确保数据资产的安全与高效利用
  • ts格式转mp4,四款亲测好用软件推荐!
  • 10、Django Admin修改标题
  • ESRI ArcGIS Pro 3.1.5新功能及安装教程和下载
  • 人工智能,语音识别也算一种人工智能。
  • Token和Refresh Token
  • STM32(一)简介
  • 接口测试工具:Postman详解
  • Linux中全局变量配置,/etc/profile.d还是/etc/profile
  • 【java入门】关键字、标识符与变量初识
  • Java常用类库
  • 2024年高教社杯数学建模国赛C题超详细解题思路分析
  • 深度学习(七)-计算机视觉基础
  • 机器人笛卡尔空间轨迹规划原理与MATLAB实现
  • 数据结构:树与二叉树
  • BUUCTF—[网鼎杯 2020 朱雀组]phpweb
  • 什么是CDN及其如何影响SEO?
  • python实现粒子群算
  • 【Unity案例】搭建射击系统与UI
  • Python使用zdppy_mysql操作MySQL和MariaDB数据库快速入门教程
  • union 的正确食用方法
  • 汇编语言在虚拟机中输出“Hello World!”
  • JVM类的加载和类的加载器
  • MLM:多模态大型语言模型的简介、微调方法、发展历史及其代表性模型、案例应用之详细攻略
  • Java健康养老智慧相伴养老护理小程序系统源码代办陪诊陪护更安心
  • Python | Leetcode Python题解之第390题消除游戏
  • Github 2024-09-01 开源项目月报 Top16
  • C++ 继承(二)
  • 第 2 章:AJAX 的使用