0

ok so the variable current is an operator ( +-*/%)

and t1 = stack.pop() is a number in String formation t2 = stack.pop() is a number in String formation also

I need to carry out the mathematical operation t2 current t1 (t2 "operator" t1, basically)

How would you go about this. Thanks in advance!

David Baez
  • 1,208
  • 1
  • 14
  • 26
  • http://stackoverflow.com/questions/3422673/evaluating-a-math-expression-given-in-string-form – Jean Logeart Mar 26 '14 at 16:12
  • 3
    Have you tried anything? Convert the strings to `int`s or `float`s or whatever, determine which operator it is using `String.equals` and perform the operation,. – clcto Mar 26 '14 at 16:12
  • `+` is operator and `"+"` is a string. `t2 current t1` wrong and `t2 + t1` is correct when you want o concat the string `t2` and `t1`. For add the numbers in the strings you have to first convert the strings to numbers and add. – Bhesh Gurung Mar 26 '14 at 16:13
  • 1
    convert the numeric strings to numbers `Double.parseDouble(String)` and do a series of `if` statements to find value of operator `if(Strings.equal("*",operator))` etc. – deanosaur Mar 26 '14 at 16:14
  • Thanks guys, I had done it as the answer provided below suggested, I just wanted to know if there was another way to do it. – David Baez Mar 26 '14 at 21:57

1 Answers1

3

something simple :

enum operation {
    SUM,
    SUB,
    DIV,
    MUL,
}

static String performOperation(operation op, String t1, String t2) {
   // parse t1 & t2 into integers or whatever you use
   switch(op) {
   case SUM:
       //do sum
       break;
   case SUB:
       //do substraction
       break;

   // handle all of them....

}
kiruwka
  • 9,250
  • 4
  • 30
  • 41