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
5 odpowiedzi
0
What expression for example?
0
Yes thats definitely possible. You just have to evalute the expressions in the brackets first and always calc * and / before + and -
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
0
Yes you always have to find the most inner expression first and evaluate it
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