我正在尝试制作一个计算器来进行操作并转换为后缀,但该程序正在读取双数字和两个不同的数字[关闭]

问题描述 投票:0回答:0

enter image description here 代码将读取 33+3-3 并使其成为 33 3 + 3 -(后缀版本),但问题是程序给我的结果是 3,这是错误的,它应该是 33(原因 3 和 - 3相互抵消) ,我认为错误出在 InfijiAposfijo 中,但不确定什么是好的解决方案

我试过想办法用一个临时字符串把所有连续的数字累加起来,然后把它们转换成一个整数,发现有空格的时候把它们加到后缀结果中。但是我没有成功

#include <iostream>
#include <stack>
#include <string>
#include <sstream>
using namespace std;

int PrioOperador(char op) {
    if (op == '*' or op == '/')
        return 2;
    else if (op == '+' or op == '-')
        return 1;
    else
        return 0;
}

string InfijoAposfijo(string expresion) {

    stack<char> StackOperador;
    string postfix = "";

    bool UltimoDigito = false;
    for (int i = 0; i < expresion.size(); i++) {
        if (expresion[i] >= '0' && expresion[i] <= '9') {
            postfix += expresion[i];
            UltimoDigito = true;
        }

        else if (expresion[i] == '+' or expresion[i] == '-' or expresion[i] == '*' or expresion[i] == '/') {
            if (UltimoDigito) {
                postfix += ' ';
                UltimoDigito = false;
            }
            while (!StackOperador.empty() && StackOperador.top() != '(' && PrioOperador(StackOperador.top()) >= PrioOperador(expresion[i])) {
                postfix += StackOperador.top();
                StackOperador.pop();
            }
            StackOperador.push(expresion[i]);
        }
        else if (expresion[i] == '(') {
            StackOperador.push(expresion[i]);
        }
        else if (expresion[i] == ')') {
            while (!StackOperador.empty() && StackOperador.top() != '(') {
                postfix += StackOperador.top();
                StackOperador.pop();
            }
            StackOperador.pop();
        }
    }
    if (UltimoDigito) {
        postfix += ' ';
    }
    while (!StackOperador.empty()) {
        postfix += StackOperador.top();
        StackOperador.pop();
    }
    return postfix;
}

float evaluatePostfix(string postfix) {
    stack<float> operandStack;
    for (int i = 0; i < postfix.size(); i++) {
        if (postfix[i] >= '0' && postfix[i] <= '9') {
            stringstream ss;
            ss << postfix[i];
            float num;
            ss >> num;
            operandStack.push(num);
        }
        else if (postfix[i] == '+' or postfix[i] == '-' or postfix[i] == '*' or postfix[i] == '/') {
            float num2 = operandStack.top();
            operandStack.pop();
            float num1 = operandStack.top();
            operandStack.pop();
            switch (postfix[i]) {
            case '+':
                operandStack.push(num1 + num2);
                break;
            case '-':
                operandStack.push(num1 - num2);
                break;
            case '*':
                operandStack.push(num1 * num2);
                break;
            case '/':
                if (num2 == 0) {
                    cout << "No se puede dividir por cero.";
                    return 0;
                }
                operandStack.push(num1 / num2);
                break;
            }
        }
    }
    return operandStack.top();
}

int main() {
    string infix;
    cout << "Ingrese una operacion infija: ";
    getline(cin, infix);

    string postfix = InfijoAposfijo(infix);
    cout << "Operacion posfija: " << postfix << endl;

    float result = evaluatePostfix(postfix);
    cout << "Resultado: " << result << endl;

    return 0;
}
c++ calculator postfix-mta infix-notation
© www.soinside.com 2019 - 2024. All rights reserved.