Simple Numerical Formula Parser
Posted
by Ralf Wirtz
on April 5th, 2000
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

Comments
Hmm
Posted by NN_PP on 10/13/2004 03:17amHey this can be done by any one, u r simply going and checking each character one by one. Isnt there any other simple means of doing this, for e.g. does the Excel also use the same way?
ReplyNice, but minor mistakes.
Posted by Legacy on 09/03/2003 12:00amOriginally posted by: oleg63
If you type something like (2+3)*
Replythe parser wan't give an error and point on extra character
"*".
Thanks.
Added function to check parenthesys
Posted by Legacy on 01/07/2003 12:00amOriginally posted by: Jordi Duatis
As a response to the author hereby a function to check parenthesys correctness, function is added to the calculation function in this way:
if (!CheckParenthesis())
{
// Formula has an error in parenthesys
ErrorPosition = -1;
Errortext = m_strErrortext;
return 0;
}
/.../
BOOL CFormulaParser::CheckParenthesis()
{
// Count open and close parenthesis to check correctness and forget about it
// for the rest of the process.
char cInput;
int iOpenPar = 0, iClosePar = 0;
// Go through all characters in the expression and check each char
for( int ii=0; ii<m_strFormula.GetLength();ii++)
{
// Get current char
cInput = m_strFormula.GetAt(ii);
// register parenthesis number and avoid tokenize it
if (cInput == '(')
{
iOpenPar++;
}
else if (cInput == ')')
{
iClosePar++;
}
}
// Check number of open parenthesys is OK
if (iOpenPar > iClosePar)
{
// missing close parenthesys
m_strErrortext.LoadString(IDS_MISSING_CLOSE_PAR);
return FALSE;
}
// Check number of close parenthesys is OK
else if (iOpenPar < iClosePar)
{
// missing open parenthesys
m_strErrortext.LoadString(IDS_MISSING_OPEN_PAR);
return FALSE;
}
return TRUE;
}
In Calculation function, error position is changed from dword to int to allow negative values. In fact, error checking has been improved adding exception handling and in the function calculation a complete try{}catch block has been implemented.
I've sent the complete module modified to the author, expecting he would update the post.
Best whishes.
ReplyNice work: Fix for text insertion
Posted by Legacy on 12/24/2002 12:00amOriginally posted by: Jan S. Yoder
ReplyGood work! Only some minor problems fixed.
Posted by Legacy on 12/05/2002 12:00amOriginally posted by: Jordi Duatis
Firstable, thanks for this code. Is exactly what I could expect for a math formula parser and interpreter. Works quite fine, but there was some minor problems in the CFormulaParser class I had to fix to add this code to my project:
1- Errors where not correctly reported. I added an exception that catches error message and position and can be trapped in the Calculation function.
2- There was a trick for division by 0 not suitable in all cases. I prefer to issue an error in this case.
3- Did not check for parenthesys.
4- Char_n returned an unused value
As you can see minor problems. I can provide the code if needed.
Good work!
Reply