C ++ Constant char

时间:2013-05-13 23:53:22

标签: c++ variables if-statement chars

我用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...

3 个答案:

答案 0 :(得分:5)

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

如果操作不等于“=”并且它等于“=”。我认为您打算在第一个外部if中放置一个 闭包运算符 或类似的东西。

答案 1 :(得分:3)

您的语句if (operation != '=')会将控件转移到else语句

答案 2 :(得分:1)

因为如果检查if (operation != '='),则=之外有{{1}}。你不想那样做。