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

A powerful function parser

By , 22 Feb 2000
Rate this:
Please Sign up or sign in to vote.
  • 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
    No Biography provided

    Comments and Discussions

     
    GeneralMFC compile PinmemberLuke Murphey6-Jul-02 9:28 
    GeneralRe: MFC compile PinmemberlLukeMurphey8-Jul-02 6:56 
    GeneralA version without MFC / MS dependencies PinmemberJochenKa14-Feb-02 20:23 
    GeneralRe: A version without MFC / MS dependencies Pinmemberjean-luc adam19-Feb-02 1:50 
    GeneralRe: A version without MFC / MS dependencies PinmemberJochen Kalmbach29-Jan-04 19:31 
    GeneralRe: A version without MFC / MS dependencies PinmemberPhilippe Lhoste4-Dec-04 2:25 
    GeneralRe: A version without MFC / MS dependencies PinmemberJochen Kalmbach [MVP VC++]1-Jan-06 19:58 
    GeneralRe: A version without MFC / MS dependencies PinmemberMember 190613125-Jul-13 13:18 
    Generalmore General expression PinmemberJie Luo16-Jan-02 1:56 
    GeneralWorld peace PinmemberBartosz Bien14-Dec-01 13:14 
    GeneralTo B.Starks, W.Dean, & J.Andreas PinmemberAmit Gefen8-Nov-01 12:43 
    GeneralOperators/precedence/types etc PinmemberAndrew Phillips26-Sep-01 18:04 
    GeneralVery good success with this PinmemberJeff12-May-01 8:59 
    GeneralYou must read jeff's reply..!! then you will success PinmemberJsSong25-Mar-02 14:47 
    GeneralBugs etc below should be renamed to Dean Wyant knows little about programming. PinmemberB. Starks13-Apr-01 7:36 
    GeneralRe: Bugs etc below should be renamed to Dean Wyant knows little about programming. PinmemberDean Wyant30-Apr-04 10:11 
    GeneralA good Parser Pinmemberstocklab18-Jan-01 17:57 
    GeneralRe: A good Parser PinmemberPing He9-May-01 23:36 
    GeneralRe: A good Parser PinmemberMathePeter3-Jul-07 3:22 
    GeneralDoes not understand decimals PinsussAnonymous3-Nov-00 4:55 
    QuestionBugs etc. - How does it deserve such high ratings? PinsussDean Wyant23-May-00 13:59 
    The author did not respond to my e-mail so I will place my comments here.
     
    The parser changes '.' to ',' instead of vice-versa.
    Spaces are not supported and the simple solution of removing them is not coded.
    Some apprently syntatically correct expressions do not parse properly due to other bugs.
    Parenthesis do not work correctly since the code blindly removes leading and trailing parens that might not match.
    So, the code fails to "parse and evaluate standard mathematical functions"
     
    Overall, the code is interesting but it is very limited in its usefulness. The code that parses the expression is difficult for me to understand and follow. Maybe I am just unaware of the logic or algorithm used? I find it hard to understand how this article that has no supporting text on usage, theory, approach, purpose, etc. and contains code with obvious bugs could rate such high marks from so many users?
     
    Have I misunderstood something? How is this code useful? I am not trying to be rude, mean or degrading. Please explain why I am wrong.
     
    Has someone actually fixed this so that it will parse and evaluate standard mathematical functions beyond single simple operations such as f(x) = x+2 to parse expressions such as f(x) = (x * 3.5)/(x * x)? I fixed several bugs, but then I gave up because I did not have confidence in the logic used for the parse function.
    QuestionEnglish translation of comments? PinsussPeter Helmers10-Apr-00 4:03 
    GeneralFunctionParser PinsussArmin Zürcher12-Mar-00 1:57 

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

    Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

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