Thread: How do I allow a user to input a function?

  1. #1
    Registered User
    Join Date
    May 2005
    Posts
    2

    How do I allow a user to input a function?

    I want to allow a user to input a function like "5x^4-3x^2+12x-6" and I want to be able to use that function, i.e. input values and things like that.

    Right now I have it set up piece wise, as in enter the first coefficient etc. and I can get the function to display but I don't know how to get it to work as a function.

  2. #2
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    very hard working that stuff
    you'll need first to read the entire function then you'll need to properly store the parsed function in a data structure: my sugestion for that DS is here.

    http://www.brpreiss.com/books/opus4/html/page262.html

    To build the tree first add all the operators to the tree, then add the numbers from left to right when a empty leaf is found. evaluate each node upwards.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Depends on how complicated you want to make the function.
    A straight-forward polynomial like that with a single variable should be pretty easy.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    May 2005
    Posts
    2
    All I want to do are simple polynomials at this point. I'm working on the other suggestion but it's pretty complicated and I think I'd have to recode everything.

    Thanks for your help.

    If you could elaborate on how to do it so easily that would really help.

  5. #5
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    Right. If the input is always in a particular format this shouldn't be too hard. But, it gets tricky if the user is allowed to use parentheses to change the order/precedence of the operations, etc.

    The key is parsing the string. This means stepping-through the characters in the string, and determining if the character is a number (or part of a number*), an operator like " ^ " or " + " , or a variable " x ", etc.

    Once you've "extracted" a coefficient or an exponent, you (your program) can perform the appropriate mathematical operation.

    *You'll need to allow for a multi-digit coefficient (i.e. 12 in your sample). You might want to get the program working with single digit numbers first, then add multi-digit capability when everything else works.
    Last edited by DougDbug; 05-18-2005 at 12:55 PM.

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Right now I have it set up piece wise, as in enter the first coefficient etc. and I can get the function to display but I don't know how to get it to work as a function.
    The short answer is you can't. Your input is a string which is either of type const char[] or type string, and neither of those types is consistent with the type of a function. A function has a type that reflects its return type and parameter types, e.g.:

    int (*)(double, string)

    compare that to type const char[] or type string. The bottom line is you can't execute a string. This doesn't work:
    Code:
    #include <string>
    #include <iostream>
    
    using namespace std;
    
    void greeting()
    {
         cout<<"hello world";
    }
    
    int main()
    {
         string str = "greeting";
         str();  //error--"term does not evaluate to a function"
         
         return 0;
    }
    Last edited by 7stud; 05-18-2005 at 05:28 PM.

  7. #7
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    Quote Originally Posted by 7stud
    The short answer is you can't. Your input is a string which is either of type const char[] or type string, and neither of those types is consistent with the type of a function. A function has a type that reflects its return type and parameter types, e.g.:

    int (*)(double, string)
    stud he meant a mathematical expression or a math function.. not a C function...
    f(x) = x^2

  8. #8
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    probably this may be interesting
    http://www.bloodshed.net/c/eval_expr.tar.gz

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > "5x^4-3x^2+12x-6"
    Step 1 is write a function which tokenises the string to

    5x^4
    -3x^2
    +12x
    -6

    x by itself is x^1
    the constant is -6x^0

    Then split each one of those up into two values, and store them in say
    Code:
    struct foo {
      int scale;
      int power;
    } terms[10];
    From which you get, as you parse each term
    Code:
    term[0].scale = 5;
    term[0].power = 4;
    term[1].scale = -3;
    term[1].power = 2;
    term[2].scale = 12;
    term[2].power = 1;
    term[3].scale = -6;
    term[3].power = 0;
    Then it's simply a matter of inputting a value for x, and looping over those terms to do
    Code:
    sum += term[i].scale * pow( x, term[i].power );
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    7stud's not wrong when it comes to standard data types, but if you wanted, you could create a class and overload the () operator to allow for something similar to f(x) notation in code.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  5. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM