使用LeftParen的不兼容类型

时间:2016-02-22 23:13:45

标签: java

尝试通过堆栈并识别LeftParen和RightParen,但是获取错误不兼容的类型令牌无法转换为leftparen?

public Double evaluate() {
    Stack<Operator> operatorStack = new Stack<>();
    Stack<Operand> operandStack = new Stack<>();
    while (!getTokenQueue().isEmpty()){
        Token token = getTokenQueue().dequeue();
        if (token instanceof Operand) {
            operandStack.push((Operand)token);
        } else if (token instanceof LeftParen) {
            operatorStack.push((LeftParen)token);
        } else if (token instanceof RightParen) {
            while (!(operatorStack.peek() instanceof LeftParen)) {
                topEval(operatorStack, operandStack);
            }
            operatorStack.pop();  
        } else {
            Operator operator = (Operator)token;
            while (keepEvaluating(operatorStack, operator)) {
                topEval (operatorStack, operandStack);
            }
            operatorStack.push(operator);
        }
    }
    while (!operatorStack.isEmpty()) {
        topEval(operatorStack, operandStack);
    }
    return operandStack.pop().getValue();

}

0 个答案:

没有答案