gpt4 book ai didi

c++ - 需要一些帮助来找出我程序中的逻辑错误

转载 作者:行者123 更新时间:2023-11-28 05:01:24 25 4
gpt4 key购买 nike

我正在通过 Bjarne Stroustrup 的“Programming Principles and Practices Using C++”第二版学习 C++。我致力于完成所有的练习和练习,因为我在学校一个月后要上一门游戏 AI 课,我需要了解 C++,而且我想了解尽可能多的知识。

无论如何,如果有人做过书中的练习,那么我正在参加第 4 章的练习。我在最后。最终产品应该是一个程序,该程序在 while 循环中获取一个数字后跟一个测量单位(cm、in、ft 或 m),同时跟踪最大值、最小值和总和以米为单位输入的单位。我完全以米为单位工作,除了最大和最小值被转换为米以检查哪个更大但存储为用户最初输入的值。这些数字也被插入到一个 vector 中,直到最后才使用该 vector 对值进行排序并按顺序打印它们。

无论如何,我似乎没有问题跟踪总米,因为它在最后正确打印出来,但当涉及到最小值时,似乎出现了问题,偶尔也会出现最大值。另外,某些值在值 vector 中完全不存在。我会但下面的代码。我认为错误出在我的 toMeters() 中的某个地方,但我已经盯着它看了很长时间,以至于我觉得新的一双眼睛可能会有所帮助。谢谢!

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>

using namespace std;

constexpr double mToCm = 100.00; //1m == 100cm
constexpr double inToCm = 2.54; //1in == 2.54cm
constexpr double ftToIn = 12.00; //1ft == 12in

//this function just increases total ammount of units in meters
double adder(string unit, double total, double num)
{
if(unit == "cm")
{
total += (num / mToCm);
}
else if(unit == "in")
{
double temp;
temp = num * inToCm;
total += (temp / mToCm);
}
else if(unit == "ft")
{
double temp;
temp = num * ftToIn;
temp *= inToCm;
total += (temp / mToCm);
}
else
{
total += num;
}
return total;
}

double toMeters(string unit, double num)
{
double value = num;

if(unit == "cm")
{
value /= mToCm;
}
else if(unit == "in")
{
value *= inToCm;
value /= mToCm;
}
else if(unit == "ft")
{
value *= ftToIn;
value *= inToCm;
value /= mToCm;
}
return value;
}

int main()
{

double num, biggest, smallest; //current number, biggest and smallest value
double total = 0; //total number in meters
string unit, biggestUnit, smallestUnit; //current unit, unit for biggest and smallest value
int count = 0; //count for the loop it's only real purpose is on the first and second loop runs
vector<double>meters; //a vector of doubles called meters


cout << "Total ammount of units will be converted to meters, largest and biggest values will be kept in original units\n";
cout << "Start of by entering a number followed by cm, m, in, ft and continue until you want to stop; to stop, press |\n";

while(cin >> num >> unit)
{
//check if user want to stop
if(num == '|')
{
break;
}

//check for correct units, if not then break out
if(unit != "cm" && unit != "m" && unit != "in" && unit != "ft")
{
cout << "Unit not recognized; only cm, m, in, ft are valid\n";
return 1;
}
//this checks if there is no second unit but since cin reads no
//whitespace it doesn't work. I just haven't gotten around to removing it
else if(unit == " ")
{
cout << "Please input a unit\n";
return 1;
}

//all values in vectors are supposed to be in meters. If the unit is not
//in meters, we will call a push back on the value returned by converting
//the orignal number to meters
if(unit != "m")
{
meters.push_back(toMeters(unit, num));
}
//else, just call a push_back
else
{
meters.push_back(num);
}

//if the count is 0, i.e. very first run of the program, biggest and
//smallest is equal to original and call adder() to increment the total
if(count == 0)
{
biggest = smallest = num;
biggestUnit = smallestUnit = unit;

total = adder(unit, total, num);
}
//else, if count is > 0, i.e. this is not the first run, do this part
else
{
//if the value returned after running toMeters on the current value
//is greater than the biggest, biggest now equals current num and
//biggest unit is equal to current unit. Then call adder to inceare total
if(toMeters(unit, num) > toMeters(biggestUnit, biggest))
{
biggest = num;
biggestUnit = unit;

total = adder(unit, total, num);
}
//same as top function but for smallers value
else if(toMeters(unit, num) < toMeters(smallestUnit, smallest))
{
smallest = num;
smallestUnit = unit;

total = adder(unit, total, num);
}
//else both numbers are equal so just make a call to adder()
else
{
total = adder(unit, total, num);
}
}
//increase count just because. It was really only needed to be incremented once
++count;
}

//call sort on the vector then print out the total units followed by bigges the smallest values
//then the values in meters, is ascending sorted order.
sort(meters.begin(), meters.end());
cout << "Total units in meters is " << total << "\n";
cout << "Largest unit is " << biggest << biggestUnit << "\n";
cout << "Smallest unit is " << smallest << smallestUnit << "\n";
cout<< "Here are all values you entered, in meters, in ascending order: \n";

for(int i : meters)
{
cout << meters[i] << "m" << " ";
}
cout << "\n";
}

我最近的输入是 1 cm 2 in 3 m 4 ft,输出是 以米为单位的总单位为 4.28 最大单位是3m(正确) 最小单位是2in(应该是1cm) 以下是您输入的所有值,以米为单位,按升序排列: 0.01m 0.01m 0.0508m 3m

最佳答案

当您在第一个输入上设置 biggestsmallest 时,您不会设置 biggestUnitsmallestUnit。因此比较无法正常工作。

尝试:

    //if the count is 0, i.e. very first run of the program, biggest and 
//smallest is equal to original and call adder() to increment the total
if(count == 0)
{
biggest = smallest = num;
biggestUnit = smallestUnit = unit;

total = adder(unit, total, num);
}

此外,将 total = adder(unit, total, num); 放在每个代码路径中也不是一个好主意。只需将它放在 if 之外一次。否则,很难看出它总是针对每个输入精确执行一次。

关于c++ - 需要一些帮助来找出我程序中的逻辑错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45850753/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com