-
Evaluate Expression in String
I need to evaluate an expression from a string. Example user enters: (5.25
* 1.5) + .50
I am hoping for a method that does this, but if a simple algorithm is available
I would appreciate that as well.
Thanks in Advance
-
Re: Evaluate Expression in String
Java itself does not provide a mechanism to do this. The difficulty in those
kind of expressions is that there is precedence to take into account, plus
the possibility for multiple levels of parenthesing. It is simpler to
evaluate an expression written in RPN (Reverse Polish Notation), because it
has explicit precedence.
In this case your expression would write as
5.25 1.5 * .50 +
which reads as:
put 5.25 on the stack,
put 1.5 on the stack,
remove and multiply top 2 numbers on stack and put result on stack
put .50 on the stack
remove and add top 2 numbers on the stack and put result on stack
the result is now the only element on the stack.
for evaluating infix notations, there are some different proven ways. One of
them is by building up an evaluation tree. Another way is by trying to
convert from infix into postfix notation.
However, I have already developped a tool that can evaluate infix
expressions. If you are interested into trying it, you can drop me an email
(jo_desmet@yahoo.com) or get it from
http://officefronts.hotdispatch.com/HiFliteTM
non-comercial use is free.
There are also tools around that can build these kind of parsers from a
specification you give (like yacc for c).
"Mark" <Mark@InteractiveFS.com> wrote in message
news:3990338a$1@news.devx.com...
>
> I need to evaluate an expression from a string. Example user enters:
(5.25
> * 1.5) + .50
>
> I am hoping for a method that does this, but if a simple algorithm is
available
> I would appreciate that as well.
>
> Thanks in Advance
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks