Page 1

Computer Programming (21 June, 2013) Final Exam (Make-up) Time Duration: 9:00 a.m.~12:00 p.m. (Open Book, No Discussion, No Copy) Design a class, say MyPoly, to inherit the Polynomial class designed in our final exam last week to have the following member functions and overloaded operator: (1) MyPoly Deriv(); (30 points) This member function returns the 1st-order derivative of the polynomial. For example, if the polynomial is x2+2x+1, then the returned polynomial is 2x+2. int main() { MyPoly p1("data1.txt"); cout << "Polynomial 1 = " << p1; cout << "Df(x) = " << p1.Deriv(); return 0; } Output: Polynomial 1 = -1-2x+x^2 Df(x) = -2+2x

(2) MyPoly Int(); (30 points) This member function returns the indefinite integral function of the polynomial, with the constant term ignored. For example, if the polynomial is x2+2x+1, the indefinite integral function should be 0.333333x3+x2+x+C. After ignoring the constant term C, we get the returned polynomial 0.333333x3+x2+x. int main() { MyPoly p1("data1.txt"); cout << "Polynomial 1 = " << p1; cout << "If(x) = " << p1.Int(); return 0; } Output: Polynomial 1 = 1+2x+x^2 If(x) = x+x^2+0.333333x^3

(3) double operator()(double val); (30 points) This overloaded operator is to evaluate the polynomial by substituting the polynomial variable x with the double val. For example, if the polynomial p is x2+2x+1, then p(1.0) = 4. int main() { MyPoly p1("data1.txt"); cout << "Polynomial 1 = " << p1; cout << "f(1.0) = " << p1(1.0) << endl;


cout << "Df(1.0) = " << p1.Deriv()(1.0) << endl; cout << "If(1.0) = " << p1.Int()(1.0) << endl; return 0; } Output: Polynomial 1 = 1+2x+x^2 f(1.0) = 4 Df(1.0) = 4 If(1.0) = 2.33333

(4) double DInt(double a, double b) (25 points) This member function computes the definite integral

1

1

( x 2 +2x+1)dx  x3 / 3  x 2  x

1 1

b

a

p( x)dx . For example,

 1/ 3  1  1  (1/ 3  1  1)  2.66667 .

int main() { MyPoly p1("data1.txt"); cout << "Polynomial 1 = " << p1; cout << "DIf(-1.0, 1.0) = " << p1.DInt(-1.0, 1.0) << endl; return 0; } Output: Polynomial 1 = 1+2x+x^2 DIf(-1.0, 1.0) = 2.66667

Content in data1.txt 0 1 1 2 2 1 -1 Note: The above contents for data1.txt is just for your own testing. The teacher will test your program with different contents in data1.txt.

13719111591338913770_  
Advertisement