我正在学习C ++,通过Bjarne Stroustrup"编程原则和实践使用C ++"第二版。我致力于完成所有的练习和练习,因为我在学校一个月内参加一个游戏AI课程,我需要了解C ++,我想尽可能多地了解。
无论如何,如果有人在书中进行了演练,我将参加第4章的练习。我在最后。最终产品应该是一个程序,它在一个while循环中取一个数字后跟一个测量单位(cm,in,ft或m),同时跟踪最大值以及最小值和总和单位以米为单位。我完全以米为单位工作,除了最大和最小的值,这些值被转换为米,以检查哪一个更大但存储起来是由用户最初输入的。这些数字也会插入到一个向量中,直到最后才对数值进行排序并按顺序打印。
无论如何,我似乎没有跟踪总计的问题,因为它在最后打印出正确但是当它达到最小值时,某些东西似乎出错了,偶尔会出现最大值。另外,某些值在值向量中完全关闭。我会,但代码吼叫。我认为错误是在我的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平方厘米,3米4英尺,输出为 以米计的总单位是4.28 最大单位是3米(正确) 最小单位是2英寸(应该是1厘米) 以下是您输入的所有值(以米为单位),按升序排列: 0.01m 0.01m 0.0508m 3m
答案 0 :(得分:1)
当您在第一个输入上设置biggestUnit
和smallestUnit
时,您不会设置biggest
或smallest
。所以比较不能正常工作。
尝试:
//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
之外。否则,很难看到每次输入总是精确执行一次。