pgm3 - Handout
pgm3 - Handout
Ex : 3
Title: Design, Develop and Implement a Program in C for converting an Infix Expression to
Postfix Expression and Evaluatethe converted postfix expression.
Problem Description: The problem involves implementing a function that convert the Infix
Expression to Postfix Expression anda function that evaluates the converted expression and
prints the result. Program should support for both parenthesized and free parenthesized
expressions with the operators: +, -, *, /, % (Remainder), ^ (Power) and alphanumeric
operands.
Method: In this program, Infix expression is given as an input andthe function for converting
infix to postfix expression. After obtaining the postfix expression for each alphanumeric
operand’s values are given at the runtime and display the resultant value.
Choose the best data structure for these operations.
Explanation
Define the precedence levels for operators like +, -, *, /, %, and ^. Higher precedence
operators should be placed above lower precedence ones.
We use a stack to convert the infix expression (which may have parentheses) into a
postfix expression.
Use a stack to evaluate the postfix expression. Each operand is pushed onto the stack,
and operators pop the required number of operands to perform the operation, pushing
the result back onto the stack.
4. Handle Parentheses:
2024-25 Page 1
Data Structures and Applications Lab (CS205) Lab Manual
Parentheses should control the precedence of operators and should be handled properly
during both conversion and evaluation.
Algorithm
Step 1: Initialize:
2024-25 Page 2
Data Structures and Applications Lab (CS205) Lab Manual
o If operator:
Pop top two values from stack, perform operation, push
result back.
Pop and display final result.
Step 5: Stop
2024-25 Page 3