$24
In this assignment, you need to complete a derivative calculator independently.
Submissions before the deadline xx:xx:xx are treated as normal submissions.
Function Requirement
In test A, each math expression includes “+”, “-”, “*”, “^”(see the example below)
1. x only appears once in each term of a polynomial
2. x will be the only variable, there will be no more than variables like y, z appear.
3. x will not be the power term
4. All constant numbers are integer, with no float (doubles).
5. The answer should be compact, i.e. no space within bits.
6. There will be no “(“ and “)” (brackets)
7. Space can appear in test A
8. In test A, all expressions are valid.
9. Tab will not be tested in oj.
10. Neglect “^1” when the answer power is 1. Example 3: 35*x^6+16*x-6 However, “x^1” can appear in test.
An example of an input file for test A
Expected output file:
x
1
x^2
2*x
5 * x ^ 7+ 8 * x ^ 2 - 6 *x^1
35*x^6+16*x-6
In test B, all requirements in test A should be satisfied, plus:
1. (no more than 2) functions including “sin” (sine function), “cos” (cosine function), the function must use a bracket to include variable x both in input and output
Assignment 1 Derivative calculator 1
2. sin and cos are compact without space splited (i.e. no “s i n (x) ”
situation) .
3. “(“ and “)” (brackets);
4. In test B, the expression may not be valid. When the answer is invalid, return string “invalid”
5. Please don’t use any product-to-sum formula or sum-to-product formula.
6. When there is no need to use the bracket, don’t use it, save it (example 2: -sin(sin(45*x))*cos(45*x) ).
7. The chain rule should follow expanding from the outside to the inside
8. “++”, “+-”, “- -” not allowed
9. “*-constant” is viewed as the valid answer, when you take derivative f(−w ⋅ x + b) in chain rule.
The answer is f′ ∗ −w.
10. “+(-”, “-(-” is no need to simplify. Example 3: cos(x)-(-sin(4*x)), more example cos(cos(x))→-sin(cos(x))*(-sin(x))
11. Invalidation occurs only if square brackets do not appear in pairs. Other invalid will not be tested in oj.
An example of an input file for test B
Expected output file:
1+ 2.0 * sin(x)
2*cos(x)
cos( sin (45 * x) )
-sin(sin(45*x))*cos(45*x)
sin(x)-cos(4*x)
cos(x)-(-sin(4*x))
sin(35-cos(4*x)) + cos(cos(x))
cos(35-cos(4*x))*(-sin(4*x))*4-sin(cos(x))*(-sin(x))
5*((3x+4)+8
invalid
sin(x)-cos(4*x)
cos(x)-1*(-sin(4*x))*4
Assignment 1 Derivative calculator 2