Taking mathematical function as input

I am trying to write a numerical integration program. My program should obtain a mathematical function in c++ syntax via cin, and then use it in my c++ integration function. For example: I enter the function x*x+3*x with variable x into the console as a string, and my program uses this as the function to integrate. Thanks for any replies
You can "compile" such an expression to a function by creating a syntax tree from the expression and treating this as a functor, along the lines of:

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
class Functor {
     public:
    virtual double operator()(double) = 0;
};

class Plus : public Functor{
      private:
      Functor* left;
      Functor* right;
      public:
      Plus(Functor* left, Functor* right) : left(left), right(right) {}
      virtual double operator() (double x) {
                 return *left(x) + *right(x);
      }
};

//other operators...

class Constant : public Functor {
        private:
        double value;
        public:
        Constant(double value) : value(value) {}
        virtual double operator() (double) {
                 return value;
        }
};

class Variable : public Functor {
           public:
           virtual double operator() (double x) {
                     return x;
           }
};


That's of course neither the only nor necessarily the best way to do it, but it works. How to get your expression into this tree form is left as an exercise to you, though it would probably be easiest to convert to polish notation first.
Last edited on
on your initializer list, does the class' left being hidden by the constructor's [code]left[code], and so the other?
Nope. In an initializer list the language allows identifiers with the same name because they can be distinguished.
var(/*something*/) is always an instance variable, and /*something*/(var) is always an argument. Note that this only applies if there is a constructor argument AND a instance variable of the same name, I'm not sure about the behavior of the compiler if only one of them is there.
Last edited on
I'm not sure about the behavior of the compiler if only one of them is there.


you mean, like:

1
2
3
4
int whatever_function (int x) {
   int x;
   cout << "the value of x: " << x;
}
No, in that case the value of x is undefined (that is, it will be the local x which is not initialized).

What I'm not sure about is whether

1
2
3
4
5
6
class A {
private:
int x; int y;
public:
A(int n) : x(n), y(x) {}
};


is legal. I assume it is, but I haven't tried it (and I would really recommend it anyways).
Thank you hanst99 , I appreciate that you took the time to help me, but I honestly don't understand a thing you wrote :(. I just started into classes and I don't know anything about functors. Does anyone know a simpler/different way? thanks
That's the simple way. At least I don't think this could be done much simpler than this.

Functor in this case has nothing to do with the mathematical concept, it just means "object that provides the () operator", so you can write myFunctor(*args*) instead of myFunctor.doSomething(*args*).

No, in that case the value of x is undefined (that is, it will be the local x which is not initialized).


oh yeah, sorry... i mean like this:

1
2
3
4
5
int whatever_function (int x) {
   int x;
   x = 9; //for example...
   cout << "the value of x: " << x;
}


like that, right?
hmm... any confirmation?
hello?
It's not really part of the topic, and I thought it was so obvious I didn't bother answering.
Topic archived. No new replies allowed.