13890 - Polynomial Calculator   

Description

Warning: In Funciton::parse, the use of atoi for parsing constants makes the calculation different from what you'd get with a normal calculator, since atoi always returns an integer. Therefore, the testcases will be different than what you expect. Sorry for the confusion.

You will be given a preordered polynomial f(x). There will be Q queries, for each query x, please calculate the answer of f(x).

This is a partial judge problem, we have already implement a class Function to you. class Function has two functions:

  • static Funtion *parse(stringstream &ss):
    used to parse the input, and it's already implemented.
  • double eval(double x):
    used to calculate f(x), and this needs all its derived class to override it.

You need to implement the following 6 class inheritted from Function:

  • Constant: represent a constant value, ex. 1, 5.3 etc.
    • static Constant *create(double x):
      read a double x as the constant
  • Variable: represent variable, in this problem, the only variable is x.
    • static Variable *create(string s):
      read a string s as the variable
  • Polynomial: represent polynomial(xn), ex. 53, x7, 9x.
    • static Polynomial *create(Function *a, Function *b):
      read Function *a and Function *b as the base and exponential of the polynomial, which represent ab
  • Arithmetic: represent a arithmetic formula, ex. 5 + 3, 7 * x, x / 2.
    • static Arithmetic *create(Function *a, char op, Function *b):
      read Function *a, char op, and Function *b, the char op contains '+', '-', '*', '/'
  • Sin: represent a sine function sin(f(x)).
    • static Sin *create(Function *a):
      read Function *a, represents sin(a)
  • Cos: represent a cosine function cos(f(x)).
    • static Cos *create(Function *a):
      read Function *a, represents cos(a)

 

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;
    }
}

Input

The first line contains a preordered polynomial f(x):

  • Constants and Variable: constant value or variable x, ex. x, 5.3, 7
  • Polynomial operator: ** a b, represent ab
  • Arithmetic operator: op a b, represent a op b, ex. + 5 x means 5 + x. op constains only + - * /
  • Trignomotric operator: op a, represent op(a), ex. sin x means sin(x). op contains only sin and cos

The second line contains an integer Q, means the number of queries.

The following Q lines are the queries, each line contains a number 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

Output

For each x, please output the answer of f(x).

Sample Input  Download

Sample Output  Download

Partial Judge Code

13890.cpp

Partial Judge Header

13890.h

Tags




Discuss