What Is Reverse Polish Notation?

Reverse Polish Notation is also known as Postfix, where we use the operand (any numbers) before using any operator to calculate any value; it saves time during the calculations. Reverse polish notation is mainly used in electronic calculators; it helps to calculate fast for an electric calculator than the infix techniques that we learn.

VIsual demonstration of rpn
1 of 9

Explanation

The above illustration explains the working process of RPN. If we need to compute the number (25)/(2+3), the electric calculator will interpret it as 2523+/ and produce the correct answer two, and using the RPN will save time.

If we look closely, it doesn’t need any parenthesis to calculate, which saves memory for any computer or calculator that employs RPN by decreasing characters and maximizing calculation performance.

RPN using Program

The Coding widget below gives a basic implementation of RPN in C++.

#include<iostream>
#include<cmath>
#include<stack>
#include<climits>
using namespace std;
float characterToFloat(char);
int findOperator(char );
int findOperand(char);
float prnOperat(int , int , char );
float prnEval(string postfix);
int main(){
string post = "25*23+/";
cout << " Result= "<<prnEval(post);
}
float characterToFloat(char ch){
int value;
value = ch;
return float(value-'0');
}
int findOperator(char ch){
if(ch == '+'|| ch == '-'|| ch == '*'|| ch == '/' || ch == '^')
return 1;//if they found an operator
return -1;//if they didn't found an operator
}
int findOperand(char ch){
if(ch >= '0' && ch <= '9')
return 1;//character is an operand
return -1;//not an operand
}
float prnOperat(int x, int y, char operat){
//Perform prnOperat
if(operat == '+')
return y+x;
else if(operat == '-')
return y-x;
else if(operat == '*')
return y*x;
else if(operat == '/')
return y/x;
else if(operat == '^')
return pow(y,x); //find b^a
else
return INT_MIN; //return negative infinity
}
float prnEval(string postfix){
int x, y;
stack<float> prnhandler;
string::iterator it;
for(it=postfix.begin(); it!=postfix.end(); it++){
//read elements and perform postfix evaluation
if(findOperator(*it) != -1){
x = prnhandler.top();
prnhandler.pop();
y = prnhandler.top();
prnhandler.pop();
prnhandler.push(prnOperat(x, y, *it));
}
else if(findOperand(*it) > 0){
prnhandler.push(characterToFloat(*it));
}
}
return prnhandler.top();
}
  • Lines 6-10: We declare some which will be used in the algorithm all these functions will be defined after the main function

  • Lines 11-14: It is the main function of the program where we pass a value 25*23+/ to get it processed

  • Lines 15-64: We define all the functions which will be helpful in the implementation of the RPN.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved