0

私は単純な計算機を C++ から作成しました。これは文字を使用するため、(+,-*,/) は数学演算子として機能しますが、ユーザーが「=」を入力すると機能しません。

#include <iostream>
#include <string>
#include <sstream>
#include <math.h>  
using namespace std;

#define PI 3.14159265359
#define NEWLINE '\n' 


void printf(string string)
{ 
  cout << string;
}

int main ()
{
char operation;
double a,b,c, value;
double answer = 0;
bool more = true;

cout << "Welcome to My Calculator\n\nInput a value: ";
cin >> answer;
operations:

cin >> operation;

if (operation != '=') {
    if (operation == '+') {
        cin >> value;
        cout << "\n" << answer << " " << operation << " " << value << "\n";
        answer += value;
        cout << "Equals " << answer << "\n";
        cout << answer << " - New Operation? ";
        goto operations;
    }
    if (operation == '-') {
            cin >> value;
            cout << "\n" << answer << " " << operation << " " << value << "\n";
            answer -= value;
            cout << "Equals " << answer << "\n";
            cout << answer << " - New Operation? ";
            goto operations;
        }
    if (operation == '*') {
            cin >> value;
            cout << "\n" << answer << " " << operation << " " << value << "\n";
            answer *= value;
            cout << "Equals " << answer << "\n\n";
            cout << answer << " - New Operation? ";
            goto operations;
        }
    if (operation == '/') {
                cin >> value;
                cout << "\n" << answer << " " << operation << " " << value << "\n";
                answer /= value;
                cout << "Equals " << answer << "\n\n";
                cout << answer << " - New Operation? ";
                goto operations;
            }
    if (operation == '^') {
                    cin >> value;
                    cout << "\n" << answer << " " << operation << " " << value << "\n";
                    answer = pow(answer, value);
                    cout << "Equals " << answer << "\n\n";
                    cout << answer << " - New Operation? ";
                    goto operations;
                }
    if (operation == '=') {
                    cout << "\nFinal Answer = " << answer << "\n\nNew operation [yes/no]: ";
                    string check;
                    cin >> check;
                    if (check == "yes") {
                        cout << "\nInput value: ";
                        cin >> answer; 
                        cout << "\n";
                        goto operations;
                    } else {
                        cout << "\nGoodbye for now...\n";
                        return 0;
                    }

                }
} else {
    cout << "Unknown Error! Program Closing...";
    return 0;
}


return 0;
}

ユーザーが = 以外の操作を使用すると完全に機能しますが、等号を使用すると機能しません。

プログラムの出力例:

Welcome to My Calculator

Input a value: 4
+4

4 + 4
Equals 8
8 - New Operation? - 3

8 - 3
Equals 5
5 - New Operation? * 5

5 * 5
Equals 25

25 - New Operation? /2

25 / 2
Equals 12.5

12.5 - New Operation? ^2

12.5 ^ 2
Equals 156.25

156.25 - New Operation? =
Unknown Error! Program Closing...
4

3 に答える 3

5
if (operation != '=') {
    ...
    if (operation == '=') {
    }
}

操作が「=」と等しくなく、「=」と等しい場合。最初の外側の if にクロージャ演算子などを入れる予定だったと思います。

于 2013-05-13T23:58:11.267 に答える
3

あなたのステートメントif (operation != '=')により、制御がelseステートメントに転送されます

于 2013-05-13T23:57:57.720 に答える
1

if (operation != '=')をチェックする場合の外側にあるためです=。あなたはそれをしたくありません。

于 2013-05-13T23:58:00.780 に答える