Develop class Polynomial. The internal representation of a polynomial is an arra
ID: 643318 • Letter: D
Question
Develop class Polynomial. The internal representation of a polynomial is an array: one which stores the coefficients and the index of the array determines the exponents. For example, the polynomial (^ refers to the power of)
-4x^0 + x^1 + 4x^3 - 3x^4
has the second element (exponent 1) of coefficient array store 1. Similarly the third term is stored as coefficient 4 in the 4th element (index and exponent 3).
Two ways to go about using an array:
Assume the maximum size of the array as 50 i.e. the maximum exponent can be 49 and the coefficients and exponents are integers only. Using this method allows for less flexibility and used up more memory.
Use a pointer and dynamic memory allocation for the array of each polynomial. This will require for the user to first enter the number of terms (or the highest exponent in the polynomial) and then the polynomial itself. Do remember to create a copy constructor as well if you use pointer for an array.
Develop a full class with proper constructor and destructor functions as well as set and get functions for the inputting and printing the polynomial (the polynomial should be printed as shown above in the example). The class should also provide the following overloaded operator capabilities:
Overload the addition (+) operator to add two polynomials
Overload the subtraction (-) operator to subtract two polynomials
Overload the assignment (=) operator to assign one polynomial to another
Overload the multiplication operator (*) to multiply two polynomials
The driver program (main part of the program) should ask the user to enter two Polynomials (specify to the user how the polynomial should be entered) and then give a choice to do the following:
(1) Addition
(2) Subtraction
(3) Multiplication
(4) Assignment
(5) Exit
Print the polynomials and the resulting polynomial of the operation after each operation is performed. The polynomial should be printed using the format above. The program should continue until the user wants to exit.
Explanation / Answer
#ifndef POLY_H
#define POLY_H
#include
#include
#include
using namespace std;
class PolyNom
{
public:
PolyNom();
PolyNom operator + ( const PolyNom& ) const;
PolyNom operator - ( const PolyNom& ) const;
PolyNom operator * ( const PolyNom& );
PolyNom operator = ( const PolyNom& );
PolyNom& operator +=( const PolyNom& );
PolyNom& operator -=( const PolyNom& );
PolyNom& operator *=( const PolyNom& );
void enterTerms();
void printPolyNom() const;
int getNumOfTerms();
int getTermExpo( int );
int getTermCoeff( int );
void setCoeff( int, int);
~PolyNom();
private:
int numOfTerms;
int expo[ 100 ];
int coeff[ 100 ];
void polyNomCombine( PolyNom& );
};
PolyNom::PolyNom()
{
for ( int t = 0; t < 100; t ++)
{
coeff[t] = 0;
expo[t] = 0;
}
}
void PolyNom::printPolyNom() const
{
int start;
bool zero = false;
if (coeff[ 0 ] )
{
cout << coeff[ 0 ];
start = 1;
zero = true;
}
else
{
if ( coeff[1])
{
cout << coeff[1] << 'x';
if( ( expo[1] != 0) && (expo[1] != 1))
cout << '^' << expo[1];
zero = true;
}
start = 2;
}
for ( int x = start; x < 100; x++)
{
if( coeff[x] != 0)
{
cout << showpos << coeff[x] << noshowpos << 'x';
if( (expo[x] !=0) && ( expo[x] != 1) )
cout << '^' << expo[x];
zero = true;
}
}
if( !zero)
cout << '0';
cout << endl;
}
PolyNom PolyNom::operator = ( const PolyNom &r )
{
expo[0] = r.expo[0];
coeff[0] = r.coeff[0];
for( int s = 1; ( s < 100); s++ )
{
if ( r.expo[s] != 0 )
{
expo[s] = r.expo[s];
coeff[s] = r.coeff[s];
}
else
{
if( expo[s] == 0)
break;
expo[s] = 0;
coeff[s] = 0;
}
}
return *this;
}
PolyNom PolyNom::operator + ( const PolyNom &r ) const
{
PolyNom temp;
bool expoExists;
int s;
temp.coeff[0] = coeff[0] + r.coeff[0];
for ( s = 1; ( s < 100) && ( r.expo[s] != 0 ); s++)
{
temp.coeff[s] = r.coeff[s];
temp.expo[s] = r.expo[s];
}
for ( int x = 1; x < 100; x++ )
{
expoExists = false;
for (int t = 1; ( t < 100) && (!expoExists); t++)
if( expo[x] == temp.expo[t] )
{
temp.coeff[t] += coeff[x];
expoExists = true;
}
if( !expoExists )
{
temp.expo[s] = expo[x];
temp.coeff[s] += coeff[x];
s++;
}
}
return temp;
}
PolyNom& PolyNom::operator +=( const PolyNom &r )
{
*this = *this + r;
return *this;
}
PolyNom PolyNom::operator - ( const PolyNom &r ) const
{
PolyNom temp;
bool expoExists;
int s;
temp.coeff[0] = coeff[0] - r.coeff[0];
for( s = 1; (s < 100) && ( expo[s] != 0 ); s++)
{
temp.coeff[s] = coeff[s];
temp.expo[s] = expo[s];
}
for( int x = 1; x < 100; x++ )
{
expoExists = false;
for( int t = 1; ( t < 100) && (!expoExists); t++)
if( r.expo[x] == temp.expo[t] )
{
temp.coeff[t] -= r.coeff[x];
expoExists = true;
}
if( !expoExists )
{
temp.expo[s] = r.expo[x];
temp.coeff[s] -= r.coeff[x];
s++;
}
}
return temp;
}
PolyNom& PolyNom::operator -=( const PolyNom &r )
{
*this = *this - r;
return *this;
}
PolyNom PolyNom::operator * ( const PolyNom &r)
{
PolyNom temp;
int s = 1;
for( int x = 0; ( x < 100) && ( x == 0 || r.coeff[x] != 0); x++)
for( int y = 0; (y < 100) && y == 0 || r.coeff[y] != 0; y++)
if( (expo[x] == 0) && ( r.expo[y] == 0) )
temp.coeff[0] += coeff[x] * r.coeff[y];
else
{
temp.coeff[s] = coeff[x] * r.coeff[y];
temp.expo[s] = expo[x] + r.expo[y];
s++;
}
polyNomCombine( temp );
return temp;
}
void PolyNom::polyNomCombine( PolyNom &w)
{
PolyNom temp = w;
int exp;
for( int x = 0; x < 100; x++ )
{
w.coeff[x] = 0;
w.expo[x] = 0;
}
for( int x = 1; x < 100; x++ )
{
exp = temp.expo[x];
for( int y = x + 1; y < 100; y++)
if( exp == temp.expo[y] )
{
temp.coeff[x] += temp.coeff[y];
temp.expo[y] = 0;
temp.coeff[y] = 0;
}
}
w = temp;
}
PolyNom& PolyNom::operator *=( const PolyNom &r)
{
*this = *this * r;
return *this;
}
void PolyNom::enterTerms()
{
bool found = false;
int c, e, term;
cout << " Enter number of polynomial terms: ";
cin >> numOfTerms;
for( int n = 1; n <= numOfTerms; n++ )
{
cout << " Enter a coefficient: ";
cin >> c;
cout << "Enter exponent: ";
cin >> e;
if( c != 0 )
{
if( e == 0)
{
coeff[0] += c;
continue;
}
for( term = 1; (term < 100) && ( coeff[term] != 0); term++)
if ( e == expo[term] )
{
coeff[term] += c;
expo[term] = e;
found = true;
}
if( !found )
{
coeff[term] += c;
expo[term] = e;
}
}
}
}
int PolyNom::getNumOfTerms()
{
return numOfTerms;
}
int PolyNom::getTermExpo( int term )
{
return expo[term];
}
int PolyNom::getTermCoeff( int term )
{
return coeff[term];
}
void PolyNom::setCoeff( int term, int coeff)
{
if( coeff[ term ] == 0)
cout << "No term at this here, can't set term." << endl;
else
coeff[ term ] = coeff;
}
PolyNom::~PolyNom()
{
}
int main()
{
PolyNom a, b, c, t;
a.enterTerms();
b.enterTerms();
t = a;
cout << "First polynomial is: ";
a.printPolyNom();
cout << "Second polynomial is: ";
b.printPolyNom();
cout << " Adding the polynomials yields: ";
c = a + b;
c.printPolyNom();
cout << " += the polynomials: ";
a += b;
a.printPolyNom();
cout << " Subtracting the polynomials: ";
a = t;
c = a - b;
c.printPolyNom();
cout << " -= the polynomials: ";
a -= b;
a.printPolyNom();
cout << " Multiplying the polynomials: ";
a = t;
c = a * b;
c.printPolyNom();
cout << " *= the polynomials: ";
a *= b;
a.printPolyNom();
cout << endl;
system("pause");
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.