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....
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 andQuote:>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....
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.
You need lex and yacc (or equivalents -- the popular GNU versions are calledQuote:> 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....
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
>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....
1. Evaluating a Mathematical expression?
Chad,
> Is there a Windows API call that can evaluate a Mathematical
> expression that is within a string?
Sorry, no. I would recommend getting a decent lexer/parser generator (my
favorite is Antler).
--
Cheers,
Felix.
If you post a reply, kindly refrain from emailing it, too.
No anti-spam address here. Just one comment: IN YOUR FACE!
2. How to open Excel file in Lotus 123 v.5
3. debugger: seeing strings, evaluate arbitrary expressions, real watchpoints
4. SN#9361 Enhancing Oracle Performance with VERITAS Database Edition Storage Mapping Option
5. Mathematical Expression Parser has moved
6. Parammeter for Units Length.
7. parser for mathematical evaluation of expressions
8. Windows 95 on old 386 notebook: pros and cons ?
9. Optimizing Mathematical Expressions For Size
10. A Class for recording mathematical expressions?
11. Help on parsing Mathematical expression
12. CXX0030: Error: expression cannot be evaluated
13. Evaluating very simple maths expressions - for example (5*(5-6))/3