Professional Documents
Culture Documents
22 - Evaluation of A Mathematical Function
22 - Evaluation of A Mathematical Function
The goal of this C++ exercise is to implement an algorithm which will be able to compute the values of a
mathematical function f(x) defined as an infix notation expression inside a std::string. In order to test your
algorithm you will have to enter the expression of this function with the keyboard by using std::cin and you
will display the result of the calculations for several sampled values of x. The bounds and the sampling of
these x values will also be entered using the keyboard.
Notes:
• f(x) will not contain any spaces inside its formula (can be read directly with std::cin and stored
inside a std::string).
• Parenthesis can be used.
• Use the operator precedence presented in “04 - C++ - Expression Parsing”.
• f(x) can contain:
o float values
o the operators: +, -, * and /
o the sin, cos and sqrt mathematical functions
cos(x)*sin(2*x)+3*x*sqrt(.5/(x+1))
0
1.5
0.2
Where you will first enter the formula of the function f(x) as a std::string, then the start value as float (0
here), the end value as float (1.5 here) and the step value as float (0.2 here), you will display this:
For this, you will have first to transform this string (which use an infix notation) to a post or a prefix notation
expression stored as a stack (or as a linked list) containing std::string as values. When you will use this data
structure in order computer the value of f(x) for a given x.