确定两个数字是否“几乎相等”并输出结果

时间:2014-07-28 16:32:11

标签: c++

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

我的工作是Bjarne Stroustrup的书"编程原则与实践使用C ++第2版"并且在第4章末​​尾的钻孔中。

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

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

  2. 更改程序以写出"较小的值为:"接着是较小的数字,而较大的值是:"其次是较大的值。

  3. 增加程序,使其写入行"数字相等" (仅)如果他们是平等的。

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

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

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

9 个答案:

答案 0 :(得分:2)

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

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

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

答案 1 :(得分:2)

这可能意味着什么

if(std::abs(v1-v2)<0.01)
@Walter的回答也可能是正确的。这取决于原始问题的意图,这一点并不十分清楚。例如,如果使用@Walter的版本,那么对于v1 = v2 = 0.0,程序会说它们没有关闭。

答案 2 :(得分:0)

我已通过以下方式理解作业:

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

答案 3 :(得分:0)

这是一个死的话题,但我想我应该澄清一下。 2个数字之间的差异是最大数字和最小数字之间减法的结果。因此,您需要声明2个变量来跟踪哪个是最小的,哪个是if-else语句中最大的变量。因此代码应如下所示。

<link href="http://code.jquery.com/ui/1.11.3/themes/smoothness/jquery-ui.min.css" rel="stylesheet"/>
<script src="http://code.jquery.com/jquery-1.11.3.js"></script>
<script src="http://code.jquery.com/ui/1.11.3/jquery-ui.js"></script>


<p>
<div id="dialog"></div>
<a class="dialogify left_menu" href="testpopup.html">Test</a>

答案 4 :(得分: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 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;
}

答案 5 :(得分: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 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;
}

答案 6 :(得分: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";
            }
        }
    }
}

到这时,您也应该已经介绍了功能,并可以在此处使用它们以使您的主要功能保持整洁。

答案 7 :(得分:0)

“ 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.
    }

由于您正在从其创建者那里阅读一本C ++书,因此您也应该得到一个cookie。

答案 8 :(得分:-1)

这对我有用。

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

}