Simple Numerical Formula Parser
This article was contributed by Ralf Wirtz.
Environment: VC6 SP3, Windows 95/98/NT/2000
The class CFormulaParser encapsulates a very useful mathematical parser for analytic/numerical formula transformation.
The formula dialog above is actually not necessary for the functionality but allows you comfortable to put in a formula string.
Hereby an extract from the header file:
class CFormulaParser
{
private:
//Implementation
CString m_strFormula;
CString m_strFunction;
CString m_strErrortext;
double m_dFktValue;
double m_dFunctionConstant[ANZFUNKTKONST];
CStringArray m_strStandardFunction;
double SignFactor(WORD& nPosition, CString& strCharacter);
double Expression(WORD& nPosition, CString& strCharacter);
double SimpleExpression(WORD& nPosition,
CString& strCharacter);
double Term(WORD& nPosition, CString& strCharacter);
double Factor(WORD& nPosition, CString& strCharacter);
double Char_n(WORD& nPosition, CString& strCharacter);
public:
CString GetFormula();
void SetFormula(CString Formula);
void SetFunctConst(int index, double val);
CFormulaParser();
virtual ~CFormulaParser();
//Interface
double Calculation(CString strFormula,
double xValue,
WORD&
ErrorPosition,
CString&
Errortext);
};
The main method in CFormulaParser for the evaluation is:
double Calculation(CString strFormula,
double xValue,
WORD&
ErrorPosition,
CString&
Errortext);
where
- strFormula is the formula string, e.g. "b*sin(x)/x"
- xValue is in this case x, e.g. 3.14 (point instead comma!)
- ErrorPosition gives back the error position in the string
- Errortext gives back the corresponding error text
The parser is part of the program SimplexNumerica. For more information, please see my German website http://www.SimplexNumerica.de.
Download
Download demo project (with source) - 104 KbThis article was originally published on April 5, 2000