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

In infix notation, the operator (+, - , *, or /) appears between the operands. F

ID: 670374 • Letter: I

Question

In infix notation, the operator (+, - , *, or /) appears between the operands. For example: • 2 + 2 • 3 / 5 An alternative is postfix notation, where the operator appears at the end. The expression above would be written as: • 2 2 + • 3 5 / The advantage of postfix notation is that complicated expressions do not require parentheses. For example, using infix notation, we need parentheses for the expression: (5 – 2) * 4. The value of this expression is 12. If we leave out the parentheses, this becomes: 5 – 2 * 4, which is -3. Using postfix notation, the expression (5 – 2) * 4 becomes: 5 2 – 4 * and the second expression 5 – 2 * 4 becomes: 5 2 4 * - No parentheses, no ambiguity! Write a postfix calculator. • Implement the ideal data structure for solving this problem. • Using this implementation, finish PostfixCalculator.java. Your calculator should accept expressions one operator or number at a time in postfix order. • Any time an operator is entered, the current value of the equation should be displayed to the user. The program continues with the current equation unless the user enters clear, which clears the current equation and starts a new one. • Your calculator should support operators +, *, - , and /. • Use doubles, not integers. Convert input into a double with the method Double.parseDouble()

Explanation / Answer


import java.io.*;

class Stack
{
private int maxSize;
private int[] stackArray;
private int top;

public Stack(int size)   
{
maxSize = size;
stackArray = new int[maxSize];
top = -1;
}

public void push(int j)   
{ stackArray[++top] = j; }

public int pop()
{ return stackArray[top--]; }

public int peek()
{ return stackArray[top]; }

public boolean isEmpty()   
{ return (top == -1); }

public boolean isFull()
{ return (top == maxSize-1); }

public int size()
{ return top+1; }

public int peekN(int n)   
{ return stackArray[n]; }

public void displayStack(String s)
{
System.out.print(s);
System.out.print("Stack (bottom-->top): ");
for(int j=0; j<size(); j++)
{
System.out.print( peekN(j) );
System.out.print(' ');
}
System.out.println("");
}

}

class PostFixCalculator
{
private Stack theStack;
private String input;

public PostFixCalculator(String s)
{ input = s; }

public int doParse()
{
theStack = new Stack(20);
char ch;
int j;
int num1, num2, interAns;

for(j=0; j<input.length(); j++)   
{
ch = input.charAt(j);
theStack.displayStack(""+ch+" ");
if(ch >= '0' && ch <= '9')
theStack.push( (int)(ch-'0') );
else   
{
num2 = theStack.pop();
num1 = theStack.pop();
switch(ch)
{
case '+':
interAns = num1 + num2;
break;
case '-':
interAns = num1 - num2;
break;
case '*':
interAns = num1 * num2;
break;
case '/':
interAns = num1 / num2;
break;
default:
interAns = 0;
} // end switch
theStack.push(interAns);   
}
}
interAns = theStack.pop();
return interAns;
}
}