我设计了一个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。对不起,这个长期的问题

最佳答案

您最好使用堆栈。

// 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.0迭代后的
  • :operands = [3.0]
  • o = 2.0迭代后的
  • :operands = [3.0, 2.0]
  • o = "/"
  • 迭代:
  • operands中删除操作数:operands = []; operand1 = 3.0; operand2 = 2.0
  • (operand1 / operand2)的结果压入堆栈:operands = [1.5]

  • o = 8.0迭代后的
  • :operands = [1.5, 8.0]
  • o = 3.0迭代后的
  • :operands = [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.0迭代后的
  • :operands = [-9.5, 2.0]
  • o = "+"
  • 迭代:
  • operands中删除操作数:operands = []; operand1 = -9.5; operand2 = 2.0
  • (operand1 + operand2)的结果压入堆栈:operands = [-7.5]

  • 10-08 18:16