简体   繁体   中英

Shunting Yard algorithm issue

So here's my Code:

public double evaluate(){

        Stack numbers = new Stack();
        Stack operators = new Stack();


        String[] divert = {};
        String problem = "2 + 2 + 3";

        divert = problem.split(" ");

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


            if(divert[i].equals("*") || divert[i].equals("/")|| divert[i].equals("+") || divert[i].equals("-")){
                if(operators.peek().equals("*") || operators.peek().equals("/")){
                    int a = Integer.parseInt((String)numbers.pop());
                    int b = Integer.parseInt((String)numbers.pop());
                    String operand = (String)operators.pop();
                    numbers.push(doMath(b, operand, a));

                }
                else if ( divert[i].equals("+") || divert[i].equals("-")){
                    operators.push(divert[i]);
                }

            } else {
                numbers.push(divert[i]);
            }
        }

        while(!operators.empty()){
            int a = Integer.parseInt((String)numbers.pop());
            int b = Integer.parseInt((String)numbers.pop());
            String operand = (String)operators.pop();
            numbers.push(doMath(a, operand, b));
        }
        double endNumber = (double)numbers.pop();
        return endNumber;
    }

I keep getting weird errors, one telling me that the if(operators.peek().equals... bit in the nested if statement returns an EmptyStackException. I get another error while trying to cast the popped number (endNumber) off to return it. I get an issue with casting that as a double.

If someone would look at this and tell me what is the problems and any possible way to resolve the issue, that would be great because I really don't understand why it's giving me these errors.

I know that removing the divert[i].equals("+")/("-") removes the issue for the first error, but that isn't very conductive to what I'm doing.

For the problem with doubles use the generics capability of Stack

Stack<Double> numbers = new Stack<Double>();

this will ensure only Doubles are stored on the stack. The auto unboxing (converts doubles to Doubles and visa-versa) feature means you should be able to do

double x = 5.0;
numbers.push(x);
double y = numbers.pop();

For good form also use

Stack<String> operator;

For testing the operator stack use

if( !operators.empty() && (operators.peek().equals("*") || operators.peek().equals("/")) )

ie test if the stacks is not empty before peeking.

Also check the order you popping numbers off the stack, in the code at the end. I've a feeling you will have problems with "5 - 3".

Also you always want to push the current operator. It looks like a "*" or "/" will never get pushed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM