该程序应该接受单位后面的测量输入(例如“10cm”),将其转换为米,然后如果它是最大值或最小值则存储它。然后,它应该将它添加到以“|”结束之前输入的所有值的总和。它应该最终输出最大,最小和总和。 HALP!不是作业 - 只是想自学。
编辑:道歉不明白 -
我现在发现我只需要在测量和单位输入之间添加一个空格。然后我的问题变成:是否有可能(没有第4章新手不能拥有的奢侈知识)接受“10cm”并获得与输入“10cm”相同的结果?这本书特别说:
“为每个输入的双重添加单位;即输入10cm,2.5in,5ft或3.33m等值”
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;
int main()
{
double n1 = 0;
string unit = " ";
double largest = 0;
double smallest = 0;
double sum = 0;
double inM = 0;
while(cin >> n1 >> unit)
{
if(unit == "m")
inM = n1;
else if(unit == "cm")
inM = n1 / 100;
else if(unit == "in")
inM = (n1 * 2.54) / 100;
else if(unit == "ft")
inM = ((n1 / 12) * 2.54) / 100;
else
{
cout << "dont understand";
break;
}
if(inM > largest && smallest == 0){
largest = inM;
smallest = inM;
}
else if (inM < smallest)
smallest = inM;
else if (inM > largest)
largest = inM;
sum += inM;
}
cout << "The smallest value you entered was: " << smallest << "\nThe largest value you entered was: " << largest << "\nThe sum of all values was: " << sum;
}