2016-04-22 18 views
-2

私はStroustrupsの「Programming Principles and Practice with C++」を使って作業しています。ここで第4章Stroustrupドリル。難しいステップ(少なくとも私にとって!)

は指摘している:

1) Write a program that consists of a while-loop that (each time around the loop) reads in two ints and then prints them. Exit the program when a terminating '|' is entered. 

2)Change the program to write out the smaller value is: followed by the smaller of the numbers and the larger value is: followed by the larger value. 

3)Augment the program so that it writes the line the numbers are equal (only) if they are equal. 

4)Change the program so that it uses doubles instead of ints. 

5)Change the program so that it writes out the numbers are almost equal after writing out which is the larger and the smaller if the two numbers differ by less than 1.0/100. 

6) Now change the body of the loop so that it reads just one double each time around. Define two variables to keep track of which is the smallest and which is the largest value you have seen so far. Each time through the loop write out the value entered. If it’s the smallest so far, write the smallest so far after the number. If it is the largest so far, write the largest so far after the number. 

これは私がこれまで持っているコードです:

int main(){ 

    double number1 = 0; 
    double number2 = 0; 
    double maximum = 0; 
    double minimum = 0; 


    cout << " Keep entering numbers. If you want to exit the program press alt + z" << endl; 
    while (cin >> number1 && cin >> number2) { 

if (number1 == '@' || number2 == '@') { 
    break; 
    } 

else if (number1 < number2){ 
    cout << "The smaller value is " << number1 << '.' << endl; 
    cout << "The larger value is " << number2 << '.' << endl; 
    maximum = number2; 
    minimum = number1; 
    if (number2 - number1 < 0.01) { 
     cout << "The numbers are almost equal"; 

    } 
} 

else if (number1 > number2) { 
    cout << "The smaller value is " << number2 << '.' << endl; 
    cout << "The larger value is " << number1 << '.' << endl; 
    maximum = number1; 
    minimum = number2; 
     if (number1 - number2 < 0.01) { 
     cout << "The numbers are almost equal"; 
    } 

} 

else { 
    cout << "Both numbers are the same." << endl; 
} 
    } 

誰かが私には、最大、最小の番号を見つけるために、これを修正するに役立つことはできますか? 私はそれについて読んで、ソートされたベクトルの解決策を見つけましたが、私はそれを私の問題に適用できないようです。

は感謝:)あなたがする#include <アルゴリズム>」する必要があります

+0

私たちはあなたの宿題をしません。 – xaxxon

+0

これは宿題ではありません。私はずっと前から大学にいません!私は自分の新しいスキルを学び、ヘップを探しています。私は立ち往生したくないからです。 – KamboMambo

+0

あなたは* "...ソートされたベクトル解を見つけましたが、私はそれを私の問題に適用できないようです" * - 試行*を示し、それが何をしたのか、やらなかったのか、何を期待したのか、あなたは「なぜ」、どのように調査しようとしたのかを理解していません。 –

答えて

0

既存のプログラムに構造的な変更を行い、最大と最小のランニングカウントを保持していないしたい場合は、あなた自身の最小と最大の機能を動かさない限り。代わりの

maximum = number1; 
minimum = number2; 

使用この:

maximum = (maximum < max(number1, number2)) ? max(number1, number2) : maximum; 
minimum = (minimum > min(number1, number2)) ? min(number1, number2) : minimum; 
+0

本のこの時点では、私はそれらのステップ(最小最大のもの)を見ていない。多分別の方法でしょうか? (私はベクトルを使用して推測することが動作するかもしれないと思っているが、私はすべてそれをstする方法を知らない)。 – KamboMambo

0

は演習6の場合は、あなたがのstd ::ベクトルを使用する必要はありません。私は演習2と5のためのstd ::ベクトルを使用している

int 
main() 
{ 
    std::string quit("|"); 
    int i = 0; 
    std::string s; 
    double n; 
    std::vector <double>v = { 0.0, 0.0 }; 

    double maxn = std::numeric_limits <double>::lowest(); 
    double minn = std::numeric_limits <double>::max(); 

    std::vector <double>in_meters; 

    while (std::cin >> s) { 
     if (quit.compare(s) == 0) 
      break; 

     if (reject(s)) 
      continue; 

     n = str2meters(s); 
     std::cout << n << " "; 

     in_meters.push_back(n); 

     if (maxn < n) { 
      std::cout << "the largest so far" << std::endl; 
      maxn = n; 
     } 

     if (minn > n) { 
      std::cout << "the smallest so far" << std::endl; 
      minn = n; 
     } 

     v[i] = n; 
     if (i == 1) { 
      std::sort(v.begin(), v.end()); 
      prn(v); 
     } 

     i = (i + 1) % 2; 
    } 

    prng(in_meters); 

    return 0; 
} 

void 
prn(std::vector <double> &v) 
{ 

    const double one_percent = 1.0/100.0; 

    std::cout << "the smaller value is: " << v[0] << std::endl; 
    std::cout << "the larger value is: " << v[1] << std::endl; 

    if ((v[1] - v[0]) < one_percent) 
     std::cout << "are almost equal" << std::endl; 

} 
次のコードスニペットに示すように、あなたは、ミネソタ州とMAXNという単一の変数で最大値と最小値を追跡し続けることができます

と練習問題9-11:

void 
prng(std::vector <double> v) 
{ 

    std::sort(v.begin(), v.end()); 

    for (auto k : v) 
     std::cout << k << " " ; 
    std::cout << std::endl; 

    double sum = std::accumulate(v.begin(), v.end(), 0.0); 
    std::cout << "number of values " << v.size() << std::endl; 
    std::cout << "sum of values " << sum << std::endl; 

} 
関連する問題