Help allowing for user input of equation to be evaluated

I've written a program that finds the maximum of a function. I now want to change it a little so that instead of evaluating the function y=x^2-7x-18, the program will ask the user to input an equation and then evaluate that equation. I'm really not sure of how to do so. Can anyone help me out?
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;
}
Alas, that's a lot more work than you think.

You can do it, though. Google around "recursive descent parser" to learn about how to compute any expression.

Next you need to add an "environment" (use a std::map) to keep track of variables while computing a value.

After that, you've got the basis for playing around with your own little interpreter. You should be able to come in under 100 lines or so.

Have fun!
Equation is a tree structure. Say y=a*x+b
    +
   / \
  *   b
 / \
a   x

Subtrees must evaluate first. Here multiplication would evaluate before addition.
The common letter for 'a' is 'm' ;o)

But seriously, recursive descent (and other methods) does that for you.
Topic archived. No new replies allowed.