Write a java code to implement the infix to postfix algorithm as described below
ID: 3743054 • Letter: W
Question
Write a java code to implement the infix to postfix algorithm as described below:
Algorithm convertToPostfix(infix) Converts an infix expression to an equivalent postfix expression operatorStack a new empty stack postfix-a new empty string while (infix has characters left to parse) nextCharacter next nonblank character of infix switch (nextCharacter case variable: Append nextCharacter to postfix break case 'A. operatorStack.push(nextCharacter) break case "+' : case "-' : case ' : case '/" : while CloperatorStack.isEmpty and precedence of nextCharacter precedence of operatorStack.peek O) Append operatorStack.peek O to postfix operatorStack.pop operatorStack.push(nextCharacter) break case': operatorStack.push (nextCharacter) break case' 1/ Stack is not empty if infix expression is valid topOperator operatorStack.popO while (topOperatorC Append topOperator to postfix topOperator operatorStack.popO break default: break // Ignore unexpected characters while CloperatorStack.isEmptyO) topOperator operatorStack.pop O Append topOperator to postfix return postfixExplanation / Answer
import java.io.IOException;
public class IntoPost {
//Code as per algorithms starts here..
public String convertTopostfix(String infix) {
Stack operatorStack;
String postfix = ""; //this will hold final postfix expression
int Size = infix.length();
operatorStack = new Stack(Size);
int i=0;
while (i < Size) {
char ch = infix.charAt(i);
switch (ch) {
case '^':
operatorStack.push(ch);
break;
case '+':
case '-':
case '*':
case '/':
while (!operatorStack.isEmpty()) {
char top = operatorStack.pop();
int prec1= precedenceLevel(ch);
if (top == '(') {
operatorStack.push(top);
break;
} else {
int prec2;
if (top == '+' || top == '-')
prec2 =0 ;
else
prec2 = 1;
if (prec2 < prec1) {
operatorStack.push(top);
break;
}
else postfix = postfix + top;
}
}
operatorStack.push(ch);
break;
case '(':
operatorStack.push(ch);
break;
case ')':
char topChar = operatorStack.pop();
while (!operatorStack.isEmpty() && topChar!='(') {
postfix = postfix + topChar;
topChar = operatorStack.pop();
}
break;
default:
postfix = postfix + ch;
break;
}
i++;
}
while (!operatorStack.isEmpty()) {
char topChar=operatorStack.pop();
postfix = postfix + topChar;
}
return postfix;
}
//Algorithm code ends here
//This function is used to give the precedence to operators..
int precedenceLevel(char op) {
switch (op) {
case '+':
case '-':
return 0;
case '*':
case '/':
return 1;
default:
throw new IllegalArgumentException("Operator unknown: " + op);
}
}
public static void main(String[] args) throws IOException {
String input = "7+2*9^5-(8+2/4)";
String postfix;
IntoPost theTrans = new IntoPost();
postfix = theTrans.convertTopostfix(input);
System.out.println("postfix is " + postfix + ' ');
}
class Stack {
private int maxSize;
private char[] stackArray;
private int top;
public Stack(int max) {
maxSize = max;
stackArray = new char[maxSize];
top = -1;
}
public void push(char j) {
stackArray[++top] = j;
}
public char pop() {
return stackArray[top--];
}
public char peek() {
return stackArray[top];
}
public boolean isEmpty() {
return (top == -1);
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.