You are on page 1of 1

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

For example, if you enter:

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:

| 0 | 0.2 | 0.4 | 0.6 | 0.8 | 1 | 1.2 | 1.4


| 0 | 0.76895 | 1.3779 | 1.7755 | 1.9613 | 1.9913 | 1.961 | 1.974

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.

You might also like