首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C++常量字符

C++常量字符
EN

Stack Overflow用户
提问于 2013-05-14 07:53:22
回答 3查看 208关注 0票数 0

我用c++构建了一个简单的计算器,它使用字符,所以(+,-*,/)将作为数学运算符工作,但当用户输入'=‘时,它不起作用。

代码语言:javascript
复制
#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;
}

当用户使用=以外的任何操作时,它都能很好地工作,但如果我使用and equals sign,它就不能工作。

示例程序输出:

代码语言:javascript
复制
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...
EN

回答 3

Stack Overflow用户

发布于 2013-05-14 07:58:11

代码语言:javascript
复制
if (operation != '=') {
    ...
    if (operation == '=') {
    }
}

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

票数 5
EN

Stack Overflow用户

发布于 2013-05-14 07:57:58

语句if (operation != '=')会导致将控制转移到else语句

票数 3
EN

Stack Overflow用户

发布于 2013-05-14 07:58:01

因为您在检查=的if之外有一个if (operation != '=')。你不会想这么做的。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16532980

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档