Velocity Reviews > Need to evaluate simple arithmetic expression

# Need to evaluate simple arithmetic expression

mstorkamp@yahoo.com
Guest
Posts: n/a

 11-20-2007
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

jameskuyper@verizon.net
Guest
Posts: n/a

 11-20-2007

(E-Mail Removed) 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

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.

Richard Heathfield
Guest
Posts: n/a

 11-20-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) said:

> I need to evaluate simple arithmetic expressions.

This might do the trick:

http://webscripts.softpedia.com/scri...l-C-26706.html

> I've had no luck googling for such a function. Does anybody have one

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@
"Usenet is a strange place" - dmr 29 July 1999

mstorkamp@yahoo.com
Guest
Posts: n/a

 11-20-2007
On Nov 20, 10:19 am, Richard Heathfield <(E-Mail Removed)> wrote:
> (E-Mail Removed) said:
>
> > I need to evaluate simple arithmetic expressions.

>
> This might do the trick:
>
> http://webscripts.softpedia.com/scri...l-C-26706.html
>
> > I've had no luck googling for such a function. Does anybody have one

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

Ben Pfaff
Guest
Posts: n/a

 11-20-2007
(E-Mail Removed) 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_

hsmit.home@gmail.com
Guest
Posts: n/a

 11-20-2007
On Nov 20, 4:49 pm, (E-Mail Removed) 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

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/rpnex...nevaluator.asp

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

Hans Smit

user923005
Guest
Posts: n/a

 11-20-2007
On Nov 20, 7:49 am, (E-Mail Removed) 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

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

You could always use yacc or bison or the gold parser to make your own
grammar. Might be a fun exercise.

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is OffTrackbacks are On Pingbacks are On Refbacks are Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post Pradyut Java 6 06-28-2008 01:45 AM John Doe Java 0 04-17-2005 10:03 PM joshc C Programming 5 03-31-2005 02:23 AM Jason luo C Programming 3 08-19-2004 12:43 AM Ken Hilton Javascript 6 05-19-2004 01:09 PM