编程原理和实践使用C++第 4 章演练,步骤 7
Programming Principles and Practice Using C++ Chapter 4 Drill, Step 7
我正在学习编程原理和使用C++的实践,但我被困在第 7 章练习的第 4 步。我在这里发现了类似的问题,但是转换单位/值并查看哪个更大/更小时有些东西不起作用。程序运行良好,但由于某种原因,某些转换返回不正确,例如,如果我输入 2 m,然后 2 ft. 2 ft 作为较大的值返回。
我知道代码可能看起来有点丑陋,如果我能让它工作,我会将转换放在一个函数中。提前谢谢。
int main() {
double doubNum = 0;
double smallestNum = ' ';
double largestNum = 0;
string unitOfDistance = " ";
double testNum = 0;
cout << "Enter a distance with a unit of measure (ft, in, cm, m): ";
while (cin >> doubNum >> unitOfDistance) { //while tests to see if the input is a double and unit is legal
//check the unitOfDistance and convert all values to cm and hold in testNum for comparison
if (unitOfDistance == "in") { //in to cm
testNum = doubNum * 2.54;
}
else if (unitOfDistance == "ft") { //ft to cm
testNum = (doubNum * 12) * 2.54;
}
else if (unitOfDistance == "cm") { //cm
testNum = doubNum;
}
else if (unitOfDistance == "m") { //m to cm
testNum = doubNum * 100;
}
else {
cout << "I don't know that unit.n";
return 0;
}
//check to see if testNum (the converted version of doubNum) is the smallest/largest/same value entered so far
if (testNum < smallestNum) {
smallestNum = doubNum;
cout << smallestNum << " " << unitOfDistance << " is the smallest distance entered so far.n";
}
else if (testNum > largestNum) {
largestNum = doubNum;
cout << largestNum << " " << unitOfDistance << " is the largest distance entered so far.n";
}
else {
cout << smallestNum << " " << unitOfDistance << " is the smallest distance entered so far.n";
cout << largestNum << " " << unitOfDistance << " is the largest distance entered so far.n";
}
cout << "Enter another distance with unit: n";
}}
试试这个
#include <iostream>
#include <limits>
using namespace std;
int main() {
double num, result, smallest, largest;
smallest = numeric_limits<double>::max();
largest = numeric_limits<double>::min();
string unit;
cout << "Enter a distance with a unit of measure (ft, in, cm, m): ";
while (cin >> num >> unit) {
if (unit == "in") // in to cm
result = num * 2.54;
else if (unit == "ft") // ft to cm
result = (num * 12) * 2.54;
else if (unit == "cm") // cm
result = num;
else if (unit == "m") // m to cm
result = num * 100;
else {
cout << "I don't know that unit.n";
break;
}
smallest = min(smallest, result);
largest = max(largest, result);
cout << smallest << " cm is the smallest distance entered so far.n";
cout << largest << " cm is the largest distance entered so far.n";
}
return 0;
}
输入
2 m
3 ft
6 in
输出
Enter a distance with a unit of measure (ft, in, cm, m):
200 cm is the smallest distance entered so far.
200 cm is the largest distance entered so far.
91.44 cm is the smallest distance entered so far.
200 cm is the largest distance entered so far.
15.24 cm is the smallest distance entered so far.
200 cm is the largest distance entered so far.
你的代码几乎没有问题:
1.double smallestNum = ' '
应替换为双smallestNum = DBL_MAX
(或非常大的值)。
2.正如您使用largestNum
和smallestNum
来跟踪最大值和最小值一样,您还需要使用unitOfLargestDistance
和unitOfSmallestDistance
来跟踪其相应的单位。
3.
if (testNum < smallestNum) {
smallestNum = doubNum;
cout << smallestNum << " " << unitOfDistance << " is the smallest distance entered so far.n";
}
在这里,您需要更新smallestNum
testNum
不doubNum
.所以它应该是这样的:
if (testNum < smallestNum) {
smallestNum = testNum;
cout << doubNum << " " << unitOfDistance << " is the smallest distance entered so far.n";
}
其他 2 个条件也是如此。
相关文章:
- 不能将复制初始化与隐式转换的多个步骤一起使用
- 生成一个生成文件,该生成文件使用Automake在一个步骤中编译和链接所有源文件
- 理解递归代码的演练
- 释放动态分配的内存时是否需要执行此额外步骤
- Conan.io 在编译步骤中或已经在签出时
- 这是什么类型的C++语法,我应该采取什么步骤来理解这一点
- 当前步骤:构建任务.json 文件
- 查找字符串可以减少到 0 的步骤数
- C++函数重载,具体步骤是什么
- Qt Creator 在执行步骤 "make" 时出现编译错误,-fno-stack-limit
- 使用步骤c++构建一个向量
- 在Linux Mint 19下,从哪里提取wxWidgets的源代码(.cpp文件)用于步骤内部调试
- 如何通过指定开始值、结束值和步骤来生成值序列
- 显示实现最大利润的步骤
- 以最少的步骤将给定整数转换为另一个整数
- 使用打开级联读取步骤文件
- 在W10下为NVidia使用VS2019构建计算cpp-sdk的任何特殊步骤
- Mujoco 的入门步骤:缺少具有 Simulate.cpp 的共享库
- 编程:原理与实践 使用C++ 第 4 章演练步骤 6:关于数字范围的一般问题
- 编程原理和实践使用C++第 4 章演练,步骤 7