Confused
2011-01-15 13:48:39 UTC
Input file: file name obtained from the command line
Output: file name obtained from the command line
The derivative of an expression that involves the variable x can be defined by a few rules:
• The derivative of a constant is 0.
• The derivative of x is 1.
• If A is an expression, let dA be the derivative of A. Then the derivative of –A is –dA.
• If A and B are expressions, let dA and dB be the derivatives of A and B respectively.
Then:
o The derivative of A+B is dA+dB.
o The derivative of A - B is dA - dB.
o The derivative of A * B is A * dB + B * dA.
o The derivative of A/B is (B*dA - A*dB) / (B*B).
For this problem, you should write a program that can compute the derivative of an expression.
All the information that you need is in the rules given above. Note that the resulting derivatives
can be more complicated than need be. For example, the derivative of 3x+1 will be computed
as (3 *1+0*x) +0. This is correct, even though it is kind of ugly, but after simplification it should
be only 3.
Input Format
The input file starts with a number M describing how many cases are to be examined.
Each of the next M lines then gives an expression with only one variable, x, to be derived. Note
that ^ means power. Also note that there is a space between characters except before or after
the power sign (i.e. ^). Assume that the input has no parentheses.
Output Format
The output should be M lines of simplified derivatives of the given cases
Sample Input: derive.in
5
3 x + 1
1 / x
3 x^3 / x
5 x^2 – 6 x + 7
x / 3
Sample Output: derive.out
3
-1 / x^2
6 x
10 x – 6
1 / 3