Ошибка переполнения стека Java: оценка Postfix
Я пытаюсь оценить значения каждого постфикса, которые были изменены из инфиксных уравнений. Мне нужно использовать 5 классов: Driver, InfixToPostfix, EvalPostfix, ObjectStack, ObjectInterface. Программа отлично работает при смене инфикса на постфикс, но я не могу оценить, потому что он говорит: "Переполнение стека". Я понятия не имею, почему это продолжает появляться. Я знаю, что где-то стек пуст, и я просил pop()
но я понятия не имею, где это происходит. Если бы кто-нибудь мог помочь мне выяснить, почему это происходит, я был бы очень благодарен.
Выход (консоль Eclipse):
infix: 8 + 4 * 2 - 6
Postfix: 8 4 2 * + 6 -
infix: 8 + 4 * 2 - 6
Postfix: 8 4 2 * + 6 -
Stack Underflow
Класс водителя:
//Driver class
import java.io.*;
import java.util.Scanner;
public class Driver {
public static void main(String[] args) throws IOException {
PrintWriter pw = new PrintWriter(new FileWriter("output.txt"));
Scanner fileScan = new Scanner(new File("infix.txt"));
InfixToPostfix infix = new InfixToPostfix(pw);
EvalPostfix getPostfix = new EvalPostfix(pw);
while(fileScan.hasNext()) {
String stringinfix = fileScan.nextLine();
infix.convertToPostfix(stringinfix);
String Postfix = infix.convertToPostfix(stringinfix);
getPostfix.Evaluate(Postfix);
}
fileScan.close();
pw.close();
}
}
Класс EvalPostfix:
//EvalPostfix class
import java.io.*;
public class EvalPostfix {
ObjectStack operator = new ObjectStack();
ObjectStack operand = new ObjectStack();
private PrintWriter pw;
public EvalPostfix(PrintWriter pw) {
this.pw = pw;
}
public void Evaluate(String Postfix) {
int n1 = 0;
int n2 = 0;
for(int i = 0; i < Postfix.length(); i++) {
char ch = Postfix.charAt(i);
if(Character.isDigit(ch))
operand.push(Character.getNumericValue(ch));
else {
operator.push(ch);
n2 = (int) operand.pop();
n1 = (int) operand.pop();
char op = (Character)operator.pop();
switch (op) {
case '^':
operand.push((int)Math.pow(n1, n2));
break;
case '*':
operand.push(n1 * n2);
break;
case '/':
operand.push(n1 / n2);
break;
case '+':
operand.push(n1 + n2);
break;
case '-':
operand.push(n1 - n2);
break;
default:
System.out.println("Order invalid.");
}
}
}
//moved bracket
int result = (int) operand.pop();
System.out.println(result);
pw.println("Evaluation: " + result);
}
}
Класс ObjectStack:
//ObjectStack class
public class ObjectStack {
private Object[] item;
private int top;
public ObjectStack() {
item = new Object[1];
top = -1;
}
public boolean isEmpty() {
return top == -1;
}
public boolean isFull() {
return top == item.length-1;
}
public void clear() {
item = new Object[1];
top = -1;
}
public void push(Object o) {
if (isFull())
resize(2 * item.length);
item[++top] = o;
}
private void resize(int size) {
Object[] temp = new Object[size];
for (int i = 0; i <= top; i++)
temp[i] = item[i];
item = temp;
}
public Object pop() {
if (isEmpty()) {
System.out.println("Stack Underflow");
System.exit(1);
}
Object temp = item[top];
item[top--] = null;
if (top == item.length/4)
resize(item.length/2);
return temp;
}
public Object top() {
if (isEmpty()) {
System.out.println("Stack Underflow");
System.exit(1);
}
return item[top];
}
}