Evaluation of Prefix Expressions in C++

In this article, we will discuss the evaluation of 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.
For more read.
* + 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.
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.
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.
Prefix Expression : 
* + 6 9 - 3 1
Iteration : 1
Element scanned  => 1
Operation => push to stackStack => 1
Iteration : 2
Element scanned  => 3
Operation => push to stackStack => 3 , 1
Iteration : 3
Element scanned  => -
Operation => pop two from stack, perform operation and push back the result.
3 - 1 = 2Stack => 2
Iteration : 4
Element scanned  => 9
Operation => push to stackStack => 9 , 2
Iteration : 5
Element scanned  => 6
Operation => push to stackStack => 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 = 30Stack => 30
End => return top of stack, result = 30.
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;
}
The result of evaluation of expression *+69-31 is 30