このプログラムは、測定値とそれに続く単位 (例: "10cm") の入力を受け入れ、それをメートルに変換し、それが最大値または最小値である場合はそれを保存することになっています。次に、「|」で終了する前に入力されたすべての値の合計にそれを追加する必要があります。最終的に、最大、最小、および合計を出力する必要があります。はる!宿題ではなく、自分で教えようとしているだけです。
編集:明確さの欠如のための謝罪 -
測定値と単位入力の間に空白を追加するだけでよいことがわかりました。私の質問は次のようになります: (第 4 章の初心者が持つことができないような贅沢な知識がなくても) 「10cm」を受け入れて、「10cm」を入力したのと同じ結果を得ることができるでしょうか? この本は具体的に次のように述べています。
"各 do double 入力に単位を追加します。つまり、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;
}