Need to evaluate simple arithmetic expression

M

mstorkamp

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.
 
J

jameskuyper

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.
 
R

Richard Heathfield

(e-mail address removed) 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.
 
M

mstorkamp

H

hsmit.home

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
 
U

user923005

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top