@C++程序原理与设计第四章简单练习里面比较两数差值0.01以内输出“almost equal”
在我输入验证的过程中发现(1 ,1.01)(2,2.01)(3,3.01)(4,4.01) 等等均可正确输出:
The numbers are almost equal
但是20和20.01 则会输出:
The smaller value is:20
The larger value is:20.01
请问各位大神问题出在哪里啊?
程序如下:
#include “pch.h”
#include
#include
#include
using namespace std;
int main()
{
double a, b;
cout << “Please give me your favorite numbers” << ‘\n’;
while (cin >> a, cin >> b)
if (a == ‘|’ || b == ‘|’)
break;
else if (a == b)
cout << “The numbers are equal” << ‘\n’;
else if (a > b&&a - b <= 0.01)
cout << “The numbers are almost equal” << ‘\n’;
else if (a > b)
cout << “The smaller value is:” << b << ‘\n’ << “The larger value is:” << a << ‘\n’;
else if (a < b&&b - a <= 0.01)
cout << “The numbers are almost equal” << ‘\n’;
else if (a < b)
cout << “The smaller value is:” << a << ‘\n’ << “The larger value is:” << b << ‘\n’;
}