2012-02-14 4 views
2

私は数日間これらの訓練に対して私の頭を打ちつけてきました、そして、私はそれらをつかむことができません。 私は自分の番号にユニットを追加しなければなりません(例えば、m、ft、in、cm)。無効なユニットを拒否し、数字とユニットをメートルに変換しなければなりません。
私は、最高と最低の番号などを選択して保持する方法を示すコードを持っていますが、ユニットパーツを追加する方法を完全に失っています。if文を試してみました。しかし、私は挫折し、正直なところバグで私はいくつかの基本的な点を見逃してしまった。ここ は私のコードは、それは宿題ではありません、これまでプログラミングの第4章からのドリルを扱う:C++を使った原則と実践。 Bjarne Stroustrup

int main(){ 
double val1=0; 
string unit; 
double large=0; 
double small=0; 

cout<<"please Enter 1 number and a unit (cm, M, Ft, In):\n"; 
while (cin>>val1>>unit){ 

    if(val1<small) {small=val1; cout<<"smallest so far\n";}//find smallest number 
    else if(val1>large) {large=val1; cout<<"largest so far\n";}//Find largest number 



else cout<<"Neither largest nor smallest\n"; 
} 
cout<<"The smaller value so far is (in metres): "<<small<<"\n"; 
cout<<"The larger value so far is (in metres): "<<large<<"\n"; 

keep_window_open("~"); 
return 0; 

}

であり、私は私自身の利益のためにこれをやっています。どんな助けもありがとう。

+0

最近のプレゼンテーションのユニット用のユーザー定義のリテラルを含むコードを、Bjarneは歩みます。http://channel9.msdn.com/Events/GoingNative/GoingNative-2012/Keynote-Bjarne-Stroustrup-Cpp11-Styleそれはあなたを助けるかもしれません:-) –

+2

'数値と単位をmetres'に変換します、どこですか?私はあなたに変換されて表示されません。 – atoMerz

+0

あなたは最小の距離を維持しているようには思われません。最初の距離を読み取って、それを最大と最小にしてから、ループを進めてください。 – Beta

答えて

-1

私はそれを得たと思う。私は最初の4つの章からこれらの基本を知っていることを覚えておいてください。これまでのツールを考慮すると、これは適切な方法ですか?

int main(){ 

double val1 = 0; 
string unit; 
double large = 0; 
double small = 0; 

cout << "please Enter 1 number and a unit (cm, M, Ft, In):" << endl; 

while (cin >> val1 >> unit) { 
    if (unit == "cm") val1 = val1/100; 
    else if (unit == "m") val1 = val1; 
    else if (unit == "in") val1 = val1 * 0.0254; 
    else if (unit == "ft") val1 = val1 * 0.3048; 
    else { 
     cout << endl << "dont understand " << unit; 
     break; 
    } 

    if (small == 0 && val1 > large){ 
     small = val1; 
     large = val1; 
     cout << val1 << "metres is both the largest and smallest" << endl; 
    } else if (val1 < small) { 
     small = val1; 
     cout << small << " metres is the smallest so far" << end; 
    } else if (val1 > large){ 
     large = val1; 
     cout << large <<" metres is the largest so far" << endl; 
    } else { 
     cout << val1 << " metres is neither largest nor smallest" << endl; 
    } 
} 

cout << endl << small << " metres" << "\t" << "is the smallest number" << endl << large << " metres" << "\t" << "is the largest number" << endl; 

keep_window_open("~"); 
return 0; 
} 
+0

多分私のものですが、コードをよりよくインデントする必要がありますmain関数のスコープ内でインデントされ、whileループのスコープ内のすべてがインデントされる必要があります)。手動で改行を挿入するのではなく、std :: endlを使用する必要があります(例: 'cout << "bla" << endl;)。 – tr9sh

+0

はい、私のindentingは注意を必要とします、私はendlのことについては知らなかった、ありがとう –

1

1。あなたはすでに定数と "魔法の定数や数字"について知っています
What is a magic number, and why is it bad?)。

2。 cout < < "1数字と単位(cm、M、Ft、In)を入力してください:" < < endl; 大文字を使用して小文字をテストするので、これは間違っています。



3。 if (small == 0 && val1 > large)

私は0メートルを入力すると、それらはすでに、私はむしろ使用することになりメートルにその変換を好まない

if(small=0 && large==0) 



4 .Personaly、より良い使用を設定されているようにそれはその「そのどちらも最大規模も小さい」と言います"m_in_cmは"

const double m_in_cm=100;

ある

if(unit=="m") 
    { 
     if((val1*=m_in_cm) <= small) 
     { 
      std::cout<<"\nThe smallest value so far "<<val1<<" "<<unit<<" \n"; 
      small=val1; 
     } 
     if((val1*=m_in_cm) >= large) 
     { 
      std::cout<<"\nThe largest value so far "<<val1<<" "<<unit<<" \n"; 
      large=val1; 
     } 
    } 
    else if(unit=="in"){....} 

