# In-Program Evaluation

**Joachim Raidl**on

**September 6th, 1998**

In my projects I often need to evaluate expressions. So I created a class that provides evaluation of expressions with the posibility to declare variables and easy functions. For this article I include the XEvaluate-class in a 'calculator'-dialog, but normaly, I use the class in my application like this: XString erg = Evaluate("1+2*sqr(3)").

!!! The XString-class that XEvaluate uses, was also contributed at codeguru.

You can use external variables in a XDoubleMap. This typedef is allready defined in XEvaluate.h (typedef CMap<CString, LPCTSTR, double, double> XDoubleMap;):

XDoubleMap var; var["alpha"] = 10; var["beta"] = 20; var["test"] = 1.234; double erg = dtoa(Evaluate("alpha+beta", var));

It's possible to use userdefined functions with one argument (this argument has to be 'x')

Evaluate("f:=sin(x)+cos(x)"); double erg = dtoa(Evaluate("f(pi/2)"));

You can also call Evaluate with more than one expression, if you divide the expressions by a semikolon:

double erg = dtoa(Evaluate("a=10;b=20;c=a+b");

The following functions and operations are included in XEvaluate:

sin | cos | tan | trigonometric functions | |||

asin | acos | atan | inverse trigonometric functions | |||

rad | deg | gon | trigonometric calculation type | |||

pow | sqr | sqrt | exp | log | log10 | power, square, squareroot, exp, natural log, tenth log |

abs | int | fract | fak | cube | absolute, integer part, factional part, n!, cube | |

dec | hex | bin | char | outputtype | ||

fix | set the decimal digits | |||||

+ | - | * | / | add, sub, mul, div | ||

% | & | | | ^ | ! | ~ | modulo, binary and, or, xor, not, binary not |

< | > | <= | >= | == | != | less, greater, less or equal, greater or equal, equal, not equal |

= | += | -= | *= | /= | variable setting | |

@ | old value (result of the last calculation) | |||||

? | help (lists all functions) | |||||

# | reset (delete all functions and variables) | |||||

' | actual status (number of decimal digits, trigonometric mode and output mode) | |||||

$ | var and function list |

Use the sample program to find out what's possible with this class.