C++计算器跳过其他语句

C++ Calculator Skipping Else Statement

本文关键字:其他 语句 计算器 C++      更新时间:2023-10-16

我正在用C++制作一个简单的计算器。但是,该程序并没有完全按照应有的方式运行。运行时,trig if 语句执行正常,但是,基本算术 else 语句不起作用。我已经确定代码没有执行 else 语句,并且想知道如何解决它。else 语句中的代码工作正常,因为我已注释掉 if 语句。帮助?

这是我的代码:

#include "stdafx.h"
#include <iostream>
#include <string>
#include <cmath>

int main()
{
    double input = 0;
    double firstnumber = 0;
    double secondnumber = 0;

    std::string function;
    std::string operation;

    std::cout << "Enter your calculation: ";
    std::cin >> function;   

    if(function == "sin" || "cos" || "tan")
    {
        if(function == "sin")
        {
            std::cin >> input;
            std::cout << "The sine is " << sin(input) << std::endl;
            system("PAUSE");
        }
        else if(function == "cos")
        {
            std::cin >> input;
            std::cout << "The cosine is " << cos(input) << std::endl;
            system("PAUSE");
        }
        else if(function == "tan")
        {
            std::cin >> input;
            std::cout << "The tangent is " << tan(input) << std::endl;
            system("PAUSE");
        }
    }
    else
    {       
        firstnumber = ::atof(function.c_str());
        std::cin >> operation;
        std::cin >> secondnumber;

        double valueadd = firstnumber + secondnumber;
        double valuesubtract = firstnumber - secondnumber;
        double valuemultiply = firstnumber * secondnumber;
        double valuedivide = firstnumber / secondnumber;

        if(operation == "+")
        {      
            std::cout << " = " << valueadd << std::endl;
            system("PAUSE");
        }
        else if(operation == "-")
        {          
            std::cout << " = " << valuesubtract << std::endl;
            system("PAUSE");
        }
        else if(function == "*")
        {
            std::cout << " = " << valuemultiply << std::endl;
            system("PAUSE");
        }
        else if(function == "/")
        {
            std::cout << " = " << valuedivide << std::endl;
            system("PAUSE");
        }
        else
        {
            std::cout << "Error" << std::endl;
            return 0;
        }
    }
    return 0;
}

这一行是错误的。

if(function == "sin" || "cos" || "tan")

它应该是

if((function == "sin") || (function == "cos") || (function == "tan"))
请注意,检查

实际上毫无意义,因为您已经单独检查了它们。你可以通过在ifelse ifelse链中执行此操作来整理它。

您必须单独写出每个条件。以下代码行可以编译,但它不会按照您的想法执行:

if (function == "sin" || "cos" || "tan")

将其更改为以下内容:

if (function == "sin" || function == "cos" || function == "tan")

由于你想为每个trig函数做一些不同的事情,你应该只有一个if...else if...else if...else if...else链。没有必要像现在这样嵌套 if 语句。事实上,它可能效率较低,因为您需要检查每个条件两次。

更改:

if(function == "sin" || "cos" || "tan")

到:

if ((function == "sin") || (function == "cos") || (function == "tan"))

您拥有的首先计算表达式"sin" || "cos" || "tan",然后尝试将字符串与该字符串进行比较。

但是,事实上,实际上没有必要进行这两个步骤。您可以简单地执行以下操作:

if (function == "sin") {
    std::cin >> input;
    std::cout << "The sine is " << sin (input) << std::endl;
    system ("PAUSE");
} else if (function == "cos") {
    std::cin >> input;
    std::cout << "The cosine is " << cos (input) << std::endl;
    system ("PAUSE");
} else if (function == "tan") {
    std::cin >> input;
    std::cout << "The tangent is " << tan (input) << std::endl;
    system ("PAUSE");
} else {
    // It's neither sin, cos nor tan if you get here.
    firstnumber = ::atof (function.c_str ());
    // and the rest of your stuff in here.
}