# How do I allow a user to input a function?

• 05-17-2005
bananasinpajama
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.
• 05-17-2005
xErath
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.
• 05-18-2005
Salem
Depends on how complicated you want to make the function.
A straight-forward polynomial like that with a single variable should be pretty easy.
• 05-18-2005
bananasinpajama
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.

If you could elaborate on how to do it so easily that would really help. :(
• 05-18-2005
DougDbug
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.
• 05-18-2005
7stud
Quote:

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; }```
• 05-18-2005
xErath
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
• 05-19-2005
xErath
probably this may be interesting
http://www.bloodshed.net/c/eval_expr.tar.gz
• 05-19-2005
Salem
> "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 );`
• 05-19-2005
joshdick
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.