0

我正在用 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;
}
4

4 回答 4

2

这条线是错误的。

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

它应该是

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

请注意,检查实际上是没有意义的,因为您已经分别检查了它们。if您可以通过在, else if,else链中执行此操作来整理它。

于 2013-02-01T00:55:24.537 回答
0

由于您想为每个三角函数做一些不同的事情,所以您应该只有一个if...else if...else if...else if...else链。无需像您一样嵌套 if 语句。事实上,它可能效率较低,因为您检查每个条件两次。

于 2013-02-01T00:58:59.753 回答
0

改变:

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.
}
于 2013-02-01T00:56:07.350 回答
0

您必须分别写出每个条件。以下代码行编译,但它并没有按照你的想法做:

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

将其更改为以下内容:

if (function == "sin" || function == "cos" || function == "tan")
于 2013-02-01T00:56:08.817 回答