0

我正在尝试评估从中缀方程更改的每个后缀值。我必须使用 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 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];
    }
}
4

0 回答 0