Bjarne Stroustrup的P:PP Chapter 4 Drill

Bjarne Stroustrup's P:PP Chapter 4 Drill

本文关键字:Chapter Drill PP Stroustrup Bjarne      更新时间:2023-10-16

我刚刚开始学习 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;
}

这是一个关于输入循环的小见解,找到min_value, max_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;
}

对于转换,您可以使用第二个容器,例如vector来存储unit s,然后使用相同的索引为值和单位创建一个循环,为每个单位转换使用if, else 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()