Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

User defined functions in wpf

  1. Sep 4, 2014 #1
    i would like to have the user enter a math function. ie cos(x)-e^x + x^2
    it will be typed into a text box and then the program performs a numerical procedure on the function (ie integration)

    In other words, how do i transform the input string into a method?
     
  2. jcsd
  3. Sep 4, 2014 #2

    Mark44

    Staff: Mentor

    Not very easily.
    Your code will need to parse the input string, looking for terms (expressions that are added together), operators such as - for negation and ^ for exponents, function keywords such as cos, sin, tan, ln, and so on.
     
  4. Sep 4, 2014 #3
    hmm, i was thinking it would be complicated. i suppose if i used a keypad (like on a calculator) it would be easier right?
     
  5. Sep 4, 2014 #4

    Mark44

    Staff: Mentor

    ???
    All you can get from the keypad are the numeric digits and the arithmetic operators (+, -, *, and /). You still have to parse the input, building numbers out of the input digits, and figuring out which things are added, subtracted, and so on.
     
  6. Sep 4, 2014 #5

    berkeman

    User Avatar

    Staff: Mentor

    Have you taken a compiler class yet? That's basically the technique you would use to parse the input and turn it into executable code...
     
  7. Sep 4, 2014 #6

    AlephZero

    User Avatar
    Science Advisor
    Homework Helper

    You don't need a lot of "compiler theory" to do this. You could build an interpreter to evaluate the expressions quite simply with lexical analysis and parsing tools like lex and yacc (or bison, or whatever the unix community calls it these days). You can probably find something similar as a lex-and-yacc tutorial on the web.

    It won't be very efficient to re-interpret the user input every time you want to evaluate the function, but something that works slowly is a better starting point than nothing at all.
     
  8. Sep 4, 2014 #7

    FactChecker

    User Avatar
    Science Advisor
    Gold Member

    It depends on which language you are using. There is an easy way and a hard way and a hybrid way.

    1) Easy: Some scripting languages can evaluate a string as a line of code even when it is running. Look for languages that can "evaluate" a text string as though it was code in that language. Look for an eval() function. Perl and Python are two such languages.

    2) Hard: Otherwise, you will have to parse the input string and perform math operations in code that you write. As AlephZero says, lex and yacc can help you do that. You will need to define a language that can be parsed.

    3) Hybrid: Languages like Python can be called from C programs. You can let the Python do the evaluation part and return results to C to do the rest. I don't have any real experience with this.
     
    Last edited: Sep 4, 2014
  9. Sep 6, 2014 #8
    I mean a built in keyboard where I have buttons for each function built into the ui.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook