Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Complete the following code, its supposed to take in an infix and convert it into its postfix equivalent and also solve it (If the infix

Complete the following code, its supposed to take in an infix and convert it into its postfix equivalent and also solve it (If the infix expression is 2 + 3 * 6 - 1, then its postfix version is 2 3 6 * + 1 - ). The program must not ask for any kind of input from the user. No file reading is required too. You must assume the following regarding the input infix expressions: They will contain only integers as operands. The only operator they can contain are these four: *, /, +, - They will not contain any parenthesis or any kind of brace. A number and a operator are separated by a whitespace. Example: 2844 + 33 * 49 - 9 / 67.

public class InfixToPostfixConverter {

private static class Stack {

private E[] stackArray;

private int stackTop = -1;

@SuppressWarnings("unchecked")

Stack(int capacity) {

stackArray = (E[])new Object[capacity];

}

public int size() {

return (stackTop + 1);

}

public E top() {

if (isEmpty())

return null;

return stackArray[stackTop];

}

public boolean isEmpty() {

return (stackTop == -1);

}

public void push(E item) {

if (size() == stackArray.length)

throw new IllegalStateException("Stack is full");

stackArray[++stackTop] = item;

}

public E pop() {

if (isEmpty())

return null;

E answer = stackArray[stackTop];

stackArray[stackTop] = null;

stackTop--;

return answer;

}

public String toString() {

StringBuilder sb = new StringBuilder("[ ");

for (int pos = stackTop; pos >= 0; pos--) {

sb.append(stackArray[pos]);

if (pos > 0)

sb.append(" , ");

}

sb.append(" ]");

return sb.toString();

}

}

public static String infix2Postfix(String infixExpression) {

StringBuilder postfixExpression = new StringBuilder();

Stack operatorStack = new Stack<>(infixExpression.length());

operatorStack.push('*');

operatorStack.push('+');

operatorStack.push('/');

System.out.println(operatorStack);

/*System.out.println(operatorStack.pop());

System.out.println(operatorStack.pop());

System.out.println(operatorStack.pop());

System.out.println(operatorStack.pop());*/

// Implement the algorithm here

return postfixExpression.toString();

}

public static double postfixEvaluator(String postfixExpression) {

double expressionValue = 0;

Stack operandStack = new Stack<>(postfixExpression.length());

/*operandStack.push(10.0);

operandStack.push(20.0);

operandStack.push(30.0);

System.out.println(operandStack.pop());

System.out.println(operandStack.pop());

System.out.println(operandStack.pop());

System.out.println(operandStack.pop());*/

// Implement the algorithm here

return expressionValue;

}

public static void main(String[] args) {

System.out.println(InfixToPostfixConverter.infix2Postfix("29 + 378 / 998"));

//System.out.println(InfixToPostfixConverter.postfixEvaluator("2 3 +"));

//String postExpr = InfixToPostfixConverter.infix2Postfix("62 - 378 + 0 + 99");

//System.out.println(postExpr);

//System.out.println(InfixToPostfixConverter.postfixEvaluator(postExpr));

}

}

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 And Python Programming MySQL MongoDB OOP And Tkinter

Authors: R. PANNEERSELVAM

1st Edition

9357011331, 978-9357011334

More Books

Students also viewed these Databases questions

Question

What is the competition?

Answered: 1 week ago

Question

What is the relative priority among the viable goals?

Answered: 1 week ago