473,503 Members | 1,805 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Need to evaluate simple arithmetic expression

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 '07 #1
6 4857


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.
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 '07 #2
ms*******@yahoo.com 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
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
Nov 20 '07 #3
On Nov 20, 10:19 am, Richard Heathfield <r...@see.sig.invalidwrote:
mstork...@yahoo.com 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
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 '07 #4
ms*******@yahoo.com 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_
Nov 20 '07 #5
On Nov 20, 4:49 pm, 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.
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
Nov 20 '07 #6
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.
Nov 20 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2589
by: evan | last post by:
Can you give me a hint as how to go about evaluating such an expression. The actual expression will be different every time (user input). The data will be sorted in an array or a list. Thanks, ...
1
3222
by: Penguin | last post by:
For some odd reason, I am trying to do some complex time calculations that I want to let the user define. So I set up a table with EventID EventType EventExpression 1 ...
19
2507
by: Mark A. Odell | last post by:
Is NULL guaranteed to evaluate 'not true', e.g. is it completely safe and portable to write: char *pFoo = malloc(1024); if (pFoo) { /* use pFoo */ free(pFoo); }
5
4535
by: Yodai | last post by:
Hi all! I have an int that comes with a value like 0x07fa and I have to turn it into a float value of 204.2 decimal to display it.... if I try to divide it by 10 I get bogus numbers. I presume...
16
5099
by: TTroy | last post by:
Hello, I'm relatively new to C and have gone through more than 4 books on it. None mentioned anything about integral promotion, arithmetic conversion, value preserving and unsigned preserving. ...
3
8004
by: Coco | last post by:
Hi! Does c# has any class method that will be able to evaluate a given string e.g ((True && False) || (True && True)) and return the result in boolean for this case is true Thanks!
13
33795
by: Zeng | last post by:
Hello, Please help!!! I've been stuck on this issue for months. I just wonder if there is a way to programmatically evaluate expression strings such as ( ( 3 + 5 ) / 2 ) > 4 --> this...
8
13111
by: No Such Luck | last post by:
Is there anyway to literally evaluate the contents of a string in an if statement? For example: int i = 0; char * str = "i == 0"; if(str) /* I know this doesn't do what I want */ {
2
12254
kadghar
by: kadghar | last post by:
Many people asks if there is a way to write a mathematical expression, writen as a string in a text box, so they can do something like: sub something_click() textbox2.text=eval(textbox1.text)...
0
7199
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7074
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7322
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6982
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
3161
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3150
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1501
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
731
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
374
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.