Unformatted text preview:

Polynomial Program HandoutPolynomial Program Handoutclass Poly { public:Poly(int *p, int terms); // constructorPoly operator + (Poly q); // poly additionPoly operator - (Poly q); // poly subtractionPoly operator * (Poly q); // poly multiplicationunsigned int deg() // degree { return(pol[0] > 0 ? pol[0] : 0); }void display(); // display host object/* other members */private:int length(); // length of polint * pol; };The class Poly is a class that creates a type for a polynomial object. It has the data members for the polynomial (represented by the pol pointer), and appropriate behaviors for polynomial objects.The data is stored in so-called sparse notation. That is, we store only the terms of the polynomial that are non-zero. For example, the polynomial3x8 - 5x6 - x2 + 4x -3would be represented as an integer array8 3 6 -5 2 -1 1 4 0 -3 -1Each term is represented by two integer values. For example, 3x8 is represented by the values 8 and 3 (first two pol values). We have the exponent, followed by the coefficient.8 3 6 -5 2 -1 1 4 0 -3 –1Here we see each term is shown enclosed in the box. The last value in pol is just aterminator marker. We need it because we do not know the last term of a polynomial. In theabove example the last term is -3 so the last term is stored as 0 –3 . But the last term couldhave been the 4x term, or even the 3x8 term. A polynomial which has no terms, can be stored as –1.What is pol?This is nothing more that a data member which is an int * (int pointer)Since each polynomial can have a different number of terms, the number of elements for theint array pol cannot be known ahead of time. So it is during the constructor time that weallocate the pol pointer.For example:pol = new int [n]allocates a buffer with n places for int values.The actual Poly class constructor follows:Poly::Poly(int *p, int n) { // constructorn = 2*n;pol = new int[n+1]; for ( int i=0 ; i < n ; i++ )pol[i] = *p++;pol[n] = -1; }The constructor takes two parameters (formal parameters) and uses them to create an instance of the pol array. Parameter 1 is an array which looks just like pol, but does not have a –1 as its last element. Parameter 2 is the number of terms the newly constructed polynomial should have. The statementpol = new int[n+1];allocates a buffer for two int values for each polynomial term as well as adds one for the –1 terminator marker. Next we copy the p array into the pol array (all n terms passed to the constructor). Finally, we place the –1 terminator into the last spot.The polynomial has been constructed!Why do we copy the array p into pol? Because we want the data of the Poly object to be private. Using pol = p; would be a poor code since the p array is accessible to the client code calling the Poly constructor!The statementpol[i] = *p++;assigns into the pol array at index i the value of the int to which p ispointing.Remember that an array is a pointer to its first element. AND its type is a pointer to the element type. So pol is an int * type. Also p is an int * type. Since an array a pointer, C++ allows arrays andpointers to access the elements they refer to by either *p or p[0] notation (access the first element)or*(p + i) or p[i] (access the ith element)Since * p ++has two operators, we need to know which has higher precedence. Actuallyit is ++. BUT since ++ follows its operand p, p does not get incrementeduntil after the operation is completed. The * operator is a dereference operator, therefore it returns the value of the int at which p is pointing.Note that the Poly class is stored as a sparse polynomial. No unnecessary terms are in the pol array. We must make sure that any Poly object always has that characteristic.A client can have code such as:#include “Poly.h”int p1[] = { 3,5,0,4,-1}int p2[] = { 3,2,1,4,-1}Poly poly1(p1, 2); // create a two term Poly object p1Poly poly2(p2, 2);// we should have a display method to output the Polyspoly1.display(); // 5x^3 + 4poly2.display(); // 2x^3 + 4xPoly poly3; // we need a default constructor too!// we should define a + () operator which returns a Polypoly3 = poly1 + poly2;poly3.display(); // 7x^3 + 4x + 4// we should define a - () operator which returns a Poly(poly1 – poly2).display(); // 3x^3 – 4x + 4All the operations on the Poly class use pointersCode such as * c ++ = * b ++assigns the value of the element that b is pointing at into the memory location that c is referring to. The ++ simply increments the pointers to the next int element.The code below is equivalent* c = * bc++;b++;Study the operator + ()closely on the code at the end of this handout. Implement the Poly class and with the methods such as display(), displayNice(), constructor() and operator+(), and see if they all make sense.A lot of the operator +() code is tricky because they are trying to keep the terms sparse, and eliminate the zero terms. Remember one polynomial can have terms no lower than the power of 5, and another can have terms in the power of 4,3,2,1, and a constant. In that case the =() operator needsto put the lower terms in the result of the addition.Note that the operator+() creates an array for the addition,uses the pol pointers from the host and the parameter Poly object (called q in the operator +() code) to create an array of exponents and coefficients. Finally placing a –1 atthe end. THEN it constructs a Poly object and returns itPoly ans(tmp, (c-tmp)/2);delete tmp;return ans }Note that c – tmp is called pointer arithmeticThis works ONLY if both pointer are of the same type AND referring to addresses in the same array!Example:6 2 2 -1 0 7 -1This corresponds to a polynomial 2x^6 – x^2 + 7Suppose we have 2 int pointers, tmp and c6 2 2 -1 0 7 -1tmp cc – tmp is pointer arithmetic and the result of subtraction of two pointers is a pure number – NOT and addressc – tmp equals the number of elements between the to pointers. In this case it is 6.So … Poly ans (tmp, (c – tmp)/2);constructs a Poly object with the tmp array with the number of terms equal to 3 (remember we need two int locations for each term (exponent/coefficient), but the Poly constructor takes care of that).If we executedc++;c – tmp would be 7The code://///// File: poly.h ///////class Poly { public:Poly() { pol = new int[1]; pol[0] = -1; }Poly(int *p, int terms); // constructorPoly operator + (Poly q); // poly additionPoly operator - (Poly q); // poly subtractionPoly operator * (Poly q); // poly


View Full Document

UB CS 400 - Polynomial Program Handout

Download Polynomial Program Handout
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Polynomial Program Handout and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Polynomial Program Handout 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?