Professional Documents
Culture Documents
Answer:
General Guidance
The answer provided below has been developed in a clear step by step
manner.
Step: 1
2. b. Answer:
Java Code:
import java.util.*;
public class Main
{
// The following function determines whether a given
character is an operator or not.
// 1st operand
String op1 = operands.peek();
operands.pop();
// 2nd operand
String op2 = operands.peek();
operands.pop();
// operator
char op = operators.peek();
operators.pop();
char op = operators.peek();
operators.pop();
operators.push(infix.charAt(i));
}
}
char op = operators.peek();
operators.pop();
Output:
∗+AB/CD
Explanation:
Precedence order
Operators Symbols
Parenthesis { }, ( ), [ ]
Exponential notation ^
Associativity order
Operators Associativity
^ Right to Left
*, / Left to Right
+, - Left to Right
Input : (A+B)∗(C/D)
Output : * + A B / C D
4. Push the character into the operator stack if the current character is "(."
5. Check to see if the opening bracket is at the top of the operator stack if
the current character is ')'. If not, remove the operator from the operator
stack, followed by the string operator, operand 2 and operand 1, and then
remove the two operands from the operand stack. Up until the top of the
operator stack is an opening bracket, keep popping from both stacks and
pushing result into operand stack.
6. At the very top of the operand stack is the last prefix expression.
Step: 2
i.e (A+B)∗(C/D) will become )C/D(*)A+B(. While reversing each ‘(‘ will
become ‘)’ and each ‘)’ becomes ‘(‘.
Answer: