You are on page 1of 8

Question:

Answer:

General Guidance
The answer provided below has been developed in a clear step by step
manner.
Step: 1
2. b. Answer:

Infix expression : (A+B) * (C/D)

Prefix expression : *+AB/CD

Prefix: If the operator appears in an expression before the operands, it is


referred to as a prefix expression of the simplest type (operator operand1
operand2).

An expression is written in a conventional or typical fashion in an infix


notation. The operators are placed between the operands in this notation.
A+B, A*B, A/B, and other examples of infix notation are available.

Java Code:

// Java program to convert infix to prefix expression.

import java.util.*;
public class Main
{
// The following function determines whether a given
character is an operator or not.

static boolean isOperator(char c)


{
return (!(c >= 'a' && c <= 'z') &&
!(c >= '0' && c <= '9') &&
!(c >= 'A' && c <= 'Z'));
}

// Function to determine the precedence of an operator.

static int getPriority(char C)


{
if (C == '-' || C == '+')
return 1;
else if (C == '*' || C == '/')
return 2;
else if (C == '^')
return 3;
return 0;
}

// An infix expression to prefix expression converter


function.

static String infixToPrefix(String infix)


{
// Operators stack.
Stack<Character> operators = new Stack<Character>();

// for operands, a stack.


Stack<String> operands = new Stack<String>();

for (int i = 0; i < infix.length(); i++)


{

// Push into the operators stack if the current character is


an opening bracket.
if (infix.charAt(i) == '(')
{
operators.push(infix.charAt(i));
}

// When a closing bracket appears, pop from both stacks and


push the result into the operands stack until a matching
opening bracket cannot be found.
else if (infix.charAt(i) == ')')
{
while (!operators.empty() &&
operators.peek() != '(')
{

// 1st operand
String op1 = operands.peek();
operands.pop();

// 2nd operand
String op2 = operands.peek();
operands.pop();

// operator
char op = operators.peek();
operators.pop();

// Operands and the operator are added in the form of


operator + operand1 + operand2.
String tmp = op + op2 + op1;
operands.push(tmp);
}

// Pull the opening bracket out of the stack.


operators.pop();
}

// Push the current character into the stack of operands if


it is an operand in the syntax.
else if (!isOperator(infix.charAt(i)))
{
operands.push(infix.charAt(i) + "");
}

// If the current character is an operator, it will be


pushed into the operands stack after the high priority
operators have been removed from the operands stack.
else
{
while (!operators.empty() &&
getPriority(infix.charAt(i)) <=
getPriority(operators.peek()))
{

String op1 = operands.peek();


operands.pop();

String op2 = operands.peek();


operands.pop();

char op = operators.peek();
operators.pop();

String tmp = op + op2 + op1;


operands.push(tmp);
}

operators.push(infix.charAt(i));
}
}

// Pop operators from the operators stack until it is empty,


then add the result of each pop operands stack operation.
while (!operators.empty())
{
String op1 = operands.peek();
operands.pop();

String op2 = operands.peek();


operands.pop();

char op = operators.peek();
operators.pop();

String tmp = op + op2 + op1;


operands.push(tmp);
}

// There is a final prefix expression in the operands stack


return operands.peek();
}
// Main code

public static void main(String args[])


{
String s = "(A+B)∗(C/D)";
System.out.println( infixToPrefix(s));
}
}

Output:

∗+AB/CD

Explanation:

Precedence order

Operators Symbols

Parenthesis { }, ( ), [ ]

Exponential notation ^

Multiplication and Division *, /

Addition and Subtraction +, -

Associativity order
Operators Associativity

^ Right to Left

*, / Left to Right

+, - Left to Right

Input : (A+B)∗(C/D)
Output : * + A B / C D

To convert an infix to prefix expression:

1. Check whether a character is an operand or an operator by traversing


the infix expression.

2. Put it in the operand stack if it is an operand.

3. If it is an operator, determine whether its priority is higher, lower, or equal


to that of the operator at the top of the stack. Operator will be pushed into
the operator stack if the priority is higher. Otherwise, remove the operator
from the stack, the string operator, and the first and second operands from
the operand stack. When the priority of the current operator is lower than or
equal to the operator at the top of the operator stack, keep popping from
both stacks and pushing the result into the operand stack.

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

 Step 1: Reverse the infix expression

i.e (A+B)∗(C/D) will become )C/D(*)A+B(. While reversing each ‘(‘ will
become ‘)’ and each ‘)’ becomes ‘(‘.

Do reverse once again for (, ) brackets. Hence (A+B)∗(C/D) will become


(C/D)*(A+B)

 Step 2: Obtain the “nearly” postfix expression of the modified


expression i.e CD/AB+*.

 Step 3: Reverse the postfix expression. Hence in our example


prefix is *+AB/CD.

Input : (A+B)∗(C/D) Output : * + A B / C D

Explanation:Please refer to solution in this step.

Answer:

 Reverse the equation or scan the equation from right to left.

 Apply the infix-postfix algorithm.

 The equation inside the brackets evaluate to DC/ and BA+


respectively giving us DC/BA+* in the end.

 Reversing this we get the *+AB/CD.

You might also like