Click here to Skip to main content
Click here to Skip to main content

A powerful function parser

By , 22 Feb 2000
 
  • Download source files - 82 Kb
  • The inspiration to write this code was to have an easy-to-use parser for functions given as string. You have a string like "cos(x)-7*x/(1+x)", and you want to compute some values, maybe for displaying the graph of the function. You can write code like this:

    CFunction<long double>* fkt = CFunction<long double>::Parse("cos(x)-7*x/(1+x)");
    long double y = fkt->Execute(1.5);

    There are lots of definitions in the background, but the result is easy and fast! There is a one-time pasing step to construct the function tree, and function evaluations are only calls of standard math functions like sin, +, *, sqrt. When you want to know details about the implementaion, you should look into the source code. It would be too complex to describe the parsing algorithm here.

    License

    This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

    A list of licenses authors might use can be found here

    About the Author

    Andreas Jäger
    Web Developer
    Germany Germany
    Member
    No Biography provided

    Sign Up to vote   Poor Excellent
    Add a reason or comment to your vote: x
    Votes of 3 or less require a comment

    Comments and Discussions

     
    You must Sign In to use this message board.
    Search this forum  
        Spacing  Noise  Layout  Per page   
    GeneralMFC compilememberLuke Murphey6 Jul '02 - 9:28 
    I am working on a port of UNIX Xspread to windows. I am attempting to integrate this code into my MFC application, however, I cannot get it to compile. Below is the code snippet intended to get the result:
     
    CString Expression;
     
    //Call the math parser, get the function result and set the cell value to the result
    CFunction* bob = CFunction::Parse(m_strText);
    Expression = bob->GetName();
     

    Note: when I post this message to this site, the template prefix argument keeps disappearing (strange). CFunction has a template argument of long double (so does the scope for the call to Parse). This is OK in my application.
     
    Here is the errors:
     
    C:\My Documents\MyProjects\Xspread32\xspread32\Grid_src\GridCell.cpp(212) : error C2065: 'CFunction' : undeclared identifier
    C:\My Documents\MyProjects\Xspread32\xspread32\Grid_src\GridCell.cpp(212) : error C2062: type 'long double' unexpected
    C:\My Documents\MyProjects\Xspread32\xspread32\Grid_src\GridCell.cpp(212) : error C2039: 'Parse' : is not a member of '`global namespace''
    C:\My Documents\MyProjects\Xspread32\xspread32\Grid_src\GridCell.cpp(213) : error C2065: 'bob' : undeclared identifier
    C:\My Documents\MyProjects\Xspread32\xspread32\Grid_src\GridCell.cpp(213) : error C2227: left of '->GetName' must point to class/struct/union
     

    I did include the header files, and the IDE does display that the object "func" is a CFunction object (i.e. when you hover the cursor over the object name in MVC++ 6). I just can't make a connnection between my code and this math parser. I downloaded the command line code and it compiles with no problems. I created a new MFC project and tried to integrate the parser in the same manner as the console code that worked & I got the same errors. I am relatively new to MFC (not new to C++ however) and I could use any help I can get.
     
    Luke
    GeneralRe: MFC compilememberlLukeMurphey8 Jul '02 - 6:56 
    I figured it out. Had the #includes for the math parser's headers over #include stdafx.h.
    GeneralA version without MFC / MS dependenciesmemberJochenKa14 Feb '02 - 20:23 
    Hello,
     
    I did a version of this source without any MS dependecies... If someone is interested, please mail me...
    GeneralRe: A version without MFC / MS dependenciesmemberjean-luc adam19 Feb '02 - 1:50 
    Hello,
     
    I'm looking for a function parser and I would be very interested by your source without MS dependencies because i must run my application on unix platform.
     
    Could you send me a copy of your version ?
     
    Thanks,
     
    Jean-Luc
    GeneralRe: A version without MFC / MS dependenciesmemberJochen Kalmbach29 Jan '04 - 19:31 
    http://srvreport.kalmbachnet.de/FunctionParser-WithoutMFC.zip[^]
     
    Greetings
    Jochen
    GeneralRe: A version without MFC / MS dependenciesmemberPhilippe Lhoste4 Dec '04 - 2:25 
    Thank you for the code.
    I would suggest you remove the .ncb file from your package, that would reduce dramatically the download size...
     
    Philippe Lhoste (Paris -- France)
    Professional programmer and amateur artist
    http://phi.lho.free.fr

    GeneralRe: A version without MFC / MS dependenciesmemberJochen Kalmbach [MVP VC++]1 Jan '06 - 19:58 
    Here is a smaller version:
    http://blog.kalmbachnet.de/files/FunctionParser-WithoutMFC.zip[^]
     
    Greetings
    Jochen

    Generalmore General expressionmemberJie Luo16 Jan '02 - 1:56 
    Hi There,
    I just found the code. I have not tested it yet but it seems to be the Function parser is a good parser. My question is that the parser can pass following expression:
    f(x)=2*x+5
     
    I guess it should be a big problem to pass following more general expression
    f(x)=a*x+b;
     
    Thanks for any advice!
     
    Jie Luo
    GeneralWorld peacememberBartosz Bien14 Dec '01 - 13:14 
    Don't argue you guys! Remember - world peace Smile | :)
    And now let's reach the another abstraction level. One definitely doesn't need such a complicated machine to perform parsing. All of you should check the free code I found at http://www.students.tut.fi/~warp/FunctionParser/. It's truly amazing how simple things work best.
     

     
    Regards,
    BB
     
    P.S. Andreas, for God's sake - write comments in English!

    GeneralTo B.Starks, W.Dean, & J.AndreasmemberAmit Gefen8 Nov '01 - 12:43 
    1st, thanks to Andreas of sharing his code. Smile | :)
    2nd, B.Starks probably right that W.Dean does not know as much as he loud speaks. But he, & not concern to his programming abilities, right in one point: If you, Andreas, post an article here - make it readable. Dont expect us to read German inline documantation and write more that two sentences that say only "it will be too complex to describe".
     
    Amit Gefen.
    Project Lead - Software Engineer
    Dusseldorf, Germany.

    General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

    Permalink | Advertise | Privacy | Mobile
    Web03 | 2.6.130523.1 | Last Updated 23 Feb 2000
    Article Copyright 2000 by Andreas Jäger
    Everything else Copyright © CodeProject, 1999-2013
    Terms of Use
    Layout: fixed | fluid