某些代码循环出错

时间:2013-12-21 11:11:28

标签: java casting compiler-errors rpn

我设计了一个RPN算法来计算给计算器的总和的结果。我的RPN代码如下:

import java.util.ArrayList;

public class RPNCalculator {
    String operator;
    double number_1;
    double number_2;
    double result;
    public int length_change;

public void calculateResult(ArrayList<Object> outputQueueArray)
{
    int length = outputQueueArray.size();
    length_change = length;
    int i;
    int b;
    int a;
    for(b = 0; b < length; b++){
        for(i = 0; i < length_change; i++){
            if(outputQueueArray.get(i).equals("+") || outputQueueArray.get(i).equals("-") || outputQueueArray.get(i).equals("/") || outputQueueArray.get(i).equals("*")){
                a = i - 2;
                operator = (String) outputQueueArray.remove(i) ;
                number_1 = (double) outputQueueArray.remove(i - 1);
                number_2 = (double) outputQueueArray.remove(i - 2);
                outputQueueArray.add(a,useOperator(number_1, number_2, operator));
                length_change = outputQueueArray.size();
                System.out.println(outputQueueArray);
            }
        }
    }
}

public double useOperator(double number_1, double number_2, String operator)
{
    if(operator.equals("+")){
        return number_2 + number_1;
    }
    else if(operator.equals("-")){
        return number_2 - number_1;
    }
    else if(operator.equals("/")){
        return number_2 / number_1;
    }
    else if(operator.equals("*")){
        return number_2 * number_1;
    }
    else{
        return 0;
    }
}
}

如果我给代码做以下计算:

[3.0, 2.0, /, 8.0, 3.0, +, -, 2.0, /]

它提供以下输出:

[1.5, 8.0, 3.0, +, -, 2.0, /]
[1.5, 11.0, -, 2.0, /] java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Double

因此处理[1.5,11.0, - ,2.0,/]

时会发生错误

但是如果我最初给它做以下计算:

[1.5, 11.0, -, 2.0, /]

它给出了正确的答案:

[1.5, 11.0, -, 2.0, /]
[-9.5, 2.0, /]
[-4.75]

任何人都可以帮忙解决这个问题:)

P.S。抱歉,这个问题很长

1 个答案:

答案 0 :(得分:3)

您最好使用堆栈。

// returns result instead of modifying the input list
// input is still a list of Double s (literals) and String s (operators)
public double calculateResult(ArrayList<Object> input)
{
    // create new java.util.Stack
    // the new stack is empty
    Stack<Double> operands = new Stack<>();

    for (Object o : input) {
        if (o instanceof String) {
            // remove operands of the operation from the stack and "replace"
            // with the result of the operation
            double operand2 = operands.pop();
            double operand1 = operands.pop();
            operands.push(useOperator(operand2, operand1, o));
        } else {
            // push a "literal" (i.e. a Double from input) to operands
            operands.push((Double)o);
        }
    }
    if (operands.size() != 1)
        throw new IllegalArgumentException("Input not valid. Missing operator or empty input.");
    return operands.pop();
}

这样算法应该明显更快,因为从ArrayList L的位置i移除元素需要O(L.size() - i)时间。

的执行示例
input = new ArrayList<Object>(Arrays.asList(new Object[] {
new Double(3.0),
new Double(2.0),
"/",
new Double(8.0),
new Double(3.0),
"+",
"-",
new Double(2.0),
"/"}))

    循环之前
  • operands = []
  • 在使用o = 3.0operands = [3.0] 进行迭代后
  • 在使用o = 2.0operands = [3.0, 2.0]
  • 进行迭代后
  • o = "/"的迭代:
    • operands移除操作数:operands = []; operand1 = 3.0; operand2 = 2.0
    • (operand1 / operand2)的结果推送到堆栈:operands = [1.5]
  • 在使用o = 8.0operands = [1.5, 8.0] 进行迭代后
  • 在使用o = 3.0operands = [1.5, 8.0, 3.0]
  • 进行迭代后
  • o = "+"的迭代:
    • operands移除操作数:operands = [1.5]; operand1 = 8.0; operand2 = 3.0
    • (operand1 + operand2)的结果推送到堆栈:operands = [1.5, 11.0]
  • o = "-"的迭代:
    • operands移除操作数:operands = []; operand1 = 1.5; operand2 = 11.0
    • (operand1 - operand2)的结果推送到堆栈:operands = [-9.5]
  • 在使用o = 2.0operands = [-9.5, 2.0] 进行迭代后
  • o = "+"的迭代:
    • operands移除操作数:operands = []; operand1 = -9.5; operand2 = 2.0
    • (operand1 + operand2)的结果推送到堆栈:operands = [-7.5]