我快要疯了..我很接近让此代码以我想要的方式工作,我只是想不通。我正在尝试解决ex的后缀方程。 3 2 +,等于5。例如
在主方法中,“ 3 2 +”可以正常工作,但是一旦我输入第3个数字,例如“ 3 2 + 2 *”(等于10),我就会得到一个arrayoutofboundserror,该错误与number2 = s.pop()有关请参见下面的代码。任何帮助是极大的赞赏。

这是postfix method:

   public int PostfixEvaluate(String e){
        int number1;
        int number2;
        int result=0;

        String[] tokens = e.split(" ");

            for(int j = 0; j < tokens.length; j++){
                String token = tokens[j];
            if (!"+".equals(token) && !"*".equals(token) && !"-".equals(token) && !"/".equals(token)) {
                s.push(Integer.parseInt(token));

        }   else {
                String Operator = tokens[j];
                number1 = s.pop();
                number2 = s.pop();
                if (Operator.equals("/")){
                    result = number1 / number2;}
                else if(Operator.equals("*")){
                    result = number1 * number2;}
                else if(Operator.equals("+")){
                    result = number1 + number2;}
                else if(Operator.equals("-")){
                    result = number1 - number2;}
                else System.out.println("Illeagal symbol");
            }
                s.push(result);

                    s.pop();
                }


        //s.pop();
        System.out.println("Postfix Evauation = " + result);

            return result;
}

public static void main(String[] args) {
    Stacked st = new Stacked(100);
    //String y = new String("((z * j)/(b * 8) ^2");
    String x = new String("2 2 2 * +");
    TestingClass clas = new TestingClass(st);

    //clas.test(y);
     clas.PostfixEvaluate(x);

    }


}

最佳答案

/**
 * Evaluate postfix arithmetic expression
 *
 * @example "1 12 23 + * 4 5 / -" => 34.2
 * @author  Yong Su
 */
import java.util.Stack;

class PostfixEvaluation {

    public static void main(String[] args) {
        String postfix = "1 12 23 + * 4 5 / -";
        Double value = evaluate(postfix);
        System.out.println(value);
    }

    /**
     * Evaluate postfix expression
     *
     * @param postfix The postfix expression
     */
    public static Double evaluate(String postfix) {
        // Use a stack to track all the numbers and temporary results
        Stack<Double> s = new Stack<Double>();

        // Convert expression to char array
        char[] chars = postfix.toCharArray();

        // Cache the length of expression
        int N = chars.length;

        for (int i = 0; i < N; i++) {
            char ch = chars[i];

            if (isOperator(ch)) {
                // Operator, simply pop out two numbers from stack and perfom operation
                // Notice the order of operands
                switch (ch) {
                    case '+': s.push(s.pop() + s.pop());     break;
                    case '*': s.push(s.pop() * s.pop());     break;
                    case '-': s.push(-s.pop() + s.pop());    break;
                    case '/': s.push(1 / s.pop() * s.pop()); break;
                }
            } else if(Character.isDigit(ch)) {
                // Number, push to the stack
                s.push(0.0);
                while (Character.isDigit(chars[i]))
                    s.push(10.0 * s.pop() + (chars[i++] - '0'));
            }
        }

        // The final result should be located in the bottom of stack
        // Otherwise return 0.0
        if (!s.isEmpty())
            return s.pop();
        else
            return 0.0;
    }

    /**
     * Check if the character is an operator
     */
    private static boolean isOperator(char ch) {
        return ch == '*' || ch == '/' || ch == '+' || ch == '-';
    }
}

关于java - 使用堆栈的Postfix评估。,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7450815/

10-11 04:21