473,769 Members | 3,755 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_expres sion(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 4884


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_expres sion(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.i nvalidwrote:
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.or g/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_expres sion(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_expres sion(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.mi sc
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.col umbia.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
2609
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, Evan
1
3241
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 SolarNoon datediff("n",Sunrise,Sunset) etc. This is supposed to be evaluated with TimeofNoon=Eval(EventExpression)
19
2540
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
4548
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 I have to make it decimal before calculating, but I am not sure about it. Any light upon this simple (or it seems to me so, at least) begginner problem? TIA..
16
5138
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. And K&R2 mentions "signed extension" everywhere. Reading some old clc posts, I've beginning to realize that these books are over-generalizing the topic. I am just wondering what the difference between the following pairs of terms are: 1)...
3
8024
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
33869
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 should return 0 or false( ( 3 + 6 ) / 3 ) > ( ( 5 + 3 ) / 4 ) --> this should return 1 or trueThanks for any comment or
8
13129
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
12343
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) end sub Well, of course it's posible, and can be done with some characters mannaging. This way you can complicate it as much as you want. The way i usualy do it is in 5 simple steps: 1. Create 2 string arrays, and save numbers in one and...
0
9583
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10039
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9990
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9860
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8869
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7406
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6668
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3955
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 we have to send another system
3
2814
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.