0

i'm searching for something that explains how i can calculate an Polish Expression, example:

if i have this ((1+2)*4)+3, in normal way is 1+2*4+3 = 15, but i need to write this way: 12+4*3+ to using stack getting the value of top and putting in the stack again, see my code : https://ideone.com/0bdkkM

i already see one post but i dont understand how i can make the operations required: StackOverflow

Community
  • 1
  • 1
Gabriel Rodrigues
  • 510
  • 1
  • 8
  • 29

2 Answers2

1

The reason Reverse Polish became popular is because it maps nicely to the computer concept of a stack.

When the user enters a number, push it on the stack.

When the user enters an operator, pop 2 numbers from the stack, calculate the result, and push the result back on the stack.

Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
  • You also need to consider operator precedence – Sean Apr 08 '15 at 13:26
  • 3
    @Sean no you don't, that's what makes Reverse Polish so simple. The user is responsible for rearranging the expression for precedence. – Mark Ransom Apr 08 '15 at 13:27
  • It does if you're converting the expression to RPN. – Sean Apr 08 '15 at 13:30
  • 2
    @Sean the question was about *calculating* a Reverse Polish expression, not *converting* one. – Mark Ransom Apr 08 '15 at 13:32
  • you have some example in code ? in my code i do one dynamics stack to receive the numbers, them i create one vector to get the operators, but i need to create another dynamic stack or using the same dynamics stack understand ? – Gabriel Rodrigues Apr 08 '15 at 13:35
  • 3
    @GabrielRodrigues do the calculations as you accept the operators, no need to store them. – Mark Ransom Apr 08 '15 at 13:41
1

Here's a simple RPN evaluator, without any error handling. You only need a stack to store the operands, not the operators, which makes it pretty easy to implement.

Note that this version assumes the operands are single digit numbers on the input expression. I've done this to simplify parsing the RPN expression. In real life you'd want to handle multi-digit operands.

std::stack<int> stack;

const char *expression="12+4*3+";

for(char c=*expression; c!=0; c=*expression++)
{
    switch(c)
    {
        case '+':
        {
            int rhs=stack.top();    stack.pop();
            int lhs=stack.top();    stack.pop();
            int result=lhs+rhs;
            stack.push(result);
            break;
        }

        case '-':
        {
            int rhs=stack.top();    stack.pop();
            int lhs=stack.top();    stack.pop();
            int result=lhs-rhs;
            stack.push(result);
            break;
        }

        case '*':
        {
            int rhs=stack.top();    stack.pop();
            int lhs=stack.top();    stack.pop();
            int result=lhs*rhs;
            stack.push(result);
            break;
        }

        case '/':
        {
            int rhs=stack.top();    stack.pop();
            int lhs=stack.top();    stack.pop();
            int result=lhs/rhs;
            stack.push(result);
            break;
        }

        default:
            int number=(c-'0');
            stack.push(number);
            break;
    }
}

int final_result=stack.top();
std::cout << "result is " << final_result << std::endl;
Sean
  • 60,939
  • 11
  • 97
  • 136