确定两个数字是否"almost equal"并输出结果

Determining if two numbers are "almost equal" and outputting the result

本文关键字:almost equal 结果 输出 是否 数字 两个      更新时间:2023-10-16

请注意,我刚刚开始学习c++。

我是从Bjarne Stroustrup的书"编程原理和实践使用c++第二版",并在第4章的末尾练习。

到目前为止的说明如下:

  1. 编写一个由while-loop组成的程序,该程序(每次循环一次)读入两个int型,然后输出它们。

  2. 当输入终止` | `时退出程序。
  3. 修改程序,写出"较小的值是:"后面跟着较小的数,"较大的值是:"后面跟着较大的数。

  4. 扩充程序,使其只在数字相等时才写出"the numbers are equal"行

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

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

感谢您花时间阅读这篇文章

这可能就是它的意思

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

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

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

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

这里我使用绝对值的最大值作为参考。你也可以使用平均值,但取(绝对值)任何一个值都是不明智的,因为它不是对称的。

我对分配的理解如下:

#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: ";
    }
}

这是一个死话题,但我认为我应该澄清这一点。两个数之间的差是最大的数和最小的数相减的结果。因此,您需要声明两个变量来跟踪每个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;

这是我的版本:

#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 equaln";
        }
        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 equaln";
        }
        else {
            cout << "The numbers are equaln";
        }
    }
    return 0;
}
#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 equaln";
    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;
}

虽然这是一个古老的线程,但我在阅读同一本书时遇到了它。如果有更多的学生做同样的事情,下面是我想到的使用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";
            }
        }
    }
}

读到这里,你也应该已经了解了函数,并且可以在这里使用它们来保持主函数的整洁。

"修改程序,使其写出"两个数字几乎相等";

如果两个数字相差小于1.0/100,则写出哪个较大,哪个较小。

你不明白作者在问什么的原因是因为这个问题是基于特别纯粹的基础数学语言(我很难理解"math";单词问题也是)。这是一个你只有理解数学语法才能理解的问题,完成这本书:《实用代数:自学指南》。读完那本书,然后读Bjarne的c++,如果你不能理解基本的数学语法,特别是对于核心编程语言c++,那将是非常困难的。

"数字几乎相等"

这是一种修辞手法,并不意味着有一个专门的c++函数或库来处理它。

"如果两个数字相差小于1.0/100;

字面意思是两个数的差等于1.0/100,即0.01,这意味着例如if(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++书。

这对我来说一直很有效。

#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 equaln";

}