Simple Numerical Formula Parser | CodeGuru

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 { private: //Implementation CString m_strFormula; CString m_strFunction; […]

Written By
CodeGuru Staff
CodeGuru Staff
Apr 5, 2000
1 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

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 Kb

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.