0

私はC++で簡単な電卓を作っていました。ただし、プログラムは正常に機能しません。実行すると、trig ifステートメントは正常に実行されますが、基本的な算術elseステートメントは機能しません。コードがelseステートメントを実行していないと判断し、それを修正する方法を考えていました。ifステートメントをコメントアウトしたので、elseステートメント内のコードは正常に機能します。ヘルプ?

これが私のコードです:

#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(function == "sin" || "cos" || "tan")

の中へ:

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

最初に式を計算して"sin" || "cos" || "tan"から、文字列をそれと比較しようとします。

しかし、実際には、この2段階のプロセスを用意する必要はありません。あなたは単にこのようなことをすることができます:

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 に答える
0

三角関数ごとに異なることをしたいので、if...else if...else if...else if...elseチェーンは1つだけにする必要があります。あなたが持っているようにifステートメントをネストする必要はありません。実際、各条件を2回チェックするため、おそらく効率が低下します。

于 2013-02-01T00:58:59.753 に答える