# Evaluation of Prefix Expressions in C++

C++Server Side ProgrammingProgramming

## Prefix Expression

In this notation, operator is prefixed to operands, i.e. operator is written ahead of operands. For example, +ab. This is equivalent to its infix notation a + b. Prefix notation is also known as Polish Notation.

## Example:

* + 6 9 - 3 1

Prefix expressions are evaluated faster than infix expressions. Also, there are no brackets in prefix expressions which make it evaluate quicker.

## Algorithm to evaluate Prefix Expression:

The evaluation of prefix expression requires a stack data structure. We will push the operators in the stack and then solve the expression.

We will visit each element of the expression one by one. If the current element is an operand, we will push it to the stack. And if it is an operator, we will pop two operands, perform the operation, operand operator operand and then push the result back to the stack.

## Algorithm −

Step 1: Start from the last element of the expression.

Step 2: check the current element.

Step 2.1: if it is an operand, push it to the stack.
Step 2.2: If it is an operator, pop two operands from the stack. Perform the operation and push the elements back to the stack.

Step 3: Do this till all the elements of the expression are traversed and return the top of stack which will be the result of the operation.

## Lets see the working of our algorithm to solve an prefix expression,

Prefix Expression :

* + 6 9 - 3 1

Iteration : 1

Element scanned  => 1

Operation => push to stack
Stack => 1

Iteration : 2

Element scanned  => 3

Operation => push to stack
Stack => 3 , 1

Iteration : 3

Element scanned  => -

Operation => pop two from stack, perform operation and push back the result.

3 - 1 = 2
Stack => 2

Iteration : 4

Element scanned  => 9

Operation => push to stack
Stack => 9 , 2

Iteration : 5

Element scanned  => 6

Operation => push to stack
Stack => 6, 9 , 2

Iteration : 6

Element scanned  => +

Operation => pop two from stack, perform operation and push back the result.

6 + 9 = 15

Stack =>  15 , 2

Iteration : 7

Element scanned  => *

Operation => pop two from stack, perform operation and push back the result.

15 * 2 = 30
Stack => 30

## Example

Live Demo

#include <bits/stdc++.h>
using namespace std;

double evaluatePrefix(string prefixExp) {

stack<double> operendStack;
int size = prefixExp.size() - 1;

for (int i = size; i >= 0; i--) {

if (isdigit(prefixExp[i]))
operendStack.push(prefixExp[i] - '0');
else {
double o1 = operendStack.top();
operendStack.pop();
double o2 = operendStack.top();
operendStack.pop();
if( prefixExp[i] == '+')
operendStack.push(o1 + o2);
else if( prefixExp[i] == '-')
operendStack.push(o1 - o2);
else if( prefixExp[i] == '*')
operendStack.push(o1 * o2);
else if( prefixExp[i] == '/')
operendStack.push(o1 / o2);
else{
cout<<"Invalid Expression";
return -1;
}
}
}
return operendStack.top();
}

int main()
{
string prefixExp = "*+69-31";
cout<<"The result of evaluation of expression "<<prefixExp<<" is "<<evaluatePrefix(prefixExp);
return 0;
}

## Output −

The result of evaluation of expression *+69-31 is 30
Published on 22-Jan-2021 17:37:16