第4章 斯特劳斯特鲁普钻头一个具有挑战性的一步(至少对我来说!

Chapter 4 Stroustrup Drill. A challenging step (at least for me!)

本文关键字:挑战性 一步 对我来说 一个 斯特劳斯 4章      更新时间:2023-10-16

我正在学习Stroustrups的"使用C++编程原理和实践",并陷入了一个练习。

以下是适应症:

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 <算法>",除非你想推出自己的最小和最大函数。

而不是:

maximum = number1;
minimum = number2;

使用这个:

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

对于练习 6,您不必使用 std::vector。您可以跟踪名为 minn 和 maxn 的单个变量中的最大值和最小值,如以下代码片段所示:

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;
}

我在练习 2 和 5 中使用了 std::vector:

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;
}

和练习 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;
}

你让它变得复杂,从这个开始,使用"std_lib_facilities"

    #include "....std_lib_facilities.h"
    int main() {
        cout << "type in two integers or a '|' to terminate the program:n";
        int x1 = 0;
        int x2 = 0;
        while (cin >> x1 >> x2)
        {
            cout << x1 << ", " << x2 << "n";
        }
    keep_window_open("x");
    }