【问题标题】:Determining if two numbers are "almost equal" and outputting the result确定两个数字是否“几乎相等”并输出结果
【发布时间】:2014-07-28 16:32:10
【问题描述】:

请注意,我刚刚开始使用 C++。

我正在阅读 Bjarne Stroustrup 的《Programming Principles and Practice Using C++ 2nd Edition》一书,并在第 4 章末尾进行了练习。

这些是到目前为止的说明:

  1. 编写一个由 while 循环组成的程序,该循环(每次围绕循环)读取两个整数,然后打印它们。终止时退出程序'|'已输入。

  2. 更改程序,写出“较小的值是:”后跟较小的数字,“较大的值是:”后跟较大的值。

  3. 扩充程序,使其写入“数字相等”行(仅当它们相等时)。

  4. 更改程序,使其使用双精度而不是整数。

5.更改程序,使其在写出两个数字相差小于 1.0/100 时哪个更大或更小后写出“数字几乎相等”。

这就是我卡住的地方。

首先,我不明白“如果两个数字相差小于 1.0/100”是什么意思。方法。这是否意味着如果这两个数字彼此相距在 100 个数字以内?

其次,如何判断“如果两个数字相差小于 1.0/100”? (解决方案越简单越好。)

到目前为止,这是我的代码:

#include "std_lib_facilities.h"

int main()
{
    double v1 = 0, v2 = 0;
    cout << "Enter two numbers: ";
    while(cin >> v1 >> v2)
    {
        if (v1 > v2)
            cout << "The smaller of the two numbers is: " << v2 << "\n";
        else if (v1 == v2)
            cout << "The numbers are equal. \n";
        else
            cout << "The smaller of the two numbers is: " << v1 << "\n";

        cout << "Enter two numbers: ";
    }
}

感谢您抽出宝贵时间阅读本文。

【问题讨论】:

  • 我相信这意味着,如果差值小于 0.01
  • “差异小于 1.0/100”表示“它们的差异小于 0.01”。
  • 和不同可能意味着最大减去最小
  • 1.0/100 表示绝对 0.01 或相对 1%。我认为后者会是更合乎逻辑的解释。
  • @hyde 那我会说 1%

标签: c++


【解决方案1】:

可能就是这个意思

if(std::abs(v1-v2)<0.01)

@Walter 的回答也可能是正确的。这取决于原始问题的意图,这不是很清楚。例如,如果使用 @Walter 的版本,那么对于 v1 = v2 = 0.0,程序会说它们不接近。

