How would I allow the user to input an equation

May 14, 2014 at 3:33am
I've written a program that finds the max of a function. I now want to get rid of the equation y=x^2-7x-18 and replace it by y=fx, fx being an equation that the user inputs. However, I'm not sure of how to go about doing so. What is the best data type to declare the user-input equation as? And will I need to do anything special with the user-input equation inside of the for loop, or can I just replacen y = pow(x, 2)-7*x-18 with y = fx?
Here's my current code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
  #include <iostream>
#include <cmath>
#include <cstdlib>

using namespace std;

int main()
{
    int a, b, delta, x, y;
    double max= -1.8 * pow(10, 308);
 
  
    cout <<"Please enter the first number of the interval to be checked: " << endl;
    cin >> a;
    cout << "Please enter the last number of the interval to be checked: " << endl;
    cin >> b;
    
    delta= 1;
      for(x = a; x <= b; x = x+delta) 
                                   
    {
        y = pow(x, 2)-7*x-18;     
        if (y > max)  
        { 
            max = y;  
         
        }
        else
        {
            delta= delta/2;
        }
        if (delta <  pow( 10, -6))
        {
            break;
        }
    }      
    
   cout <<"The maximum over the interval from " << a <<" to " << b <<" is " << max;
   
    return 0;
}
May 14, 2014 at 8:14am
What is the best data type to declare the user-input equation as?
Best data type to get an equation from user is string.
Best data type to represent and actually calculate equation is AST: abstract syntax tree.
May 14, 2014 at 8:17am
This is a rather complex. You need to to parse the input of the user and identify the components and precedence.
May 14, 2014 at 5:17pm
are you sure you've written this sourcecode by yourself? it looks like you have some kind of assignment, and you take others sourcecode and post it here. So we edit and write it for you
Topic archived. No new replies allowed.