0

Evalute string mathematical equation in java

I wanna know if it's possible to develop a method or algorithm to evaluate a mathematical string expression recived as parameter to a method. It should: -Replace "x" by whatever number -Follow PEMDAS rules -Evaluate correctly To simplify: -Assume expression is correctly written and without spaces. -Forget about "." and "," notation. All integers -No sin or cos functions -All parenthesis are "(" or ")". No "[{}]" Edit: For example (4*x+2)+(16/x-7)^3+4 when x=4

16th Jan 2020, 7:46 PM
Sebastián M. González
Sebastián M. González - avatar
5 Respostas
0
What expression for example?
16th Jan 2020, 7:51 PM
Jnn
Jnn - avatar
0
Yes thats definitely possible. You just have to evalute the expressions in the brackets first and always calc * and / before + and -
16th Jan 2020, 7:54 PM
Jnn
Jnn - avatar
0
What about having several expressions in brackets? That's the main problem. After identifying the first operations some other bracket could be there or not. Therefore the same methoud should evaluate this expression before evaluating the top expression. For example (4*x+2)+(1-((3+4)*4)/2)*10 when x=4
16th Jan 2020, 7:59 PM
Sebastián M. González
Sebastián M. González - avatar
0
Yes you always have to find the most inner expression first and evaluate it
16th Jan 2020, 8:00 PM
Jnn
Jnn - avatar
0
You can for example go though the whole string and find the first closing bracket and save the index of the last opening bracket to get the substring of the inner expression
16th Jan 2020, 8:02 PM
Jnn
Jnn - avatar