【讨论】:

    【解决方案2】:

    我认为这意味着两个浮点数之间的相对差异小于百分之一,即

     if(std::abs(x-y)<0.01*std::max(std::abs(x),std::abs(y)))
    

    这里我使用绝对值的最大值作为参考。您也可以使用均值,但取其中一个值的(绝对值)不太可取,因为它不是对称的。

    【讨论】:

    • ...为什么要乘以两个值中的最大值?直到 std::max...
    • 我认为他把它当作一个百分比。
    • @Josh 该公式采用数字的差异,然后测试差异是否小于数字绝对值中较大者的 1%。对我来说看起来不错,对1.0/100 的最合乎逻辑的解释。
    • @hyde 啊抱歉,我以为他只是希望差异小于 0.01,并且无法辨别您在做什么。将 -1 更改为 +1 :)
    【解决方案3】:

    我是这样理解这个任务的:

    #include "std_lib_facilities.h"
    
    int main()
    {
        double v1 = 0, v2 = 0;
    
        cout << "Enter two numbers: ";
    
        while(cin >> v1 >> v2)
        {
            if ( v1 < v2 )
            {
                cout << "The smaller of the two numbers is the first number: " << v1 << "\n";
                cout << "The largest of the two numbers is the second number: " << v2 << "\n";
            }
            else if ( v2 < v1 )
            {
                cout << "The smaller of the two numbers is the second number: " << v2 << "\n";
                cout << "The largest of the two numbers is the first number: " << v1 << "\n";
            }
    
            if ( abs( v1 - v2) < 1.0 / 100 )
            {
                cout << "The numbers are almost equal. \n";
            }
    
            cout << "Enter two numbers: ";
        }
    }
    

    【讨论】:

      【解决方案4】:

      这是一个死胡同,但我想我应该澄清一下。两个数之差是最大数和最小数相减的结果。因此,您需要声明 2 个变量来跟踪每个 if-else 语句中哪个是最小的,哪个是最大的。因此代码应该是这样的。

      #include "std_lib_facilities.h"
      
      int main()
      {
      double v1 = 0, v2 = 0, larger, smaller;
      cout << "Enter two numbers: ";
      while(cin >> v1 >> v2)
      {
          if (v1 > v2)
      {            
              cout << "The smaller of the two numbers is: " << v2 << "\n";
                   smaller = v2;
                   larger = v1;
      }
          else if (v1 == v2)
      {
              cout << "The numbers are equal. \n";
      }        
          else if(v1 < v2)
      {
              cout << "The smaller of the two numbers is: " << v1 << "\n";
                   smaller = v1;
                   larger = v2;
      }
          if((larger-smaller)<1.0/100)
      {
              cout << "the numbers are almost equal" << endl;
      }
          cout << "Enter two numbers: ";
      }
      }
      return 0;
      

      【讨论】:

        【解决方案5】:

        这是我的版本:

        #include "std_lib_facilities.h" // Custom header file for this book    
        int main() {
        
            double a, b;
        
            while (cin >> a >> b) {
                if (a < b) {
                    cout << "The smaller value is:  " << a << '\n'
                        << "The larger value is: " << b << '\n';
        
                    if ((b - a) < (1.0 / 100))
                        cout << "The numbers are almost equal\n";
                }
        
                else if (a > b) {
                    cout << "The smaller value is:  " << b << '\n'
                        << "The larger value is: " << a << '\n';
        
                    if ((a - b) < (1.0 / 100))
                        cout << "The numbers are almost equal\n";
        
                }
        
                else {
                    cout << "The numbers are equal\n";
                }
            }
            return 0;
        }
        

        【讨论】:

        • 欢迎来到 Stack Overflow!一般来说,如果答案包含对代码的用途的解释,以及为什么在不介绍其他人的情况下解决了问题,那么答案会更有帮助。
        【解决方案6】:
        #include <iostream>
        #include <string>
        using namespace std;
        int main(){
            double n1, n2;
            double smaller, larger;
            double differ=1.0/100;   
            string small ="The smallest number is: ";
            string large ="The largest number is: ";
            string equal ="\nBoth numbers are almost equal\n";
        
            while(cin>>n1>>n2){
                if (n1==n2){
                    cout<<"Both number are abslute equal!!!";
                    continue;
                }
                else if(n1>n2){
                    cout<<small<<n2<<" ";smaller=n2;larger=n1;}                     
                else if (n1<n2) {
                    cout<<large<<n2<<" ";larger=n2;smaller=n1;}                                                               
        
               if(larger-smaller<=differ)
                   cout<<equal;               
            }
        return 0;
        }
        

        【讨论】:

        • 能否请您添加一些解释如何回答这个问题?
        • 首先,两个数字之间的每次比较都会产生三个逻辑(大于小于或等于),所以,我从相等条件开始以消除任何混淆,问题忽略这种情况,最后我把最后一个条件作为问题提出。
        【解决方案7】:

        尽管这是一个旧线程,但我在阅读同一本书时遇到了它。如果有更多学生这样做,这就是我想出的使用 1.0 / 100 作为文字 0.01 的方法:

        #include "../../std_lib_facilities.h"
        
        double lower(double int1,  double int2) //function to calculate the smallest of two doubles
        {
            if (int1 > int2)
                return int2;
            else
                return int1;
        }
        
        double upper(double int1, double int2)  //function to calculate the largest of two doubles
        {
            if (int1 < int2)
                return int2;
            else
                return int1;
        }
        
        int main()
        {
            double val1, val2;
            while (cin >> val1 >> val2)
            {
                double smallest = lower(val1, val2);
                double largest = upper(val1, val2);
                if (val1 == val2)
                {
                    cout << "The values are equal.\n";
                }
                else
                {
                    cout << "The smaller value is: " << smallest << "\n";
                    cout << "The larger value is: " << largest << "\n"; 
                    if (abs(largest - smallest) < 1.0 / 100)
                    {
                        cout << "The numbers are almost equal.\n";
                    }
                }
            }
        }
        

        到本书的这一点,你也应该已经涵盖了函数,并且可以在这里使用它们来保持你的 main 函数的整洁。

        【讨论】:

          【解决方案8】:

          "5. 更改程序,使其在写出两个数相差小于 1.0/100 时哪个更大和更小后写出“数字几乎相等”。"

          你不明白作者在问什么的原因是因为这个问题是基于特别纯粹的基本数学演讲(我也很难理解“数学”字题)。这是你只有理解数学语法才能理解的问题之一,完成这本书:“实用代数:自学指南”完成那本书然后阅读 Bjarne 的 c++,如果你能,这将是非常困难的'不懂基本的数学语法,专门用于硬核编程语言 c++。

          “数量差不多”

          它是一个比喻,并不意味着它有一个特殊的 C++ 函数或库。

          “如果两个数字相差小于 1.0/100。”

          字面意思是两个数字的差等于 1.0/100,即 0.01,例如,如果 (9.99 - 10) = 0.01。但是这个问题还有很多棘手的问题,不仅作者要求差异为 0.01,而且差异必须小于 0.01。所以基本上你可以写出如下公式:

          double a;  // use the built-in type keyword "double" since the values are in decimal.
          double b;
              
          if ((a - b) < 0.01)
          
              {
                 //statement code goes here.
              }
          

          此外,您应该得到一个 cookie,因为您正在阅读其创建者的 C++ 书籍。

          【讨论】:

            【解决方案9】:

            这对我有用。

            #include <iostream>
            #include <string>
            #include <algorithm>
            #include <cmath>
            
            using namespace std;
            
            int main()
            {
              double x=0;
              double y=0;
              while(cin >> x ,cin >> y)
              if (x > y){
                cout << "The larger value is " << x << " the smaller value is " << y << "\n";
                if (x - y < (1.0/100)) {
                    cout << "the numbers are almost equal" << endl;
                   }
                   }
                   else if (x < y){
                    cout << "The larger value is " << y << " the smaller value is " << x << "\n";
            
                     if (y - x < (1.0/100)) {
                    cout << "the numbers are almost equal" << endl;
                   }
                   }
                 else if (x == y)
                     cout << "the numbers must be equal\n";
            

            }

            【讨论】:

              猜你喜欢
              • 2018-06-07
              • 1970-01-01
              • 2014-06-26
              • 2018-05-26
              • 2015-10-17
              • 1970-01-01
              • 2019-04-20
              • 2014-04-08
              • 2020-09-16
              相关资源
              最近更新 更多