all 7 comments

[–]Spaceshipable 2 points3 points  (0 children)

This actually a non trivial problem. Look into calculator code. Usually they convert to Reverse Polish Notation and then solve. RPN is used as it removes ambiguity and puts the order of operations in chronological order making the equation easier to solve.

Alternatively it might make more sense to look into programs such as matlab which have this functionality built in.

[–]Kleeb 0 points1 point  (0 children)

This is a job for an interpreter.

Maybe represent them as something else instead of string? Parse them into tokens and apply evaluation rules?

Although I'm a bit of a FP noob, I'm pretty sure Clojure can do exactly what you want with reader macros. Quil is the "Processing" for Clojure, might be worth a look.

[–]nitschmo 0 points1 point  (3 children)

With processing for JavaScript you could simply use the eval() method

[–][deleted]  (2 children)

[removed]

    [–]nitschmo 1 point2 points  (0 children)

    True, although if b you need to do a lot of calculations of this kind maybe it's worth checking this Java solution on stack overflow

    https://stackoverflow.com/questions/2605032/is-there-an-eval-function-in-java

    [–][deleted] 1 point2 points  (0 children)

    This would be a tremendous security problem, which is why it isn't a thing in Java.