Using the algorithm convertToPostfix given in photo below, create a class in jav
ID: 3678187 • Letter: U
Question
Using the algorithm convertToPostfix given in photo below, create a class in java that will convert each of thefollowing infix expressions to postfix expressions for the test cases a-d:
a. a * b / (c -d)
b. (a -b * c) / (d * e * f + g)
c. a / b * (c + (d -e))
d. (a ^ b * c -d) ^ e + f ^ g ^ h
, ollowing algorithm encompasses the previous observations s in our expression are single-letter variables. process. For simplicity, all operand Algorithm convertToPostfix(infix) // Converts an infix expression to an equivalent postfix expression operatorStack - a new empty stack postfix = a new empty string while Cinfix 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 ' casecase while (loperatorStack.isEmptyO and O) precedence ofnextCharacter precedence of operatorStack.peek Append operatorStack.peek O to postfix operatorStack.popC operatorStack.push (nextCharacter) break case' operatorStack.push(nextCharacter) break case)' :11 Stack is not empty if infix expression is valid topoperator = operatorStack.pop() while (topOperatorC)Explanation / Answer
JAVA PROGRAM FOR INFIX TO POSTFIX CONVERSION: import java.io.*; class stack { char stack1[]=new char[20]; int top; void push(char ch) { top++; stack1[top]=ch; } char pop() { char ch; ch=stack1[top]; top--; return ch; } int pre(char ch) { switch(ch) { case '-':return 1; case '+':return 1; case '*':return 2; case '/':return 2; } return 0; } boolean operator(char ch) { if(ch=='/'||ch=='*'||ch=='+'||ch=='-') return true; else return false; } boolean isAlpha(char ch) { if(ch>='a'&&ch='0'&&ch=='9') return true; else return false; } void postfix(String str) { char output[]=new char[str.length()]; char ch; int p=0,i; for(i=0;ipre(stack1[top]))||stack1[top]=='(') { push(ch); } } else if(pre(ch)Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.