In: Computer Science
Skills needed to complete this assignment: dynamic arrays, classes.
PROMPT:
In mathematics, a polynomial is an expression consisting of cariables and coefficients which involves only the operation of addition, subtraction, multiplication, and non-negative integer exponents of variables. A polynomial in a single variable can always be written in the form: anxn + an-1xn-1+ ....... + a2x2 + a1x + a0
Where a0 ....., an are coefficients and x is the variable.
In this assignment, you will complete a polynomial class using dynamic arrays. For simplicity, we only consider polynomials with one variable and integer coefficients. |
One simple way to implement the polynomial class is to use an array of integers to store the coefficients. The index of the array is the exponent of the corresponding term. If a term is missing, then it simply has a zero coefficient. For example, we can use [2, 0, 0, 4] to represent the polynomial 4x3 + 2, since the polynomial can be rewritten as 2x0 + 0x1 + 0x2 + 4x3 . |
The Poly class provided in the template has two data members, an integer called arraySize, an integer pointer called coeff.coeff will be used to link the dynamic coefficient array, and arraySize will be used to hold the current size of coeff array. With these data members, you will complete the following class member functions and friend functions. |
Poly() The default class constructor. A class member function. It is used to allocated an array of size DEFAULTPOLY (a global constant provided in the template) and initialize the polynomial to the constant 0. |
Poly(int size) An alternate class constructor. A class member function. It is used to allocate an array of size size (the input parameter) and initialize the polynomial to the constant 0. |
Poly(const Poly& aPoly) The copy constructor. A class member function. It is used to construct a new Poly that is a copy of an existing polynomial object. aPoly. Note that the new Poly object does not share the same dynamic array with aPoly. A new array needs to be allocated to it. |
~Poly() The deconstructor. A class member function. It is used to destroy a Poly object by freeing its dynamically allocated array. |
void grow(int newSize) A class member function. If newSize is greater than the current array size, this function will increase the size of the dynamically allocated array by allocating a new array of the desired size, copying the data from the old array to the new array, and then releasing the old array. If newSize is less than or equal to the current size, no actions are taken. |
int degree() const A class member function. The degree of a single variable polynomial is the highest exponent of its terms with non zero coefficients. For example, the polynomial 4x3 + 2 has degree 3, and the polynomial 2 has degree 0. This function returns the degree of the polynomial. |
void setCoeff(int value, int i) A class member function. The function is used to add a term value x xi, in the polynomial. Grow the coefficient array if necessary. |
int getCoeff(int i) const A class member function This function is used to find the coefficient of the xiterm in the polynomial. If the polynomial does not contain a term with power i (for example i >= arraySize) , the function returns 0. |
void negate() A class member function. The polynomial will be changed to represent its multiplication by -1 after this function is called. For example, the polynomial 4x3 + 2 will be changed to -4x3 -2 |
Poly operator+ (const Poly& aPoly, const Poly& bPoly) A friend function of the Poly class. This function overloads the operator +. This function adds two polynomials and returns a new polynomial that is the result. For example, with poly1 = 4x3 + 2 and poly2 = 5x3 +2x + 2, poly1 + poly2 is 9x3 + 2x + 4 |
Poly operator- (const Poly& aPoly, const Poly& bPoly) A friend function of the Poly class. This function overloads the operator -. This function subtracts the second parameter Poly object from the first parameter Poly object and returns a new polynomial that is the result. For example, with poly1 = 4x3 + 2 and poly2 = 5x3 + 2x + 2, poly1 - poly2 is -x3-2x. (Hint: poly1 - poly2 = poly1 + (-1) * poly2) |
bool operator== (const Poly& aPoly, const Poly& bPoly) A friend function of the Poly class. This function overloads the operator ==. It is used to decide whether two Poly objects represent the same polynomial. Note that for two Poly objects representing the same polynomial, their coeff array sizes can be different. For example, coefficient arrays [2, 0, 0, 4] and [2, 0, 0, 4, 0, 0] both represent the polynomial 4x3+2 |
ostream& operator<< (ostream& out, const Poly &aPoly) A friend function of the Poly class. This function overloads the operator <<. It prints a Poly object to the given output stream in a readable format. For example, the polynomial 4x3-2x+2 will be printed as 4x^3 - 2x^1+2. Note that we will not print the variable nor exponent for a term with zero exponent (2x0 is printed as 2). If a polynomial has no nonzero coefficient, it is printed as 0. |
PROGRAM TEMPLE: YOU MUST DEVELOP YOUR CODE FROM THIS TEMPLATE. DO NOT MODIFY THE CLASS HEADER.
#include <cmath> #include <iostream> using namespace std; const int DEFAULTPOLY = 10; //Default size of our dynamic coefficient array |
//DO NOT modify the class header class Poly { private: //Data members int arraySize; //size of array int *coeff; //dynamic array public: Poly(); Poly(int size); Poly(const Poly& aPoly); ~Poly(); const Poly& operator=(const Poly& aPolt); void grow(int newSize); int degree() const; void setCoeff(int value, int i); int getCoeff(int i) const; void negate(); friend Poly operator+(const Poly& aPoly, const Poly& bPoly); friend Poly operator-(const Poly& aPoly, const Poly& bPoly); friend bool operator==(const Poly& aPoly, const Poly& bPoly); friend ostream& operator<<(ostream& out, const Poly &aPoly); }; |
int main() { Poly poly1, poly2; int numCoeff, coeffValue, coeffDegree, x; //prompt user for number of coefficients cout << "How many coefficients for polynomial 1?" << endl; cin >> numCoeff; for(int i=0; i<numCoeff; ++i) { cout << "Coefficient " << i+1 << " for polynomial 1:"; cin >> coeffValue >> coeffDegree; poly1.setCoeff(coeffValue, coeffDegree); } cout << endl << "How many coefficients for polynomial 2?" << endl; cin >> numCoeff; for(int i=0; i<numCoeff; ++i) { cout << "Coefficient " << i+1 << " for polynomial 2:"; cin >> coeffValue >> coeffDegree; poly2.setCoeff(coeffValue, coeffDegree); } //sample test cases for degree and operator << cout << endl << "Polynomial 1 = " << poly1 << endl; cout << "Polynomial 1 has degree " << poly1.degree() << endl; cout << "Polynomial 2 = " << poly2 << endl; cout << "Polynomial 2 has degree " << poly2.degree() << endl; //Sample test cases for operator+ and operator- cout << endl << "Polynomial 1 + Polynomial 2 = " << poly1 + poly2 << endl; cout << "Polynomial 1 - Polynomial 2 = " << poly1 - poly2 << endl << endl; //Sample test cases for operator == if(poly1 == poly2) { cout << "Two polynomials are the same." << endl; } else { cout << "Two polynomials are different." << endl; } return 0; } |
//DO NOT MODIFY THIS FUNCTION const Poly& Poly::operator= (const Poly& aPoly) { if(this == &aPoly){ return *this;} if(coeff){ delete [] coeff;} arraySize = aPoly.arraySize; coeff = new int[arraySize]; for(int i=0; i<arraySize; ++i) { coeff[i] = aPoly.getCoeff(i); } return *this; } |
//YOUR CODE GOES HERE. COMPLETE EACH CLASS MEMBER FUNCTIONS AND FRIEND FUNCTIONS HERE: |
Program
#include <cmath>
#include <iostream>
using namespace std;
const int DEFAULTPOLY = 10; //Default size of our dynamic coefficient array
//DO NOT modify the class header
class Poly
{
private:
//Data members
int arraySize; //size of array
int *coeff; //dynamic array
public:
Poly();
Poly(int size);
Poly(const Poly& aPoly);
~Poly();
const Poly& operator=(const Poly& aPolt);
void grow(int newSize);
int degree() const;
void setCoeff(int value, int i);
int getCoeff(int i) const;
void negate();
friend Poly operator+(const Poly& aPoly, const Poly& bPoly);
friend Poly operator-(const Poly& aPoly, const Poly& bPoly);
friend bool operator==(const Poly& aPoly, const Poly& bPoly);
friend ostream& operator<<(ostream& out, const Poly &aPoly);
};
//default constructor
Poly::Poly()
{
arraySize = DEFAULTPOLY;
coeff = new int[DEFAULTPOLY];
for(int i=0; i<DEFAULTPOLY; i++)
coeff[i] = 0;
}
//constructor
Poly::Poly(int size)
{
arraySize = size;
coeff = new int[size];
for(int i=0; i<size; i++)
coeff[i] = 0;
}
//copy constructor
Poly::Poly(const Poly& aPoly)
{
arraySize = aPoly.arraySize;
if(coeff){
delete [] coeff;}
coeff = new int[arraySize];
for(int i=0; i<arraySize; i++)
coeff[i] = aPoly.coeff[i];
}
//destructor
Poly::~Poly()
{
delete []coeff;
}
// = operator overloading
const Poly& Poly::operator=(const Poly& aPoly)
{
if(this == &aPoly){
return *this;}
if(coeff){
delete [] coeff;}
arraySize = aPoly.arraySize;
coeff = new int[arraySize];
for(int i=0; i<arraySize; ++i)
{
coeff[i] = aPoly.getCoeff(i);
}
return *this;
}
// function to grow the polynomial
void Poly::grow(int newSize)
{
if(arraySize>=newSize) return;
Poly temp(newSize);
for(int i=arraySize; i<newSize; i++)
temp.coeff[i] = 0;
for(int i=0; i<arraySize; i++)
temp.coeff[i] = coeff[i];
delete []coeff;
coeff = temp.coeff;
}
// function to return the degree of the polynomial
int Poly::degree() const
{
int i;
for(i=DEFAULTPOLY-1; coeff[i]==0; i--);
return i;
}
// function to set the coefficient of the polynomial
void Poly::setCoeff(int value, int i)
{
if(i>arraySize) grow(i);
coeff[i] = value;
}
// function to return coefficient of the polynomial
int Poly::getCoeff(int i) const
{
if(i>=arraySize) return 0;
return coeff[i];
}
// function to negate the polynomial
void Poly::negate()
{
for(int i=0; i<arraySize; i++)
coeff[i] = -coeff[i];
}
// + operator overloading
Poly operator+(const Poly& aPoly, const Poly& bPoly)
{
Poly temp;
int i;
for(i=0; i<DEFAULTPOLY-1; i++)
temp.coeff[i] = aPoly.coeff[i] + bPoly.coeff[i];
return temp;
}
// - operator overloading
Poly operator-(const Poly& aPoly, const Poly& bPoly)
{
Poly temp;
int i;
for(i=0; i<DEFAULTPOLY-1; i++)
temp.coeff[i] = aPoly.coeff[i] - bPoly.coeff[i];
return temp;
}
// == operator overloading
bool operator==(const Poly& aPoly, const Poly& bPoly)
{
int i;
for(i=0; i<DEFAULTPOLY; i++)
if(aPoly.coeff[i]!=bPoly.coeff[i]) return false;
return true;
}
// << operator overloading
ostream& operator<<(ostream& out, const Poly
&aPoly)
{
for(int i=aPoly.degree(); i>=0; i--)
{
if(aPoly.coeff[i]==0) continue;
if(i==aPoly.degree() || aPoly.coeff[i]<0)
out<<aPoly.coeff[i]<<"X^"<<i<<" ";
else
out<<"+ "<<aPoly.coeff[i]<<"X^"<<i<<"
";
}
out<<endl;
return out;
}
/////////////////////////////////////////////////////////////////////////////////////////
int main() {
int numCoeff, coeffValue, coeffDegree, x;
Poly poly1, poly2;
//prompt user for number of coefficients
cout << "How many coefficients for polynomial 1?" << endl;
cin >> numCoeff;
for(int i=0; i<numCoeff; ++i)
{
cout << "Coefficient " << i+1 << " for polynomial 1:";
cin >> coeffValue >> coeffDegree;
poly1.setCoeff(coeffValue, coeffDegree);
}
cout << endl << "How many coefficients for polynomial
2?" << endl;
cin >> numCoeff;
for(int i=0; i<numCoeff; ++i)
{
cout << "Coefficient " << i+1 << " for polynomial 2:";
cin >> coeffValue >> coeffDegree;
poly2.setCoeff(coeffValue, coeffDegree);
}
//sample test cases for degree and operator <<
cout << endl << "Polynomial 1 = " << poly1 << endl;
cout << "Polynomial 1 has degree " << poly1.degree() << endl;
cout << "Polynomial 2 = " << poly2 << endl;
cout << "Polynomial 2 has degree " << poly2.degree() << endl;
//Sample test cases for operator+ and operator-
cout << endl << "Polynomial 1 + Polynomial 2 = " << poly1 + poly2 << endl;
cout << "Polynomial 1 - Polynomial 2 = " << poly1 - poly2 << endl << endl;
//Sample test cases for operator ==
if(poly1 == poly2)
{
cout << "Two polynomials are the same." << endl;
}
else
{
cout << "Two polynomials are different." << endl;
}
return 0;
}
Output: