Bjarne Stroustrup的P:PP第4章钻

时间:2015-09-17 17:39:01

标签: c++ vector curly-braces

我刚刚开始学习 C ++ ,我正在通过 Bjarne Stroustup的P:P& P 独立工作。

我正在进行第4章练习

我遇到的问题似乎在于程序的顺序。如果我在向量之前添加另一个结束大括号来关闭while语句,我得到max_valmin_val的正确输出。但是,通过添加该括号,名为sum的双精度值保持为零,即使我希望sum以双精度数字递增。

如果我现在编写程序编译(没有添加额外的花括号),我得到min_valmax_val的错误输出,但是sum的输出正确。

另外,正如您可以在程序底部看到的那样: cout << " values were entered." << '\n';不完整。我想打印输入的总值的数量,但由于沮丧和需要帮助而使其不完整。我对编程非常陌生,任何建设性的批评,无论多么苛刻,都会受到赞赏。

#include "std_lib_facilities.h"

int main()
{
    double value = 0;
    double max_val = 0;
    double min_val = 0;
    string unit =" ";
    double sum = 0;

    cout << "Enter some numbers, followed by a unit of distance (m, cm, ft, inch):" << '\n';

    while (cin >> value >> unit){

        //determines entered value as max/min/both/neither
        if (min_val == 0 && value > max_val){                       // first number entered is both largest and smallest
            max_val = value;
            min_val = value;
            cout << value << " metres is both the smallest and largest so far" << '\n';
        }
        else if (value < min_val){// smallest number min_val = value;
            cout << min_val << " metres is the smallest so far." << '\n';
        }
        else if (value > max_val){// largest number max_val = value;
            cout << max_val << " metres is the largest so far." << '\n';
        }
        else { // number between smallest and largest
            cout << value << " metres is neither the smallest or largest." << '\n';    }

        //convert entered unit to metres
        if (unit == "m"){
           value = value;
        }
        else if (unit == "cm"){//converts cm to metres
           value = value/100;
        }
        else if (unit == "ft"){//converts ft to metres
           value = value/3.28084;
        }
        else if (unit == "inch"){//converts inch to metres
            value = value/39.3701;
        }
        else{
            cout << "I dont know the unit " << unit << ".";
    }
    vector <double> numbers; //reads input into vector
    double number = 0;
    while (cin >> number) numbers.push_back(number);

    for (double number: numbers) sum += number;

    cout << "The largest value entered was: " << max_val << "." << '\n';
    cout << "The smallest value entered was: " << min_val << "." << '\n';
    cout << "The sum of the numbers entered is: " << sum << "metres" <<'\n';
    cout << " values were entered." << '\n';

    keep_window_open("~");

return 0;
}

1 个答案:

答案 0 :(得分:1)

以下是有关输入循环的一个小见解,找到min_valuemax_valuesum

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

int main(){
    // vector holding the input values
    vector<double> inputValues;
    // vector-input variable
    double temp = 0;                                    
    // variable holding the sum of the input 
    double sum = 0;                                     
    // variables holding the minimum and maximum input value
    double minVal = 100000; // note the initialization values
    double maxVal = -100000;

    vector<string>units;
    string unit;
    // prompt message; value input
    cout << "Enter the first value: ";
    while (cin >> temp >> unit) { 
        cout << "Enter the next value: ";
        inputValues.push_back(temp);
        units.push_back(unit); 
    }

    // conversion... 

    for (int i = 0; i < inputValues.size(); ++i){
        if (inputValues[i] > maxVal){ maxVal = inputValues[i]; }
        if (inputValues[i] < minVal){ minVal = inputValues[i]; }
        sum += inputValues[i];
    }

   // print result
   cout << "Minimum temperature = " << minVal << endl;
   cout << "Maximum temperature = " << maxVal << endl;
   cout << "Average temperature = " << sum/inputValues.size() << endl;
   return 0;
}

对于转换,您可以使用第二个容器,例如用于存储unit的向量,然后使用相同的索引为值和单位创建循环,每个单位转换使用ifelse if块:

vector<string> units;
for(int i = 0 ; i < inputValues.size(); ++i){
    if(units[i] == "cm") inputValue[i] = inputValue[i] / 100.;
    else if(units[i] == "...") inputValue[i] =...;
    // ...
}

注意:

输入循环需要改进,例如终止条件,考虑使用do - while循环终止关键字,处理错误的输入类型,cin.clear()等。