Page 1

Computer Programming (14 June, 2013) Final Exam (Time Duration: 9:00 a.m.~12:00 p.m.) ( Open Book, No Discussion, No Copy ) A polynomial of single variable (單變數多項式) contains many terms of a variable x . Each term has a coefficient and an order. For example, the term 2x3 has a coefficient of 2 and a degree of 3. Thus, we can define the data structure for a term like the following: class Term { public: Term(){} //Defult constructor Term(int o, double c):coeff(c), degree(o){}; //Constructor double coeff; //Coefficient int degree; //Degree }; With the class Term, please define a class for representing a polynomial using the list container of C++ (please do not implement it with other containers). The following definition is for your reference. class polynomial{ public: Polynomial(void){} ~Polynomial(void){} Polynomial(string filename); list <Term> terms; void InsertTerm(Term t); Polynomial & operator += (Polynomial & p); Polynomial & operator *= (Polynomial & p); friend ostream & operator << (ostream & os, Polynomial & p); void SortTerm(int order); }; In the above definition, a polynomial contains a list of Term objects, i.e. terms. For example, the polynomial x2 + 2x − 3 would have a list of three Term objects, including (2, 1), (1, 2), and (0, -3), where a pair (a, b) denotes a term b × xa . Now, you are required to inplement the member functions step by step such that the addition and multiplicantion of polynomials can be done by using the Polynomial class. Suggestion: I would like to suggest you solve the following tasks in sequence because some succeeding tasks would become very easy by using the funcitons designed for their preceding tasks. Task 1(20 pts): Insert a term into a polynomial. ( Tested Skill: List Insertion ) When constructing a polynomial, we often need to add terms to the plynomial one by one. Therefore, it would be helpfful if you can design a member function void InsertTerm (Term t); for the Polunomial class. The function insert a term t into the polynomial. When inserting the term, you have to make sure that the polynomial would not contain more than two terms which have the same degree after insertion (每個多項式中插入新項後不可 包含二個以上的同次項). If the polynomial already contains a term t0 which have the same degree of the term t before insertion, then add the coefficient of t to the coefficient of t0 (若原來已含有與插入項同次之項目,則以原項目之係數加上插入項後取代原項目係 數). Otherwise, just insert the new Term object t into the Term list termsof the

polynomial. Task 2(10 pts): Construct a polynomial by reading data from a text file. ( Tested Skill: File Stream ) Provided that a text file contains the coefficients and orders of terms in a polynomial, please implement the constuctor Polynomial (string filename); This comstructor receives the filename of the text file and then reads the data to construct a polynomial object. The format and content of the text file is like 0 2 2 3 3 4 ­1 which mean the stored polynomial is 2 + 3x2 + 4x3 . Note that -1 denote the end of the data. ( Hint: once you have successfully finished Task 1, it would be very easy to finish this task by using the designed InserTerm()funciton. ) Task 3(10pts): Print a polynomial ( Test Skill: << operator Overloading ) To show a polynomial on the screen, please design a friend function to overload the << operator friend ostream & operator << (ostream &os, Polynomial &p); for the Polynomial class. The following are example outputs for somepolynomials (0,3)(2,2)(3,-3) -> 3+2x^2­3x^3 (0,-4)(2,1)(1,-1)(3,-3) -> ­4+x^2­x­3x^3 (3,1)(2,0(1,-2)(3,4)(0,1) -> x^3­2x+4x^3+1 The following outputs are erroneous in format (underlined characters indicate the mistakes) +3+2x^2­3x^3 (correct version: 3+2x^2­3x^3) 3+x^1­2x^3 (correct version: 3+x­2x^3) 0+3x+­2x^2 (correct version: 3x­2x^2) +2x­0x^2 (correct version: 2x) Task 4(20pts): Sort the terms in a polynomial ( Tested Skill: Fuctor Design ) It is very often that we need to sort the terms of a polynomial into some specific order. Hence, please implement the member function void SortTerms(int o); for the Polynomial class. The parameter ocould be any number in {0, 1, 2, 3} to sprcify different sorting orders. If ois 0, then the terms are sorted according to the ascending (遞增) order of their degrees (次方數). If o is 1, then the terms are sorted accorfing to the descending (遞減) order of their degrees. If ois 2, then the terms are sorted according to the ascending order of the magnitude of coefficients, i.e. |coeff | (i.e., the absolute value of coefficient, 係數絕對值). When more than two terms have the same coefficient magnitudes. In ois 3, then please sort the terms according to the descending order of the coefficient magnitudes. In contrast, when . more than two terms have the same coefficient magnitudes, place those terms with higher degrees first (高次項在前). To make it easy to define different sorting orders, you are required to define a functor class, say Comparator class. class Comparator{ public:

int order; Comparator():order(0){} Comparator(int o){order = p;} bool operator () (Term a, Term b){ if(order==0) ...; // do comparison and return Boolean here else if(order==1) ...; // do comparison and return Boolean here else if(order==2) ...; // do comparison and return Boolean here else ...; // do comparison and return Boolean here } }; With the Comparator class, you can easily sort the term list of a polynomial by passing a properly constructed Comparator object as a parameter to the sort() function of the C++ list container. Then the sort() function will do the sorting for you. Task 5(20 pts): Overload the += operator (Tested Skill: Overloading += operator) To do the polynomial addition with simpler syntax, you are required to overload the += operator of the Polynomial class. With the +=operator, we can simply write a statement like p1 += p2 ; to add two polunomial p1and p2.Then, the polynomial p2 is added to the polynomial p1 and the resultant polynomial is store in p1. (Hint: you can easily finish this task by using the InsertTerm()of Task1. ) Task 6(20 pts): Overload the *= operator (Tested Skill: Overloading *= operator) Similarly, we can also overload the *=operator for doing the polynomial multiplication. For example, the following statement p1 *= p2; would store the multiplication of two polynomials, p1and p2, to p1. Here is a hint for you Overloading the *=operator can be easily done by using the +=operator and InsertTerm(). ( Hint: again, you can easily finish this task by using the InsertTerm() of Task 1. ) To verify the correctness of your design in responding to all task requirments, the following code would be give in the main() function. int main(){ Polynomial p1(“data1.txt”); // construct the first polynomial Polynomial p2(“data2.txt”); // construct the second polynomial cout << “Polynomial 1 = ” << p1; // print the first polynomial cout << “Polynomial 2 = ” << p2; // print the second polynomial p1 += p2; // add two polynomial int k = 3; for(int o=0;o<4;o++){ // print the result polynomial in four different orders p1.SortTerm(o); cout << “Polynomial ” << k << “ (order = ” << o << “) = “ << p1; k++;

} Term t(0,1); // construct a term 1x^0 ­> 1 Polynomial p3; p3.InsertTerm(t); // initialize the polynomial with 1 p3 *= p1; // Note: p1 aleady has the previous polynomial addition result p3 *= p2; // multiply the above resultant polynomial with the second polynomial k = 7; for(int o=0;o<4;o++){ // print the result polynomial in four different orders p3.Sort(o); cout << “Polynomial ” << k << “ (order = ” << o << “) = ” << p3; k++; } return 0; }

Remember that no line in the above main() function can be modified in your program ( 你的程式中不得修改上面 main() 函數中任何一行指令 ). If you cannot produce correct outputs for the given main() function, that would be meaning that somewhere in your designis wrong. Example Content of Data1.txt ( − 1 − 2x + x2 ) 0 ­1 1 ­2 2 1 ­1 Example Content of Data2.txt( 2 − 2x2 + x3 ) 0 2 2 ­2 3 1 ­1 Example Outputs of main(): Polynomial 1 = ­1­2x+x^2 Polynomial 2 = 2­2x^2+x^3 Polynomial 3 (order = 0) = 1­2x­x^2+x^3 Polynomial 4 (order = 1) = x^3­x^2­2x+1 Polynomial 5 (order = 2) = 1­x^2+x^3­2x Polynomial 6 (order = 3) = ­2x+x^3­x^2+1 Polynomial 7 (order = 0) = 2­4x­4x^2+7x^3­3x^5+x^6 Polynomial 8 (order = 1) = x^6­3x^5+7x^3­4x^2­4x+2 Polynomial 9 (order = 2) = x^6+2­3x^5­4x­4x^2+7x^3 Polynomial 10 (order = 3) = 7x^3­4x^2­4x­3x^5+2+x^6 Some Required Header Files #include <iostream> #include <list> #include <fstream> #include <vector> #include <string>

Note: The above contents for data1.txt and data2.txt are just for your own testing. The teacher will test your program with different contents in data1.txt and data2.txt.