Most of the problem are as same as Polynomial Calculator (https://acm.cs.nthu.edu.tw/problem/13890/), the only difference is that you think if a calculator can only calculate f(x) is too weak, so you decide to add a function: differential.
There is one more function in class Function:
Function *differential(): this is used to return the differential function, and it needs all its derived class to override it.
Differential Formula:
Please paste the following code after the code you write, and don't forget to include "function.h"!
Function* Function::parse(stringstream &ss){
string s;
ss >> s;
if(s == "+" || s == "-" || s == "*" || s == "/"){
Function *a = parse(ss), *b = parse(ss);
Function *now = Arithmetic::create(a, s[0], b);
return now;
}else if(s[0] == 'x'){
Function *now = Variable::create(s);
return now;
}else if(s == "**"){
Function *a = parse(ss), *b = parse(ss);
Function *now = Polynomial::create(a, b);
return now;
}else if(s == "sin"){
Function *a = parse(ss);
Function *now = Sin::create(a);
return now;
}else if(s == "cos"){
Function *a = parse(ss);
Function *now = Cos::create(a);
return now;
}else{
Function *now = Constant::create(atoi(s.c_str()));
return now;
}
}
The first line contains a preordered polynomial f(x):
The second line contains an integer Q, means the number of queries.
The following Q lines are the queries, each line contains an integer x.
The length of the first line won't exceed 1e6, and 1 <= Q <= 2e5.
Each constant and query is between [-100, 100]. It's guarantee that no overflow and divided by 0
For each x, please output the answer of f(x) and f'(x).