Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a Java program using stack implementation with array implementation to con

ID: 3586400 • Letter: W

Question

Write a Java program using stack implementation with array implementation to convert infix strings to postfix strings. Your program must be able to handle spaces and tabs. You can assume all variables are single letter variables and all digits are single digits. You must name your program InfixPostfix.java and stack class name ArrayStack. Please also include BalanceChecker we covered in class. Please use stack interface we used in the class Bonus points (5 bonus points): Your program can detect the following errors Extra bonus points (5 extra bonus points) Extra extra bonus points (10 extra extra bonus points): Missing operators (for example, (a +b)(c d)) Your program can handle operator (exponentiation) Your program can actually evaluate an expression and returns a correct result (for example, (3 + 4)* (7 - 2) will return 35)

Explanation / Answer

Here is a basic implementation using stack template.

boolean isOperator(char c)

{

return c == '+' || c == '-' || c == '*' || c == '/' || c == '^'

|| c == '(' || c == ')';

}

boolean isLowerPrecedence(char op1, char op2)

{

switch (op1)

{

case '+':

case '-':

return !(op2 == '+' || op2 == '-');

case '*':

case '/':

return op2 == '^' || op2 == '(';

case '^':

return op2 == '(';

case '(':

return true;

default:

return false;

}

}

public static String Postfix(String infix)

{

Stack<Character> stack = new Stack<Character>();

StringBuffer post = new StringBuffer(in.length());

char c;

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

{

c = in.charAt(i);

if (!isOperator(c))

{

post.append(c);

}

else

{

if (c == ')')

{

while (!stack.isEmpty() && stack.peek() != '(')

{

post.append(stack.pop());

}

if (!stack.isEmpty())

{

stack.pop();

}

}

else

{

if (!stack.isEmpty() && !isLowerPrecedence(c, stack.peek()))

{

stack.push(c);

}

else

{

while (!stack.isEmpty() && isLowerPrecedence(c, stack.peek()))

{

Character pop = stack.pop();

if (c != '(')

{

post.append(pop);

} else {

c = pop;

}

}

stack.push(c);

}

}

}

}

while (!stack.isEmpty()) {

post.append(stack.pop());

}

return post.toString();

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote