user1547052
user1547052

Reputation: 3

Convert arithmetic string into double in Java

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?

Upvotes: 0

Views: 2165

Answers (3)

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726569

Since there are no parentheses, a trivial approach will work:

  • Go through the list once to process multiplications and divisions
  • When an operator between X and Y is * or /, replace X by X*Y or X/Y, and remove Y; also remove the operator
  • Now go through the list again, this time processing additions and subtractions in sequence.

To implement this approach, define two lists - the list of N Doubles, and N-1 operators, and implement the calculation as follows:

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);
    }
}

Upvotes: 2

acdcjunior
acdcjunior

Reputation: 135762

If you need the operators' and operands' information, you should build a Parse Tree (this has been asked before).

If you are only interested in the result, you can evaluate the String directly:

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));
    }    
}

Output:

119.0

Upvotes: 1

duffymo
duffymo

Reputation: 308763

I would say you should parse it into a tree and then walk the tree to evaluate. Numbers are leaf nodes and operators are parents.

Upvotes: 0

Related Questions