Evaluate Expression in String


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: Evaluate Expression in String

  1. #1
    Mark Guest

    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

  2. #2
    Jo Desmet Guest

    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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center