CodeGuru
Earthweb Search
Forums Wireless Jars Gamelan Developer.com
CodeGuru Navigation
RSS Feeds

RSSAll

RSSVC++/C++

RSS.NET/C#

RSSVB

See more EarthWeb Network feeds

follow us on Twitter

Member Sign In
User ID:
Password:
Remember Me:
Forgot Password?
Not a member?
Click here for more information and to register.

Become a Marketplace Partner

jobs.internet.com

internet.commerce
Partners & Affiliates
















Home >> Visual C++ / C++ >> Miscellaneous >> Miscellaneous >> Math


Simple Numerical Formula Parser
Rating: none

Ralf Wirtz (view profile)
April 5, 2000


(continued)




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

Tools:
Add www.codeguru.com to your favorites
Add www.codeguru.com to your browser search box
IE 7 | Firefox 2.0 | Firefox 1.5.x
Receive news via our XML/RSS feed







RATE THIS ARTICLE:   Excellent  Very Good  Average  Below Average  Poor  

(You must be signed in to rank an article. Not a member? Click here to register)

Latest Comments:
Hmm - NN_PP (10/13/2004)
Nice, but minor mistakes. - Legacy CodeGuru (09/03/2003)
Added function to check parenthesys - Legacy CodeGuru (01/07/2003)
Nice work: Fix for text insertion - Legacy CodeGuru (12/24/2002)
Good work! Only some minor problems fixed. - Legacy CodeGuru (12/05/2002)

View All Comments
Add a Comment:
Title:
Comment:
Pre-Formatted: Check this if you want the text to display with the formatting as typed (good for source code)



(You must be signed in to comment on an article. Not a member? Click here to register)