Objective Toolkit : Chapter 16 Utility Classes : Formula Engine
Formula Engine
Objective Toolkit now includes a Lex (Flex)-based formula evaluation engine. By instantiating objects of the toolkit-defined class SRFormulaScanner, developers can easily add formula evaluation functionality to their applications.
Features of the Formula Scanner
The formula scanner:
Supports decimal and hexadecimal representations of numeric input.
Supports all common arithmetic, trigonometric, and bitwise logical functions.
Supports common string operations (e.g., LEFT, MID).
Tests for formula validity and error explanations.
Is usable in Unicode and single-byte character mode applications.
Parsing is driven by a Flex-generated, finite state, machine type, regular expression parser.
Use of the Formula Engine Class
Use of the formula engine class is straightforward.
1. In any source file in which you wish to incorporate formula evaluation functionality, include the following toolkit-supplied header files, as follows:
 
#include <SRFormulaScanner.h>
#include <errorcodes.h>
2. Declare an instance of the formula engine class, as follows:
 
SRFormulaScanner scan;
3. Set the formula, tell the formula engine to evaluate it, and extract the answer (or an error message), as follows:
 
scan.lex(_T("SUM(3,4,(6 * (4+SQRT(9))))"));
if (scan.IsValid())
{
_TCHAR *cb = NULL;
scan.GetResult(&cb);
//cb now points to the answer, formatted as a string
if(cb)
free(cb);
//since the answer string is dynamically allocated
//within the formula engine, you must free it to avoid a
//memory leak
}
else
{
_TCHAR * errdesc = NULL;
scan.GetErrDescription(&errdesc);
//errdesc points to an error message string.
//this string is not dynamically allocated, and
//therefore does not have to be freed.
}
 
NOTE >> See the sample included with the product for an example of how the formula engine can be used.