【问题标题】:Keeping track of which is the smallest and which is the largest value so far in a loop跟踪循环中到目前为止哪个是最小的,哪个是最大值
【发布时间】:2016-05-15 09:46:42
【问题描述】:

你能帮我解决简单的问题吗?我对 C++ 非常熟悉,并从 Bjarne Stroustrup 的“编程:使用 C++ 的原则和实践”一书中学习。我以前从未学过 C++,所以我不熟悉许多有用的特性。钻头说:

"6. 现在改变循环体,让它只读取一个双精度数 每次。定义两个变量来跟踪哪个是 最小的,这是您迄今为止看到的最大值。每个 通过循环的时间写出输入的值。如果是 迄今为止最小的,在数字后面写迄今为止最小的。如果是 迄今为止最大的,在数字后面写迄今为止最大的”

如果不使用矢量,我不知道如何正确执行此操作。这是我的代码:

#include "C:/std_lib_facilities.h"

int main()
{
    double a, b,differ=0;
    char c=' ';
    cout << "Enter two values: \n";
    while (c != '|' && cin >> a >> b )
    {
        if (a > b)
        {
            cout << "The smaller value is: "<< b << " and the larger value is: " << a << "\n \n";
            differ = a - b;
            if (differ < 1.0 / 100)
                cout << "Numbers are almost equal\n\n";
        }
        else if (a < b)
        {
            cout << "The smaller value is: " << a << " and the larger value is: " << b << "\n \n";
            differ = b - a;
            if (differ < 1.0 / 100)
                cout << "Numbers are almost equal\n\n";
        }
        else
        {
            cout << "These values are equal!\n";
        }
        cout << "Enter a character | to break loop: \n";
        cin >> c;
    }
    cout << "You have exited the loop.\n";
    keep_window_open();
}

这里是前面的步骤,这些我已经用上面的代码解决了:

  1. 编写一个由 while 循环组成的程序,该循环(每次围绕循环)读取两个整数,然后打印它们。退出 终止“|”时的程序被输入。
    1. 改程序写出较小的值是:后面是较小的数字,较大的值是:后面是 更大的价值。
    2. 扩充程序,使其写入数字相等的行(仅当它们相等时)。
    3. 更改程序,使其使用双精度而不是整数。
    4. 更改程序,使其写出的数字在写出后几乎相等,如果两者越大越小 数字相差不到 1.0/100。

您能给我一些提示如何执行第 6 步吗?我有一些想法,但没有一个奏效..

这是新代码:

#include "C:/std_lib_facilities.h"

int main()
{
    double smallestSoFar = std::numeric_limits<double>::max();
    double largestSoFar = std::numeric_limits<double>::min();
    double a,differ=0;
    char c=' ';
    cout << "Enter value: \n";

    while (c != '|' && cin >> a)
    {
        if (a > largestSoFar)
        {
            largestSoFar = a; 
            cout <<"Largest so far is: "<< largestSoFar << endl;
        }
        else if (a < smallestSoFar)
        {
            smallestSoFar = a;
            cout <<"Smallest so far is: "<< smallestSoFar << endl;
        }
        else if(smallestSoFar >= a && a<=largestSoFar)
            cout << a << endl;
        cout << "Enter a character | to break loop: \n";
        cin >> c;
    }
    cout << "You have exited the loop.\n";
    keep_window_open();
}

【问题讨论】:

    标签: c++ algorithm


    【解决方案1】:

    如果不使用矢量,我不知道如何正确执行此操作。

    您不需要矢量。描述正确地说两个变量就足够了:

    // Declare these variables before the loop
    double smallestSoFar = std::numeric_limits<double>::max();
    double largestSoFar = std::numeric_limits<double>::min();
    

    修改循环以读入a,而不是同时读入ab。对照smallestSoFarlargestSoFar 检查新输入的值,进行打印,并根据需要重新分配最小和最大。请注意,您第一次应该会看到两个打印输出 - 迄今为止最大的和迄今为止最小的。

    【讨论】:

    • 我已经改进了代码,你能看看上面我的第一篇文章吗?我对其进行了编辑并在那里放置了新代码。我不太了解它,但它似乎工作。代码可以吗?
    • @Michael 是的,正确的代码。祝你的项目好运!
    【解决方案2】:

    根据您在本次作业的当前阶段应该知道的知识。代码应该是这样的:

    #include < iostream>
    #include < cstdlib>
    
     int main() {
    
     double num_1 = 0;
     double num_2 = 0;
     double largest = 0;
     double smallest = 0;
    
     bool condition1 = true;
    
     while (true) {
    
        std::cin >> num_1;
    
        if (num_1 > largest){
            largest = num_1;
        }
        else if (num_1 < smallest) {
            smallest = num_1;
        }
        
        std::cout << "The largest so far: " << largest << std::endl;
    
        std::cin >> num_2;
        
        if (condition1) {
            smallest = largest;
            condition1 = false;
        }
    
        if (num_2 < smallest) {
            smallest = num_2;
        }
        else if (num_2 > largest) {
            largest = num_2;
        }
        
        std::cout << "The smallest so far: " << smallest << std::endl;
    }
    
    system("pause");
    return 0;
    }
    

    【讨论】:

      【解决方案3】:
      double large = 0;
      double small = 0;
      
      double input;
      int counter = 0;
      
      while (counter < 5) {
          cin >> input;
          cout <<"Large value: "<< large << '\t' <<"Small value: "<< small\
              << '\t' <<"Input value: "<< input << '\n';
          if (input < small) {
              cout << "The smallest value is " << input<<\
                  "\nthe largest value is "<< large<<'\n';
              small = input;
          }
      
          else if (input > small&& input < large) {
              cout << "The smallest value is " << small << \
                  "\nthe largest value is " << large<<'\n';
          }
          else if (input > small&& input > large) {
              cout << "The smallest value is " << small << \
                  "\nthe largest value is " << input << '\n';
              large = input;
          }
          counter += 1;
      

      【讨论】:

      • largesmall 初始化不正确。 large 应该在开始时尽可能小,small 在开始时应该尽可能大。
      • 复制代码时忘记初始化。大小应该如何变化?通过将 large/small = input 放在 cout 语句之前?
      猜你喜欢
      • 2023-03-18
      • 2022-01-20
      • 2023-03-23
      • 2011-01-15
      • 1970-01-01
      • 2011-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多