C++ 常量字符

标签 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...

最佳答案

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

如果运算不等于“=”,如果等于“=”。我认为您计划在第一个外部 if 中放置一个闭包运算符 或类似的东西。

关于C++ 常量字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16532980/

相关文章:

c++ - 在控制结构 block 中定义变量

c++ - 在哪里可以找到有关 C++/STL 方法异常保证的信息?

php - array_intersect 可变数量的数组

python - 根据 txt 文件的搜索结果分配变量值

java - RadioButtons 和 boolean 值连接出现问题

sql - 格式化日期中的 If/Then/Else

c++ - 指定具有 3 个组件的 STL 映射键

c++ - 在 CPP 中运行 tensorflow 模型

java - 如何使用try block 中声明的变量?

python - 检查数字属于哪个特定范围(在许多范围内)的最pythonic方法是什么?