On Nov 20, 7:49 am, mstork...@yahoo.com 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.