0

I have a program where the user inputs 6 doubles, and the program outputs every combination of operators that can go in-between the doubles as 1024 separate strings. Here are the first two results if the user inputed 14,17,200,1,5, and 118:

"14.0+17.0+200.0+1.0+5.0+118.0"

"14.0+17.0+200.0+1.0+5.0-118.0"

What I want to do is perform the arithmetic according to the order of operations. Each double is stored as a variable a through f and each operator in-between these variables is stored as a char a_b through e_f. So:

    double a, b, c, d, e, f;
    char a_b, b_c, c_d, d_e, e_f;

My first thought was to write the code like this:

    public double operateGroup() {
    value = 0;

    switch (a_b) {
    case '+':
        value += a + b;
        break;
    case '-':
        value += a - b;
        break;
    case '*':
        value += a * b;
        break;
    case '/':
        value += a / b;
        break;
    default:
        break;
    }

    switch (b_c) {
    case '+':
        value += c;
        break;
    case '-':
        value += -c;
        break;
    case '*':
        value *= c;
        break;
    case '/':
        value /= c;
        break;
    default:
        break;
    }

    switch (c_d) {
    case '+':
        value += d;
        break;
    case '-':
        value += -d;
        break;
    case '*':
        value *= d;
        break;
    case '/':
        value /= d;
        break;
    default:
        break;
    }

    switch (d_e) {
    case '+':
        value += e;
        break;
    case '-':
        value += -e;
        break;
    case '*':
        value *= e;
        break;
    case '/':
        value /= e;
        break;
    default:
        break;
    }

    switch (e_f) {
    case '+':
        value += f;
        break;
    case '-':
        value += -f;
        break;
    case '*':
        value *= f;
        break;
    case '/':
        value /= f;
        break;
    default:
        break;
    }

    return value;
}

But this doesn't work because it is the same as doing (a O b) O c) O d) O e) where O is any arbitrary operator. Any tips?

4

3 回答 3

2

由于没有括号,因此可以采用一种简单的方法:

  • 遍历列表一次以处理乘法和除法
  • 当 X 和 Y 之间的运算符为*or/时,替换XX*Yor X/Y,并删除 Y;也删除运营商
  • 现在再次浏览列表,这次按顺序处理加法和减法。

要实现这种方法,定义两个列表 - N Doubles 和N-1运算符列表,并按如下方式实现计算:

ArrayList<Double> vals = ...
ArrayList<Integer> ops = ... // 1=+, 2=-, 3=*, 4=/
for (int i = 0 ; i < ops.Count ; i++) {
    int op = ops.get(i);
    if (op == 3 || op == 4) {
        if (op == 3) {
            vals.set(i, vals.get(i) * vals.get(i+1));
        } else {
            vals.set(i, vals.get(i) / vals.get(i+1));
        }
        ops.remove(i);
        vals.remove(i+1);
        i--;
    }
}
double res = vals.get(0);
for (int i = 0 ; i != ops.Count ; i++) {
    if (op == 1) {
        res += vals.get(i);
    } else {
        res -= vals.get(i);
    }
}
于 2013-05-19T02:13:48.637 回答
1

如果您需要运算符和操作数的信息,您应该构建一个Parse Tree之前已经问过这个问题)。

如果您只对结果感兴趣,您可以String直接评估:

import javax.script.ScriptEngineManager;
import javax.script.ScriptEngine;
public class Eval {
    public static void main(String[] args) throws Exception {
        ScriptEngineManager s = new ScriptEngineManager();
        ScriptEngine engine = s.getEngineByName("JavaScript");
        String exp = "14.0+17.0+200.0+1.0+5.0-118.0";
        System.out.println(engine.eval(exp));
    }    
}

输出:

119.0
于 2013-05-19T02:14:33.483 回答
0

我会说你应该把它解析成一棵树,然后走这棵树来评估。数字是叶节点,运算符是父节点。

于 2013-05-19T02:12:56.703 回答