元の値と使用された単位を保持します。

+0

#2のあなたの答えを理解していなかったので、元の投稿のコードと一致しません。しかし、質問を書いた同じ人からの答えを見つけた後は意味があります。 – drescherjm

0

この私はそれを第4章では、ベクトルを議論し

#include "../../std_lib_facilities.h" 
double unitconv(double x, string y) // a function to convert units. 
{ 
    if (y == "in") 
    { 
     x = x/39.37; 
     y = "m"; 
    } 
    else if (y == "ft") 
    { 
     x = x/3.28; 
     y = "m"; 
    } 
    else if (y == "cm") 
    { 
     x = x/100; 
     y = "m"; 
    } 
    else if (y == "m") 
    { 
     x = x; 
     y = y; 
    } 
    else 
     cout << "unrecognised unit"; 
    return x; 

} 

int main() 
{ 
    double value = 0; 
    string unit = " "; 
    double sum = 0; 
    int iter = 0; 
    vector<double>values; 
    cin >> value >> unit; 
    value = unitconv(value, unit); 
    values.push_back(value); 
    sum = sum + value; 
    ++iter; 
    double largest = value; 
    double smallest = value; 
    while (cin >> value >> unit) 
    { 
     value = unitconv(value, unit); 
     if (value > largest) 
     { 
      largest = value; 
     } 
     if (value < smallest) 
     { 
      smallest = value; 
     } 
     values.push_back(value); 
     sum = sum + value; 
     ++iter; 
     cout << "\nLargest Value = " << largest << "\nSmallest Value = " << smallest << "\nSum of Values = " << sum << "\nNumber of Values = " << iter << "\n"; 
    } 
    sort(values.begin(),values.end()); 
    for (int i = 0; i < values.size(); ++i) 
     cout << values[i] << '\n'; 
    keep_window_open(); 
} 
0
#include <iostream> 
#include <algorithm> 
#include <vector> 
#include <cmath> 

using namespace std; 

int main() 
{ 
    double a = 0.0; 
    vector<double> double_vec; 

    int iterator = 0; 
    double temp_smallest = 0; 
    double temp_largest = 0; 
    int count = 0; 

    while (cin >> a) 
    { 
     // add i to the vector; 
     double_vec.push_back(a); 

     for (int i = 0; i < count; i++) 
     { 
      if (double_vec[i] > temp_largest) 
      { 
       temp_largest = double_vec[i]; 
      } 

      else if (double_vec[i] < temp_smallest) 
      { 
       temp_smallest = double_vec[i]; 
      } 
     } 

     if (temp_smallest > a) 
      cout << "smallest so far" << endl; 
     else if (temp_largest < a) 
      cout << "largest so far"; 
     count++; 
    } 

    system("PAUSE"); 

    return 0; 
} 

を解く方法を、すべての値のテストを実行するために追加することができますいくつかの変更があります。このプログラムはで動作しますが、すべてがという値ではありません。

0

これは私の2セントです(話す)。知識を使用して、同じブック、同じドリルは、

/* The problem: 
    Add a unit to each double entered; that is, enter values such as 10cm, 
    2.5in, 5ft, or 3.33m. Accept the four units: m, cm, in, ft. Assume 
    conversion factors 1m == 100cm, l in == 2.54cm, ft == 12in. Read the unit 
    indicator into a string. */ 


#include "../../std_lib_facilities.h" 

int main() 
{ 
    double a; 
    string unit = ""; 
    double smallest = 0; 
    double largest = 0; 
    bool initialize = false; 

    while (cin >> a >> unit) 
    { 
     cout << "You have entered: " << a << unit << endl; 

     if (unit == "m") 
      a = a * 100; 
     else if (unit == "cm") 
      a = a * 1; 
     else if (unit == "ft") 
      a = a * 12 * 2.54; 
     else if (unit == "in") 
      a = a * 2.54; 

     /*no comparison until second set of values are accepted. That is when 
      trackers of largest and smallest numbers are initialized*/ 

     if (!initialize) 
     { 
      smallest = a; 
      largest = a; 
      initialize = true; 
     } 
     else 
     { 
      if (a > largest) 
      { 
       cout << "The largest so far" << endl; 
       largest = a; 
      } 

      if (a < smallest) 
      { 
       cout << "The smallest so far" << endl; 
       smallest = a; 
      } 
     } 
    } 
    return 0; 
} 

出力...これまで

10 m 
You have entered: 10m 
40 ft 
You have entered: 40ft 
The largest so far 
500 in 
You have entered: 500in 
The largest so far 
39 ft 
You have entered: 39ft 
30 ft 
You have entered: 30ft 
The smallest so far 
1 m 
You have entered: 1m 
The smallest so far 
1 in 
You have entered: 1in 
The smallest so far 

PSを説明:私はまだ初心者だので、私は本当にコメントコード上の任意のフィードバックをいただければ幸いです、書式設定、論理、およびその他のすべて。

関連する問題