You are on page 1of 5

COLLEGE OF ENGINEERING & TECHNOLOGY

UNIVERSITY OF SARGODHA

CE 416: Data Structure and Algorithms (Lab)

Lab 7 Manual
Infix to Postfix Expression Conversion

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
conversion of an infix expression into postfix expression.

ABOUT THE EXPERIMENT:


Infix expression:
The expression of the form a op b. When an operator is in-between every pair of operands.
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.

ALGORITHM:
1. Scan the infix expression from left to right.
2. If the scanned character is an operand, output it.
3. Else,
3.1 If the precedence of the scanned operator is greater than the precedence of the operator in the
stack(or the stack is empty or the stack contains a ‘(‘ ), push it.
3.2 Else, Pop all the operators from the stack which are greater than or equal to in precedence than
that of the scanned operator. After doing that Push the scanned operator to the stack. (If you encounter
parenthesis while popping then stop there and push the scanned operator in the stack.)
4. If the scanned character is an ‘(‘, push it to the stack.
5. If the scanned character is an ‘)’, pop the stack and and output it until a ‘(‘ is encountered, and discard
both the parenthesis.
6. Repeat steps 2-6 until infix expression is scanned.
7. Print the output
8. Pop and output from the stack until it is not empty.

Python Program Code:


def priority(alpha):
if alpha in ['+', '-']:
return 1
elif alpha in ['*', '/']:
return 2
elif alpha == '^':
return 3
else:
return 0

def convert(infix):
i=0
postfix = ""
stack = []

while i < len(infix):


if infix[i].isalpha():
postfix += infix[i]
i += 1
elif infix[i] == '(':
stack.append(infix[i])
i += 1
elif infix[i] == ')':
while stack and stack[-1] != '(':
postfix += stack.pop()
stack.pop() # Pop the '('
i += 1
else:
while stack and priority(infix[i]) <= priority(stack[-1]):
postfix += stack.pop()
stack.append(infix[i])
i += 1

while stack:
postfix += stack.pop()

print("Postfix is:", postfix)


return postfix
if __name__ == "__main__":
infix = "((A*D/(E*G))-C*B)"
postfix = convert(infix)

OUTPUT:

Postfix is: AD*EG*/CB*-

Process finished with exit code 0


Assessment Rubric for Lab 7
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 Infix to Postfix to illustrate Infix to to illustrate Infix to to illustrate Infix to
Conversion Postfix Conversion Postfix Conversion Postfix Conversion

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 Infix to Postfix Python programs for Infix to Postfix for Infix to Postfix
Conversion for Infix to Postfix Conversion Conversion
Conversion
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 Infix to to demonstrate syntax of Infix to syntax of Infix to
Postfix Conversion syntax of Infix to Postfix Conversion Postfix Conversion
Postfix Conversion

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 Infix execute different programs for Infix for Infix to Postfix
to Postfix programs for Infix to Postfix Conversion
Conversion to Postfix Conversion
Conversion

Total

You might also like