evaluate a string as a mathematical expression

evaluate a string as a mathematical expression

Post by Andreas Nage » Thu, 10 Aug 2000 04:00:00



I need to evaluate a string as a mathematical expression :

exemple if s is char*
and     s = "a*2 + exp(3*x)"
double a = 3.0
double x = 2.5
I need a function evalf(s)that  return : 1814.04241446....

 
 
 

evaluate a string as a mathematical expression

Post by Andreas Kaha » Thu, 10 Aug 2000 04:00:00




Quote:>I need to evaluate a string as a mathematical expression :

>exemple if s is char*
>and     s = "a*2 + exp(3*x)"
>double a = 3.0
>double x = 2.5
>I need a function evalf(s)that  return : 1814.04241446....

The need for an eval()-function has popped up in both this group and
some other C and C++ related groups lately. Is there a home assignment
going on at some big university somewhere?

C++ does not support run-time generation/evaluation of code (as e.g.
Perl does) and thus lacks the eval()-function you are looking for.

What you have to do is to implement a parser that reads the
expression, identifies the variables and mathematical functions and
evaluates them.

One way to do this is to generate an expression tree with each node
being a operation with left and right operands as children (pick up a
book on data structures). Another way is to convert the [infix]
expression to a postfix expression and use a stack to evaluate it.
Variables may be stored in a linked list.

/A

--
# Andreas K?h?ri, <URL:http://hello.to/andkaha/>.
# ...brought to you from Uppsala, Sweden.
# All junk e-mail is reported to the appropriate authorities.
# Criticism, cynicism and irony available free of charge.

 
 
 

evaluate a string as a mathematical expression

Post by Tristan Mille » Thu, 10 Aug 2000 04:00:00


Greetings.


Quote:> I need to evaluate a string as a mathematical expression :

> exemple if s is char*
> and     s = "a*2 + exp(3*x)"
> double a = 3.0
> double x = 2.5
> I need a function evalf(s)that  return : 1814.04241446....

You need lex and yacc (or equivalents -- the popular GNU versions are called
flex and bison).  These programs will create for you a lexer (token
recognizer) and parser (grammar processor) in C, based on the rules you feed
them.  (Technically speaking, it would be possible to write your own lexer
and parser, but it will probably be more trouble than it's worth.)

If you're on a Un*x system, lex/flex and yacc/bison are probably already
installed; if not, try grabbing sources from http://www.gnu.org.

--

"Close your mouth and not try to make me look fool, you are."
        -- Ioannis Vranos

 
 
 

evaluate a string as a mathematical expression

Post by David Harmo » Thu, 10 Aug 2000 04:00:00


On Wed, 09 Aug 2000 10:43:57 +0200 in comp.lang.c++,


>I need to evaluate a string as a mathematical expression :

>exemple if s is char*
>and     s = "a*2 + exp(3*x)"
>double a = 3.0
>double x = 2.5
>I need a function evalf(s)that  return : 1814.04241446....

I deduce from he fact that you are posting here instead of
comp.sources.wanted or doing a web search or checking the "Available C++
libraries FAQ" that you want to write your own version and NOT use one
that someone else has written.  In that case, the calculator example in
Stroustrup _The C++ Programming Language, Third Edition_ is a good
starting point.  Beware of the long side trip you will be starting if
you try to use YACC etc. for such a simple task.