Click here to Skip to main content
15,867,995 members
Articles / Programming Languages / Python

Reverse Polish Notation (RPN) Calculator in Python

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
9 Nov 2022CPOL2 min read 10.2K   111   4   6
How to create a Reverse Polish Notation (RPN) Calculator using Python
This article is the demonstration of a stack based RPN calculator. In this article, first we will look at how to create a stack class with the basic push and pop operations and then we will see how this stack class can be used to evaluate postfix expressions.

Introduction

Through this article, I wish to demonstrate creating a Reverse Polish Notation (RPN) Calculator which can be used to evaluate postfix expressions. In a postfix operation, an operator appears after its operands. For example, an infix expression like 25 + 12 would be written as 25 12 + in the postfix notation. A postfix expression is evaluated in the order in which the operations appear (left to right).

For this, we need to define a stack class. This stack class will be the container for the operands as well as intermediate results. The final result also gets stored in the stack and is extracted at the end of the process and displayed.

Background

RPN Calculator, also known as Stack Calculator, is a special type of calculator in which there must be two operands before an operator in an expression. The RPN calculator works by pushing operands into a stack until an operator is encountered. When an operator is encountered, the last two pushed operands are popped, the required operation is performed, and the result of the operation is again pushed into the stack. At the end of the expression, the last value is popped from the stack and displayed as the final result.

Using the Code

Following is the code for the Node class which is the building block for our Stack class. It defines a constructor and the required getter and setter methods.

Python
# Representing the node for the stack.
# The node class defines a constructor and the required getter and setter methods.

class Node:

   def __init__(self,d):
       self.data = d

   def setnext(self,n):
       self.next = n

   def getdata(self):
       return self.data

   def getnext(self):
       return self.next

Following is the code for the Stack class. It defines a constructor and the push and pop operations in addition to a method to check if the stack is empty.

Python
# Representing the stack class.
# The stack class defines a constructor and the implementations for the
# push and pop operations. It also contains a method to check if the stack is empty.

class Stack:

   def __init__(self):
       self.top = None

   def push(self,d):
       self.newnode = Node(d)
       self.newnode.setnext(self.top)
       self.top = self.newnode

   def pop(self):
       temp = self.top
       self.top = self.top.getnext()
       n = temp.getdata()
       del temp
       return n

   def isempty(self):
       return self.top == None

Following is the code for the main program. The main program involves accepting input from the user and performing the calculations by using the functions of our stack class. The expression entered to be evaluated must have a space between two operands as well as between an operand and operator, for example, 78 100 + 200 - 5 *

IMPORTANT: If at least one space is not used between elements of the expression, you may get unexpected results.

The code uses the sub() function of the regular expression module (re) to subsitute multiple spaces in the postfix expression with one space and the split() function to parse the expression and extract the elements of the expression (operators and operands) into a list. The strip() function is used to remove the leading and trailing spaces. It pushes the operands into the stack and pops the last two operands and pushes their result when an operator is encountered. At the end, it pops and displays the value from the stack as the final result.

Python
import re

if __name__ == "__main__":
    try:
        mystack = Stack()
        expr = input("Enter expression with space between numbers and operators: ")
        expr = re.sub(" +"," ",expr)
        expr = expr.strip()
        elements = re.split(r"[\s]",expr)
        for x in elements:
            if x == "+":
                n1 = int(mystack.pop())
                n2 = int(mystack.pop())
                n3 = n2 + n1
                mystack.push(n3)
            elif x == "-":
                n1 = int(mystack.pop())
                n2 = int(mystack.pop())
                n3 = n2 - n1
                mystack.push(n3)
            elif x == "*":
                n1 = int(mystack.pop())
                n2 = int(mystack.pop())
                n3 = n2 * n1
                mystack.push(n3)
            elif x == "//":
                n1 = int(mystack.pop())
                n2 = int(mystack.pop())
                n3 = n2 // n1
                mystack.push(n3)
            elif x == "/":
                n1 = int(mystack.pop())
                n2 = int(mystack.pop())
                n3 = n2 / n1
                mystack.push(n3)
            else:
                mystack.push(x)
        print("Result: " + str(mystack.pop()))
    except AttributeError as e:
        print("Invalid Expression: " + str(e))

Image 1

History

  • 10th November, 2022: Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Instructor / Trainer NIIT, India
India India
I am a trainer by profession. Currently I am working with iFuture Technologies(India) as a Senior Faculty. I enjoy programming as a hobby. During my career I have seen the growth and decline of many technologies, many of them being my favorites like Flash, WPF, Windows Mobile Development. Few of my current favorites are Android, Xamarin and Python, though I also like traditional and evergreen languages like PHP, C#, Visual Basic and Java.

Apart from computers, my favorite pastime is bicycling.

Comments and Discussions

 
QuestionTest example Pin
Member 960826310-Nov-22 21:36
Member 960826310-Nov-22 21:36 
AnswerRe: Test example Pin
Harald Reichert11-Nov-22 0:39
professionalHarald Reichert11-Nov-22 0:39 
GeneralRe: Test example Pin
Azim Zahir11-Nov-22 4:18
Azim Zahir11-Nov-22 4:18 
AnswerRe: Test example Pin
Azim Zahir11-Nov-22 4:12
Azim Zahir11-Nov-22 4:12 
GeneralRe: Test example Pin
Member 960826311-Nov-22 10:59
Member 960826311-Nov-22 10:59 
GeneralRe: Test example Pin
Azim Zahir12-Nov-22 7:08
Azim Zahir12-Nov-22 7:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.