"(4-5)*3","((4+6)/2)*3" how to get the answer from a string expression like this in kotlin. The order of evaluation is always left to right and this is indicated by “compulsory” parentheses if the arithmetic expression involves more than 2 terms
Asked
Active
Viewed 43 times
0
-
1There's no built-in way to do it without coding all the complicated logic yourself. I would use a library like `exp4j`. – Tenfour04 Mar 15 '22 at 14:35
-
Given the restricted preconditions of always left to right and compulsory parentheses, this looks like an exercise to create a trivial evaluator. Just ignore the parentheses, read each number into an accumulator variable, then read the operator and the next number, do the sum and update the accumulator, and repeat. (If negative numbers are allowed, this will be much more difficult.) – k314159 Mar 15 '22 at 14:41
-
Practically [the same question](/71466439/convert-arithmetic-expression-of-a-string-type-to-an-integer-type) was asked only yesterday… – gidds Mar 15 '22 at 14:57