Simple Numerical Formula Parser

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  
 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);
 CString GetFormula();
 void SetFormula(CString Formula);
 void SetFunctConst(int index, double val);
 virtual ~CFormulaParser();

 double Calculation(CString strFormula, 
                    double xValue, 
The main method in CFormulaParser for the evaluation is:
double Calculation(CString strFormula, 
                   double xValue, 
  • 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


Download demo project (with source) - 104 Kb


  • Hmm

    Posted by NN_PP on 10/13/2004 03:17am

    Hey 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?

  • Nice, but minor mistakes.

    Posted by Legacy on 09/03/2003 12:00am

    Originally posted by: oleg63

    If you type something like (2+3)*
    the parser wan't give an error and point on extra character

  • Added function to check parenthesys

    Posted by Legacy on 01/07/2003 12:00am

    Originally 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 == '(')
    else if (cInput == ')')
    // Check number of open parenthesys is OK
    if (iOpenPar > iClosePar)
    // missing close parenthesys
    return FALSE;
    // Check number of close parenthesys is OK
    else if (iOpenPar < iClosePar)
    // missing open parenthesys
    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.

  • Nice work: Fix for text insertion

    Posted by Legacy on 12/24/2002 12:00am

    Originally posted by: Jan S. Yoder

    Nice work, and a pretty good calculator.  However, I tried to insert parentheses around your default text and had to go to the keyboard to do it.  I added a CEdit variable named m_EditInputControl to the header file and a DDX_Control call, then changed your function as shown here.  This retains the current selection location and inserts the character pressed on the on-screen keyboard at the appropriate location in the existing string.

    In SimplexParserDlg.h
    CEdit m_EditInputControl;

    In SimplexParserDlg.cpp

    DDX_Control(pDX, IDC_FORMELEINGABE, m_EditInputControl);

    void CSimplexParserDlg::SetEingabe(CString str)

    int nStart = -1, nEnd = -1;
    m_EditInputControl.GetSel(nStart, nEnd);

    CString strExistingText(m_strFormulainput);

    int nLength = strExistingText.GetLength();

    if (nLength > 0)
    strExistingText = strExistingText.Mid(0,
    nStart) + str + strExistingText.Mid(nEnd);
    strExistingText += str;

    nStart += str.GetLength();
    nEnd = nStart;

    m_strFormulainput_vorher = strExistingText;
    m_strFormulainput = strExistingText;

    m_EditInputControl.SetSel(nStart, nEnd);

  • Good work! Only some minor problems fixed.

    Posted by Legacy on 12/05/2002 12:00am

    Originally 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!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Anthony Christie, the Chief Marketing Officer for Level Communications, is responsible for customer experience, worldwide marketing and product management. In this informative asset, he shares his insights into why a private network connection to cloud-bases applications is the right decision for your enterprise. Download now to find out more.

  • Enterprises are increasingly looking to platform as a service (PaaS) to lower their costs and speed their time to market for new applications. Developing, deploying, and managing applications in the cloud eliminates the time and expense of managing a physical infrastructure to support them. PaaS offerings must deliver additional long-term benefits, such as a lower total cost of ownership (TCO), rapid scalability, and ease of integration, all while providing robust security and availability. This report …

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date