【问题标题】:I am getting inf when diving a floating point number by 0 and I want instead to say "error"将浮点数乘以 0 时,我得到了 inf,而我想说“错误”
【发布时间】:2020-03-08 17:21:30
【问题描述】:

我是一名初学者 C++ 程序员,这是我的家庭作业之一,除了最后一个问题,当用户输入一个数字除以零时,它应该说“错误”,但我得到了 inf作为我的输出。我做了一个 if 语句,说 if (num1 == 0 || num2 == 0) 它会说错误,但事实并非如此!

#include <iostream>
#include <iomanip> 
using namespace std;



int main() {

    double num1 {};
    double num2 {};
    char input {};
    double result {};

    cout << "Enter your calculations: ";
    cin >> num1 >> input >> num2;

    cout << fixed << setprecision(2);

    if (input == '+') {
        result = num1 + num2;
    } else if (input == '-') {
        result = num1 - num2;
    } else if (input == '/') {
        result = num1 / num2;
    } else if (input == '*') {
        result = num1 * num2;
    } else if ( num1 == 0 || num2 == 0 ) 
        cout << "error";

     cout << "Answer: "<< result << endl;

    }

【问题讨论】:

  • 除法前需要检查零。
  • 另外你应该只测试num2。将零除以非零数是明确定义的,并得到零。
  • else if ( num1 == 0 || num2 == 0 ) 仅在上述条件均不成立时才被检查。 else if (input == '/') 进入,所以零校验用例不运行。
  • 有趣的事实:else if (condition) {} 实际上是 else { if (condition) {} } 更多阅读:Is “else if” a single keyword?
  • @user4581301 有趣的事实:它不是。那是另一种写法,但它在解析器中需要不同的路径才能被理解。

标签: c++ if-statement zero


【解决方案1】:

除以零检查应在除法输入块内。

else if (input == '/') {
    if (num1 == 0 || num2 === 0) {
        cout << "error" << endl;
    }
}

在您的代码中,由于输入是 '/',因此不会执行任何其他 else 块。

【讨论】:

    【解决方案2】:

    您可以使用std::isinf (1 / 0 =&gt; inf) 和std::isnan (0 / 0 =&gt; nan) 来检查无穷大和Not-a-Number(不管是哪个操作导致的)。您也可以使用std::isfinite 来检查前两个都不是true

    #include <cmath>     // std::isfinite
    #include <iomanip>
    #include <iostream>
    
    int main() {
        double num1{};
        double num2{};
        char input{};
        double result{};
    
        std::cout << "Enter your calculations: ";
        std::cin >> num1 >> input >> num2;
    
        std::cout << std::fixed << std::setprecision(2);
    
        if(input == '+') {
            result = num1 + num2;
        } else if(input == '-') {
            result = num1 - num2;
        } else if(input == '/') {
            result = num1 / num2;
        } else if(input == '*') {
            result = num1 * num2;
        }
    
        std::cout << "Answer: ";
    
        if(std::isfinite(result))  // using std::isfinite
            std::cout << result;
        else
            std::cout << "error";
    
        std::cout << "\n";
    }
    

    【讨论】:

      【解决方案3】:

      您需要在执行操作之前确保您的数据不是0

      一种选择是将它移到 if 语句的开头,如下所示:

      if ( num1 == 0 || num2 == 0 ) {
          std::cout << "error";
      } else if (input == '+') {
          result = num1 + num2;
      } else if (input == '-') {
          result = num1 - num2;
      } else if (input == '/') {
          result = num1 / num2;
      } else if (input == '*') {
          result = num1 * num2;
      } else { // if none of the "else"s apply, it might be nice to show an error
          std::cout << "error";
      }
      

      或者,如果你只关心/,你可以像这样在部门内进行:

      //...
      } else if (input == '/') {
          if ( num1 == 0 || num2 == 0 ) {
              std::cout << "error";
          } else {
              result = num1 / num2;
          }
      } else if (input == '*') {
      //...
      

      【讨论】:

        猜你喜欢
        • 2018-12-23
        • 2016-01-03
        • 2016-11-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-09-29
        • 2014-07-26
        • 1970-01-01
        相关资源
        最近更新 更多