I need to make function in C++
to calculate integrals. I am using the simpsone rule
to calculate value of the given integral. I know how to calculate that. I don't have any problem with math. I need to know how can I pass whole expression to make my program flexible.
I have 4 f(x)
functions for which I should make calculations. For example:
f(x)=2e^x
f(x)=x^3e
etc.
I have two options to make it.
1)I can do separate function for each f(x)
function.
double function1() {
...
calculations 2e^x
...
return resault;
}
double function2() {
...
calculations x^3e
...
return resault;
}
This way is easy and fast to write, but the code is not flexible at all. In this case I need to make new function for every new given f(x) function.
I would like to have one function to which I can pass selected f(x)
function.
2) Second case I prefer is to make some kind of interpreter of expressions. I thought about putting the parts of expression into std::vector
and then making calculations for each cell of vector
.
I've seen already an idea to parse string to the expression, but I think at the end it will be almost the same as idea with vector
. I can be wrong.
What is the best way to make my code flexible and easy to use for users(not programmers)?