Question
You can use function pointers to maintain a map of mathematical functions by name. For example, if you start with the declarations you can then
You can use function pointers to maintain a map of mathematical functions by name. For example, if you start with the declarations
you can then store functions under their conventional names simply by adding entries to function Table. For example, the following lines add the sin and cos functions from the library:
Use this technique to add the standard mathematical functions to the expression interpreter from Chapter 19. This change requires you to make several extensions to the existing framework, as follows:
The interpreter must use real numbers rather than integers in its computation. You had the opportunity to make this change in exercise 8 from Chapter 19.
The function table needs to be integrated into the Evaluation Context class so that the interpreter has access to the functions by name.
The parser module needs to include a new grammatical rule for expressions that represents a function call with a single argument.
The eval method for the new function class must look up the function name in the function table and then apply that function to the result of evaluating the argument. Your implementation should allow functions to be combined and nested just as in C++. For example, if your interpreter defines the functions sqrt, sin, and cos, your program should be able to produce the following sample run:
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started