Lab Sheet 3 - Infix to Postfix Conversion
Lab Sheet 3 - Infix to Postfix Conversion
Lab Objective:
LO1: Interpret the rules of operator precedence and associativity in the context of infix
expressions.
Theory:
Infix Expression:
In an infix expression, operators are placed between operands. For example, A + B * C is an
infix expression where the operator + is between operands A and B, and * is between B and
C.
Postfix Expression:
In postfix expressions, operators are placed after their operands. For example, the infix
expression A + B * C would be converted to ABC*+.
Algorithm:
1. Operands (letters or digits): Directly add them to the output.
2. Operators:
- If the stack is empty or contains only parentheses, push the operator onto the stack.
- If the precedence of the current operator is higher than the operator at the top of the
stack, push it onto the stack.
- Otherwise, pop operators from the stack and add them to the output until an operator of
lower precedence is found or the stack is empty.
3. Parentheses:
- If you encounter an opening parenthesis `(`, push it onto the stack.
- When a closing parenthesis `)` is found, pop and add operators to the output until the
corresponding opening parenthesis is encountered.
Procedure:
Code Implementation:
Stack Class
package infixtopostfix;
// topElement() returns the top element of the stack without removing it.
public char topElement() {
if (!isEmpty()) {
return stack[top];
}
return '\0'; // null character to indicate empty stack
}
// pop() removes and returns the top element from the stack.
public char pop() {
if (!isEmpty()) {
return stack[top--];
}
return '\0'; // null character to indicate empty stack
}
}
InfixToPostfix Class
package infixtopostfix;
import java.util.Scanner;
// While loop iterates through the infix expression one character at a time.
while (index < infix.length()) {
cs = infix.charAt(index);
// After reading the entire expression, pop remaining operators in the stack.
while (!stack.isEmpty()) {
System.out.print(stack.pop());
}
}
// priority() returns precedence levels for operators.
public static int priority(char op) {
switch (op) {
case '(':
return 0;
case '+':
case '-':
return 1;
case '*':
case '/':
case '%':
return 2;
case '^':
return 3;
}
return -1; // For unknown operators
}
}
Sample Input/Output:
Test Case 1:
Input:
Enter the Infix Expression:
A+B*C
Output:
ABC*+
Test Case 2:
Input:
Enter the Infix Expression:
(A+B)*(C+D)
Output:
AB+CD+*
Post-Lab Questions:
1. What is the main advantage of using postfix notation over infix notation?
2. Explain how the stack is used to handle operator precedence in this algorithm.
3. What happens when parentheses are used in the infix expression?
4. Write the postfix expression for the infix expression A*B-(C+D)/E.
Conclusion:
In this lab, you have implemented a program to convert infix expressions to postfix using a
stack data structure. You learned how to handle operator precedence, parentheses, and the
order of evaluation in mathematical expressions.
Test Cases:
Test Case 1:
Input:
Enter the Infix Expression:
((A+B)*(C-D)/(E+F))^(G+H)
Output:
AB+CD-*EF+/GH+^
Test Case 2:
Input:
Enter the Infix Expression:
A+(B*C-(D/E^F)*G)*H
Output:
ABC*DEF^/G*-H*+
Test Case 3:
Input:
Enter the Infix Expression:
A*B+C/D-E^F^G
Output:
AB*CD/+EFG^^-
Test Case 4:
Input:
Enter the Infix Expression:
((A+B)*C-(D-E))^(F+G/H)
Output:
AB+C*DE--FGH/+^
Test Case 5:
Input:
Enter the Infix Expression:
A%B*C^D+(E-F/G)
Output:
AB%D*CD^EFG/-+