You are on page 1of 5

COLLEGE OF ENGINEERING & TECHNOLOGY

UNIVERSITY OF SARGODHA

CE 416: Data Structure and Algorithms (Lab)

Lab 8 Manual
Postfix Expression Evaluation

Instructor & Demonstrator: Engr. Nauman Ahmad Tariq

Student Name OBAID UR REHMAN

Roll No. ELEN51F20R010

Date Performed

Marks obtained

Instructor Signature
CLO-1 Illustrate understanding of key concepts of Data structures
and Algorithms using Python PyCharm Platform
CLO-2 Design a programming application by applying concepts of
Data Structures and algorithms leading to the solution of a moderate
scale-programming problem.
CLO-3 Write lab notes, effective communication and the analysis of
the given problem to perform in the laboratory environment.
CLO-4 Demonstrate involvement in the Project as a team or
individually with respect to the contribution.
OBJECTIVE:
Objective of this experiment is to design, develop and implement a Python code for the evaluation of a
postfix expression.

ABOUT THE EXPERIMENT:


Postfix expression:
The expression of the form a b op. When an operator is followed for every pair of operands.

Why postfix representation of the expression?


The compiler scans the expression either from left to right or from right to left.

Consider the below expression: a op1 b op2 c op3 d


If op1 = +, op2 = *, op3 = +

The compiler first scans the expression to evaluate the expression b * c, then again scan the expression to
add a to it. The result is then added to d after another scan.

The repeated scanning makes it very in-efficient. It is better to convert the expression to postfix(or prefix)
form before evaluation.

The corresponding expression in postfix form is: abc*+d+. The postfix expressions can be evaluated easily
using a stack.
The Postfix notation is used to represent algebraic expressions. The expressions written in postfix form are evaluated
faster compared to infix notation as parenthesis are not required in postfix.

ALGORITHM:
1) Create a stack to store operands (or values).
2) Scan the given expression and do following for every scanned element.
a) If the element is a number, push it into the stack
b) If the element is a operator, pop operands for the operator from stack. Evaluate the operator
and push the result back to the stack
3) When the expression is ended, the number in the stack is the final answer
Python Program Code:
def scan_num(ch):
value = ord(ch)
return float(value - ord('0'))

def is_operator(ch):
if ch in ['+', '-', '*', '/', '^']:
return 1
return -1

def is_operand(ch):
if '0' <= ch <= '9':
return 1
return -1

def operation(a, b, op):


if op == '+':
return b + a
elif op == '-':
return b - a
elif op == '*':
return b * a
elif op == '/':
return b / a
elif op == '^':
return pow(b, a)
else:
return float('-inf')

def postfix_eval(postfix):
stk = []
for char in postfix:
if is_operator(char) != -1:
a = stk.pop()
b = stk.pop()
stk.append(operation(a, b, char))
elif is_operand(char) > 0:
stk.append(scan_num(char))
return stk[0]

if __name__ == "__main__":
post = "(10-3*34+8/4)*"
print(postfix_eval(post))
OUTPUT:

3.0

Process finished with exit code 0


Assessment Rubric for Lab 8
Method of Evaluation: Lab report.

Outcomes Assessed:

CLO-1 Illustrate understanding of key concepts of Data structures and Algorithms using
Python PyCharm Platform.
CLO-2 Design a programming application by applying concepts of Data Structures and
algorithms leading to the solution of a moderate scale-programming problem.
CLO-3 Write lab notes, effective communication and analysis of the given problem to perform
in the laboratory environment.
CLO-4 Demonstrate involvement in the Project as a team or individually with respect to the
contribution.

Performance 5 Excellent 4 Good 3 Satisfactory 2-1 Needs Marks


Improvement
Realization of Fully understand & Close to fully Partially Unable to
Experiment able to illustrate understand & able understands & able understand & able
CLO-1 Postfix Evaluation to illustrate Postfix to illustrate Postfix to illustrate Postfix
Process Evaluation Process Evaluation Process Evaluation Process

Conducting Completely able to Close to Partially able to Unable to


Experiment successfully completely able to successfully successfully
CLO-2 design and compile successfully design and compile design and compile
Python programs design and compile Python programs Python programs
for Postfix Python programs for Postfix for Postfix
Evaluation Process for Postfix Evaluation Process Evaluation Process
Evaluation Process
Data Collection Completely Close to Partially Unable to
and Data Analysis understand & able completely understand & able understand & able
CLO-3 to demonstrate understand & able to demonstrate to demonstrate
syntax of Postfix to demonstrate syntax of Postfix syntax of Postfix
Evaluation Process syntax of Postfix Evaluation Process Evaluation Process
Evaluation Process

Individual/Team Completely able to Close to Partially able to Unable to execute


Work execute different completely able to execute different different programs
CLO-4 programs for execute different programs for for Postfix
Postfix Evaluation programs for Postfix Evaluation Evaluation Process
Process Postfix Evaluation Process
Process

Total

You might also like