Click here to Skip to main content
15,867,756 members
Articles / Programming Languages / C++
Article

A powerful function parser

Rate me:
Please Sign up or sign in to vote.
4.40/5 (21 votes)
22 Feb 2000 140.7K   2.2K   60   23
A simple yet powerful function parser that parses and evaluates standard mathematical functions
  • 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


    Written By
    Web Developer
    Germany Germany
    This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

    Comments and Discussions

     
    GeneralMFC compile Pin
    6-Jul-02 9:28
    suss6-Jul-02 9:28 
    GeneralRe: MFC compile Pin
    8-Jul-02 6:56
    suss8-Jul-02 6:56 
    GeneralA version without MFC / MS dependencies Pin
    14-Feb-02 20:23
    suss14-Feb-02 20:23 
    GeneralRe: A version without MFC / MS dependencies Pin
    19-Feb-02 1:50
    suss19-Feb-02 1:50 
    GeneralRe: A version without MFC / MS dependencies Pin
    Jochen Kalmbach [MVP VC++]29-Jan-04 19:31
    Jochen Kalmbach [MVP VC++]29-Jan-04 19:31 
    GeneralRe: A version without MFC / MS dependencies Pin
    Philippe Lhoste4-Dec-04 2:25
    Philippe Lhoste4-Dec-04 2:25 
    GeneralRe: A version without MFC / MS dependencies Pin
    Jochen Kalmbach [MVP VC++]1-Jan-06 19:58
    Jochen Kalmbach [MVP VC++]1-Jan-06 19:58 
    GeneralRe: A version without MFC / MS dependencies Pin
    supermcjt25-Jul-13 13:18
    supermcjt25-Jul-13 13:18 
    Generalmore General expression Pin
    16-Jan-02 1:56
    suss16-Jan-02 1:56 
    GeneralWorld peace Pin
    Bartosz Bien14-Dec-01 13:14
    Bartosz Bien14-Dec-01 13:14 
    GeneralTo B.Starks, W.Dean, & J.Andreas Pin
    Amit Gefen8-Nov-01 12:43
    Amit Gefen8-Nov-01 12:43 
    GeneralOperators/precedence/types etc Pin
    Andrew Phillips26-Sep-01 18:04
    Andrew Phillips26-Sep-01 18:04 
    GeneralVery good success with this Pin
    Jeff12-May-01 8:59
    Jeff12-May-01 8:59 
    GeneralYou must read jeff's reply..!! then you will success Pin
    JsSong25-Mar-02 14:47
    JsSong25-Mar-02 14:47 
    GeneralBugs etc below should be renamed to Dean Wyant knows little about programming. Pin
    13-Apr-01 7:36
    suss13-Apr-01 7:36 
    To Dean Wyant,

    I nearly didn't bother trying FunctionParser due to your scathing review. However, I'm glad I recognized that Dean simply doesn't know very much and talks too much. I went ahead and tried FunctionParser and found it to be good.

    The issue with the '.' changed to ',' as almost all experienced programmers know, is due to Locale settings (the code was written in Germany, you are in the U.S.). Simply comment out the line that replaces the '.' with ','.

    Next the issue you have with it not being able to parse (x*3.5) / (x*x) is due to the fact that the author wrote a quick and dirty using std::cin which everyone knows will stall on the ' ' in front of the '/'. This isn't a problem if you simply don't use std::cin for input from the user. You can't blame the author for this either because he was simply trying to provide a demonstration app quickly, however he could clear this up by not using cin.

    I find the Parser to be a good one and thank the author for sharing it, and gladly accept any problems it may have. To Dean Wyant I suggest that you should evaluate your own programming abilities before railing on someone for providing great code that is simply above your abilities to understand.

    Sincerely,

    B. Starks
    b_starks@hotmail.com
    GeneralRe: Bugs etc below should be renamed to Dean Wyant knows little about programming. Pin
    Dean Wyant30-Apr-04 10:11
    Dean Wyant30-Apr-04 10:11 
    GeneralA good Parser Pin
    18-Jan-01 17:57
    suss18-Jan-01 17:57 
    GeneralRe: A good Parser Pin
    Ping He9-May-01 23:36
    Ping He9-May-01 23:36 
    GeneralRe: A good Parser Pin
    MathePeter3-Jul-07 3:22
    MathePeter3-Jul-07 3:22 
    GeneralDoes not understand decimals Pin
    Anonymous3-Nov-00 4:55
    Anonymous3-Nov-00 4:55 
    QuestionBugs etc. - How does it deserve such high ratings? Pin
    Dean Wyant23-May-00 13:59
    Dean Wyant23-May-00 13:59 
    QuestionEnglish translation of comments? Pin
    Peter Helmers10-Apr-00 4:03
    sussPeter Helmers10-Apr-00 4:03 
    GeneralFunctionParser Pin
    Armin Zürcher12-Mar-00 1:57
    sussArmin Zürcher12-Mar-00 1:57 

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

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