Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

// compile and run this infix class using the Stack class, Tokenizer Class, and Queue Class. Debugged the three issues that I found when I

// compile and run this infix class using the Stack class, Tokenizer Class, and Queue Class. Debugged the three issues that I found when I run the infix class and fixed please import java.io.*; import MyStackQueue.*; class infix { static Queue infixToPostfix(String s) { Tokenizer token = new Tokenizer(s);//step3 Queue queue = new Queue(); Token tkn = token.nextToken();//step5 Stack theStack = new Stack();//step 1 theStack.push(new Operator('#'));//step2 while(tkn != null) {//step4 System.out.println(tkn); if(tkn instanceof Operand) {//step8 queue.enqueue(tkn); } else { Operator Opr = (Operator)tkn; if(Opr.operator == '(') {//step9 theStack.push(Opr); } else if(Opr.operator == ')') { while (((Operator)theStack.top()).operator != '(') { queue.enqueue(theStack.pop());//step6 } theStack.pop(); } else { while (((Operator)theStack.top()).precedence() >= (Opr.precedence())) { queue.enqueue(theStack.pop()); } theStack.push(Opr); } } tkn = token.nextToken(); } while(((Operator)theStack.top()).operator != '#') {//step11 queue.enqueue(theStack.pop()); } theStack.pop(); return queue; } static int evaluePostfix(Queue Post) { Stack theStack = new Stack(); int result= 0; while(!Post.isEmpty()) { Token tkn = (Token)Post.dequeue(); if(tkn instanceof Operand) { theStack.push(tkn); } else if(tkn instanceof Operator) { Operator Opr = (Operator)tkn; int opnd2= (int)((Operand)theStack.pop()).operand; int opnd1= (int)((Operand)theStack.pop()).operand; switch(Opr.operator) {//step 10 to perform an operation case('+'): result = opnd1 + opnd2; break; case('-'): result = opnd1 - opnd2; break; case('*'): result = opnd1*opnd2; break; case('/'): result = opnd1/opnd2; break; } theStack.push(new Operand(result)); } } return result; } public static void main(String[] args) throws IOException { Queue Post; while(true) { System.out.print("Enter infix: "); System.out.flush(); InputStreamReader isr = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(isr); String s = br.readLine(); if ( s.equals("") ) break; Post = infixToPostfix(s); System.out.println("Postfix is " + Post.toString() + ' '); int result = evaluePostfix(Post); System.out.println("Result is " + result + ' '); } } } ------------------------------------------------------------------------------------------------------------------------------------------

class Tokenizer { private char [] Buf; private int cur; Tokenizer(String infixExpression) { Buf = infixExpression.toCharArray(); cur = 0; } boolean moreTokens() { while (cur< Buf.length && Buf[cur]== 32){//. cur++; if (Buf[cur]!=32) return true; //Skip blanks. } return cur=Buf.length)return null; } //3. If the next character is a digit, keep reading until a non-digit is read. //Convert the string of digits into an integer. int result = 0; Token token = null; while (cur<=Buf.length && Buf[cur]>=48 && Buf[cur]>=57){ int start = cur; int len = Buf.length; String Digits = new String(Buf, start, len); int num = Integer.valueOf(Digits).intValue(); if (result == 0) result+=num; else { result *=10; result +=num; } cur++; if (cur<=Buf.length && Buf[cur]>=48 && Buf[cur]>=57){ Operand op=new Operand(result); token = (Token)op; return token; } } Operator o=new Operator(Buf[cur]); token = (Token)o; return token; } } ------------------------------------------------------------------------------------------------------------------------------------------

package MyStackQueue; public class Stack { private int topIndx = -1; int capacity = 100; private Object S[]; public Stack() { S = new Object[capacity]; } public boolean isEmpty() { return topIndx<0; } public boolean isFull() { return topIndx==capacity-1; } public void push(Object Element) { S[++topIndx] = Element; } public Object pop() { Object Element; Element = S[topIndx]; S[topIndx--] = null; return Element; } public Object top() { return S[topIndx]; } } -------------------------------------------------------------------------------------------------------

package MyStackQueue; public class Queue { private Node Rear = null, Front = null; private int size = 0; public boolean isEmpty() { return Front==null; } public void enqueue(Object Element) { Node Tmp = new Node(); Tmp.Data=Element; if (Rear==null) Rear = Front = Tmp; else { Rear.Next = Tmp; Rear = Tmp;} size++; } public Object dequeue() { Node Tmp = Front; Front = Front.Next; size--; if (Front==null) Rear = null; return Tmp.Data; } public String toString() { Node cur = Front; String Str = " "; while (cur!= null) { Str += cur.Data. toString() + " "; cur = cur.Next; } return Str; } class Node { Object Data; Node Next; } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Databases Illuminated

Authors: Catherine M. Ricardo, Susan D. Urban, Karen C. Davis

4th Edition

1284231585, 978-1284231588

More Books

Students also viewed these Databases questions