This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
I was trying to comopute a string, such as "32*45-7", I'm unsure how to go about this. Do I need to break it down and compute each part...? I tried but is throws an error.
I think my main problem is preserving the operand..? and its position, alot of the calculations have '((' etc..
Any thoughts on how I can tackle something like thingsRegards Zein
You have to parse the expression keeping in mind operator precedence. Here is a programming assignment that gives a pseudocode algorithm along with a discussion.
I was just wondering, given this is (from my perspective) a very common opeation, is there an libraries that Java provides to evaluate arithmetic expressions.
My calculations at the most will have +/-* and brackets... i.e. - (((5+65)/5) + (16+26/2)? Any ideas..?
Not to my knowledge. Certainly not in the Java SE/EE APIs. A quick Google search for "infix calculator" returns lots of academic programming assignments. Maybe you could find a library in those results. [ November 02, 2007: Message edited by: Joe Ess ]
bart zagers
Ranch Hand
Joined: Feb 05, 2003
Posts: 234
posted
0
This recent thread seems to be about the same problem.
JEP is a commercial library doing this, but that might be a little overkill.
I am currently adding such capabilities to an open source project of mine. I am using antlr to parse the expressions. The learning curve for antlr is a little steep (There is a book by the creator Terrence Parr that is good though), though pretty easy to work with once you get the hang of it.
It would be nice if this was added as part of the java platform.
If I may toot my own horn, the just-published edition of the JavaRanch Journal contains an article by me about how to create real Java classes from user-defined functions at runtime. That's quite a bit faster than using a library like JEP that basically interprets the function.
There are other libraries that come much closer in speed to compiled code, like JEL.