0

我设计了一个 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]

有人可以帮忙吗:)

ps对不起这个长问题

4

1 回答 1

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 = "/"
    • 从中删除操作数operandsoperands = []; 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 = "+"
    • 从中删除操作数operandsoperands = [1.5]; operand1 = 8.0; operand2 = 3.0
    • 将结果推(operand1 + operand2)送到堆栈:operands = [1.5, 11.0]
  • 迭代o = "-"
    • 从中删除操作数operandsoperands = []; operand1 = 1.5; operand2 = 11.0
    • 将结果推(operand1 - operand2)送到堆栈:operands = [-9.5]
  • 迭代后o = 2.0operands = [-9.5, 2.0]
  • 迭代o = "+"
    • 从中删除操作数operandsoperands = []; operand1 = -9.5; operand2 = 2.0
    • 将结果推(operand1 + operand2)送到堆栈:operands = [-7.5]
于 2013-12-21T12:03:49.743 回答