x = +2
2 * + 7
1 / (+1 - 2)
Factor is SimpleFactor ^ Factor // a ^ b (a to the power of b) or SimpleFactor SimpleFactor is ( Expression ) // parenthesized expression or Number // literal floating point number or Identifier ( Expression )// function call or Identifier// symbolic variable or - Factor // unary minus
void AddNode::Print (int indent) const
{
_pLeft->Print (indent + 2);
Indent (indent);
cout << "+" << endl;
_pRight->Print (indent + 2);
cout << endl;
}
where Indent prints as many spaces as is the value of its argument.
sin(x) -> cos(x)
cos(x) -> -sin(x)
exp(x) -> exp(x)
log(x) -> 1/x
sqrt(x) -> 1/(2 * sqrt(x))
The derivative of a sum is a sum of derivatives, the derivative a product is given by the formula
(f(x) * g(x))’ = f’(x) * g(x) + f(x) * g’(x)
where prime denotes a derivative. The derivative of a quotient is given by the formula
(f(x) / g(x))’ = (f(x) * g’(x) - f’(x) * g(x)) / (g(x) * g(x))
and the derivative of the superposition of functions is given by
(f(g(x))’ = g’(x) * f’(g(x)).
Rewrite the calculator to derive the symbolic derivative of the input by transforming the parse tree according to the formulas above. Make sure no memory is leaked in the process (that is, you must delete everything you allocate).