Description
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)
-
x only appears once in each term of a polynomial
-
x will be the only variable, there will be no more than variables like y, z appear.
-
x will not be the power term
-
All constant numbers are integer, with no float (doubles).
-
The answer should be compact, i.e. no space within bits.
-
There will be no “(“ and “)” (brackets)
-
Space can appear in test A
-
In test A, all expressions are valid.
-
Tab will not be tested in oj.
-
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:
-
(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
2. sin and cos are compact without space splited (i.e. no “s i n (x) ”
situation) .
-
“(“ and “)” (brackets);
-
In test B, the expression may not be valid. When the answer is invalid, return string “invalid”
-
Please don’t use any product-to-sum formula or sum-to-product formula.
-
When there is no need to use the bracket, don’t use it, save it (example 2: -sin(sin(45*x))*cos(45*x) ).
-
The chain rule should follow expanding from the outside to the inside
-
“++”, “+-”, “- -” not allowed
-
“*-constant” is viewed as the valid answer, when you take derivative f(−w ⋅ x + b) in chain rule.
The answer is f′ ∗ −w.
-
“+(-”, “-(-” is no need to simplify. Example 3: cos(x)-(-sin(4*x)), more example cos(cos(x))→–sin(cos(x))*(-sin(x))
-
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