0
 string input = "12+5-1";

Expected output would be: 16

The algorithm should identify the operators(+,-,*,/) and then do the operations one by one between the given numbers as inputs.

I initially created a string array

string[] strArrInput = input;

Somebody advised me however that I'm being inefficient that way because a string is already a character array. I need help with this logic.

EDIT:

By the way, I'm trying to make a scientific calculator that could do PEMDAS.

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
TJ Riceball
  • 240
  • 1
  • 2
  • 11
  • 1
    Ask the right questions and show us, what you have done so far! – bobbel Feb 05 '14 at 08:37
  • Is efficiency really the issue here? If not, you should possibly focus on the most elegant solution. – Magnilex Feb 05 '14 at 08:37
  • 1
    You could parse this `String` as a javascript script (pun intended) and use a javascript engine like rhino or `ScriptEngine` to execute it. Related: http://stackoverflow.com/q/7487908/1065197 – Luiggi Mendoza Feb 05 '14 at 08:40
  • 1
    Have a look on http://stackoverflow.com/questions/3422673/evaluating-a-math-expression-given-in-string-form – Sunil Kumar Sahoo Feb 05 '14 at 08:54

3 Answers3

0

You can use built in javascript engine for this..

    import javax.script.ScriptEngine;
    import javax.script.ScriptEngineManager;
    import javax.script.ScriptException;

    public class Test {
        public static void main(String[] args) throws ScriptException {
            ScriptEngineManager mgr = new ScriptEngineManager();
            ScriptEngine scriptEngine = mgr.getEngineByName("JavaScript");
            String expression = "12+5-1";
            System.out.println(scriptEngine.eval(expression));

        }
    }
Sinto
  • 920
  • 5
  • 10
0

I have made a simple code using stack, look at this.

import java.util.Scanner;
import java.util.Stack;

public class Evaluate {

public static void main(String[] args) {
    Scanner sc=new Scanner(System.in);
    String input=sc.next();
    Stack<Character> operator;
    Stack<Integer> operands;
    operands=new Stack<Integer>();
    operator=new Stack<Character>();
    int res=0,c;
    for(int i=0 ; i<input.length() ;i++){
        c =input.charAt(i);
        if(!isOperator(c)){
            res *= 10;
            res += c - '0';
        }else{
            operator.push((char)c);
            operands.push(res);
            res=0;
            }
    }
    operands.push(res);
    int a,b;
    char op;
    for(; operands.size()>1 ;){
        a=operands.pop();
        b=operands.pop();
        op=operator.pop();
        operands.push(result(a,b,op));
    }

    System.out.println(operands);

}
public static boolean isOperator(int c) {
    return c == '+' || c == '-' || c == '*' || c == '\\';
}
public static int result(int a,int b,char c){
    if(c=='+')
        return a+b;
    if(c=='-')
        return b-a;
    if(c=='*')
        return a*b;
    else
        return b/a;
}

}

You can use something like this, I have not implemented operand priorities but i think you can do that now.

CodeLover
  • 126
  • 1
  • 10
0

you can parse the String with the givn String functions. (indexOf,lastOf,split) But it is not that easy to write a algho who knows the mathematik. I have done it once (without "()") but havnt the code to give it to you. 8[ I tell you what i have done:

I searched first for '*,/' eg

input=120-20*2+30;

split the string at the givn index

left=120-20; 
right=2+30;

at the left split i searcht the last +,- and on the right the first split it again to the givn index (leftRest=120;rightRest=+30)

now i take the left found Value -20 parse it to BigDecimal (because that will give u better results for comma values) and parse the right found Value to BigDecimal 2 now u can calculate

calcValue=-40=-20 * 2;

write the value back to the string, if the calcValue is not minus u have to write a + before; somting like this will do:

if(calcValue>0){
 setPlus="+";
}else{
 setPlus="";
}

input=leftRest+setPlus+Value+rightRest;

that give u:

 120-40+30

now u repead until there are no *,/ then u start searching +,-

If u Divide somthing with bigDecimal u have to say how many comma values u like to have ... otherwise it will throw an exeption for thinks like 1/3

if u want to do it with '()' u have to split the string at '(',')' first and do the math within, then write it back without '()'

hope i could help you a little, i think there are other ways to do so, but i only need 6h, fully drunk do write this code ^^

Stefan

Stefan
  • 438
  • 4
  • 9