Need to evaluate simple arithmetic expression

Discussion in 'C Programming' started by mstorkamp@yahoo.com, Nov 20, 2007.

  1. Guest

    I need to evaluate simple arithmetic expressions.

    I wrote a quick and dirty program that allows me to enter dimensions
    off of a drawing, and based on global or local tolerances calculate an
    upper and lower limit to put in an inspection worksheet.

    When it asks for a dimension I would like to be able to type in
    something like: '(12.5+10)/2' or '150-3.45'

    What I'm looking for is a function along the lines of 'double
    evaluate_expression(char *)' that would solve simple 4-function
    arithmetic with parenthesis.

    (and I don't want to have to write it myself)

    I've had no luck googling for such a function. Does anybody have one
    or have a link to download one? Thanks.
    , Nov 20, 2007
    #1
    1. Advertising

  2. Guest

    wrote:
    > I need to evaluate simple arithmetic expressions.
    >
    > I wrote a quick and dirty program that allows me to enter dimensions
    > off of a drawing, and based on global or local tolerances calculate an
    > upper and lower limit to put in an inspection worksheet.
    >
    > When it asks for a dimension I would like to be able to type in
    > something like: '(12.5+10)/2' or '150-3.45'
    >
    > What I'm looking for is a function along the lines of 'double
    > evaluate_expression(char *)' that would solve simple 4-function
    > arithmetic with parenthesis.
    >
    > (and I don't want to have to write it myself)
    >
    > I've had no luck googling for such a function. Does anybody have one
    > or have a link to download one? Thanks.


    Do you really need a function? Would a stand-alone program be
    acceptable? I would ordinarily use a spreadsheet program for that kind
    of stuff. If you're using a Unix-like system, there's also a program
    called bc that makes it easy to evaluate simple expressions - but
    definitely read the man page before trying to use it, it's not very
    user-friendly.
    , Nov 20, 2007
    #2
    1. Advertising

  3. said:

    > I need to evaluate simple arithmetic expressions.


    This might do the trick:

    http://webscripts.softpedia.com/script/Snippets/Eval-C-26706.html

    > I've had no luck googling for such a function. Does anybody have one
    > or have a link to download one? Thanks.


    I got the above hit with the search terms

    eval simple c

    (It was the third hit.)

    It probably originates from http://www.snippets.org/ which has got a ton of
    useful stuff. Well worth a look.

    --
    Richard Heathfield <http://www.cpax.org.uk>
    Email: -http://www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999
    Richard Heathfield, Nov 20, 2007
    #3
  4. Guest

    On Nov 20, 10:19 am, Richard Heathfield <> wrote:
    > said:
    >
    > > I need to evaluate simple arithmetic expressions.

    >
    > This might do the trick:
    >
    > http://webscripts.softpedia.com/script/Snippets/Eval-C-26706.html
    >
    > > I've had no luck googling for such a function. Does anybody have one
    > > or have a link to download one? Thanks.

    >
    > I got the above hit with the search terms
    >
    > eval simple c
    >
    > (It was the third hit.)
    >
    > It probably originates fromhttp://www.snippets.org/which has got a ton of
    > useful stuff. Well worth a look.
    >


    Thanks for the links, that's just what I was looking for. (and I tried
    every permutation of arithmetic, function, evaluation, etc I could
    think of, honestly :)
    , Nov 20, 2007
    #4
  5. Ben Pfaff Guest

    writes:

    > I need to evaluate simple arithmetic expressions.


    libmatheval
    http://www.gnu.org/software/libmatheval/
    --
    "...what folly I commit, I dedicate to you."
    --William Shakespeare, _Troilus and Cressida_
    Ben Pfaff, Nov 20, 2007
    #5
  6. Guest

    On Nov 20, 4:49 pm, wrote:
    > I need to evaluate simple arithmetic expressions.
    >
    > I wrote a quick and dirty program that allows me to enter dimensions
    > off of a drawing, and based on global or local tolerances calculate an
    > upper and lower limit to put in an inspection worksheet.
    >
    > When it asks for a dimension I would like to be able to type in
    > something like: '(12.5+10)/2' or '150-3.45'
    >
    > What I'm looking for is a function along the lines of 'double
    > evaluate_expression(char *)' that would solve simple 4-function
    > arithmetic with parenthesis.
    >
    > (and I don't want to have to write it myself)
    >
    > I've had no luck googling for such a function. Does anybody have one
    > or have a link to download one? Thanks.


    Another piece of code that does what you want, and is more C++
    oriented is provided in the following link:

    http://www.codeproject.com/cpp/rpnexpressionevaluator.asp

    It's very clean and concise code. Hat's off to the developer.

    Hans Smit
    , Nov 20, 2007
    #6
  7. user923005 Guest

    On Nov 20, 7:49 am, wrote:
    > I need to evaluate simple arithmetic expressions.
    >
    > I wrote a quick and dirty program that allows me to enter dimensions
    > off of a drawing, and based on global or local tolerances calculate an
    > upper and lower limit to put in an inspection worksheet.
    >
    > When it asks for a dimension I would like to be able to type in
    > something like: '(12.5+10)/2' or '150-3.45'
    >
    > What I'm looking for is a function along the lines of 'double
    > evaluate_expression(char *)' that would solve simple 4-function
    > arithmetic with parenthesis.
    >
    > (and I don't want to have to write it myself)
    >
    > I've had no luck googling for such a function. Does anybody have one
    > or have a link to download one? Thanks.


    It's a FAQ:

    18.14: I need code to parse and evaluate expressions.

    A: Two available packages are "defunc," posted to
    comp.sources.misc
    in December, 1993 (V41 i32,33), to alt.sources in January,
    1994,
    and available from sunsite.unc.edu in
    pub/packages/development/libraries/defunc-1.3.tar.Z, and
    "parse," at lamont.ldgo.columbia.edu. Other options include
    the
    S-Lang interpreter, available via anonymous ftp from
    amy.tch.harvard.edu in pub/slang, and the shareware Cmm ("C-
    minus-minus" or "C minus the hard stuff"). See also questions
    18.16 and 20.6.

    There is also some parsing/evaluation code in _Software
    Solutions in C_ (chapter 12, pp. 235-55).

    Those solutions may no longer be available so the other suggestions
    elsethread may be helpful.

    You could always use yacc or bison or the gold parser to make your own
    grammar. Might be a fun exercise.
    user923005, Nov 20, 2007
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Doe
    Replies:
    0
    Views:
    529
    John Doe
    Apr 17, 2005
  2. Jason luo
    Replies:
    3
    Views:
    532
    Peter Ammon
    Aug 19, 2004
  3. joshc
    Replies:
    5
    Views:
    552
    Keith Thompson
    Mar 31, 2005
  4. Pradyut
    Replies:
    6
    Views:
    1,213
    Arne Vajhøj
    Jun 28, 2008
  5. Ken Hilton
    Replies:
    6
    Views:
    202
    Dr John Stockton
    May 19, 2004
Loading...

Share This Page