Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

question Develop the class Polynomial. The internal representation of a Polynomi

ID: 3626372 • Letter: Q

Question

question

Develop the class Polynomial. The internal representation of a Polynomial is an array of terms. Each term contains a coefficient and an exponent. The term:

2x4

has the coefficient of 2 and the exponent 4. Develop a complete class containing proper constructor and destructor functions as well as set and get functions. 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.

Explanation / Answer

please rate - thanks

#include <iostream>
using namespace std;
class polynomial
{private:
int coeff[10];
int exp[10];
int terms;
public:
polynomial();
void input(int);
void output();
polynomial operator + (polynomial);
polynomial operator - (polynomial);     
};

int main()
{int i,na,nb,nc;
polynomial a,b,c;
a.input(1);
b.input(2);
cout<<"    ";
a.output();
cout<<" + ";
b.output();
cout<<"-------------- ";
c=a+b;
c.output();
cout<<endl;
cout<<"    ";
a.output();
cout<<" - ";
b.output();
cout<<"-------------- ";
c=a-b;
c.output();
cout<<endl;
system("pause");
return 0;
}
polynomial::polynomial()
{int i;
for(i=0;i<10;i++)
   {coeff[i]=0;
   exp[i]=0;
   }
terms=0;
}    
void polynomial:: input(int poly)
{
cout<<"Input is in the form coefficient space exponent -1 -1 to exit ";
cout<<"Enter polynomial number "<<poly<<endl;
cin>>coeff[terms]>>exp[terms];
while(coeff[terms]>0||exp[terms]>=0)
{terms++;
   cin>>coeff[terms]>>exp[terms];
}
terms--;

}
void polynomial ::output()
{int i;
for(i=0;i<terms;i++)
     {cout<<coeff[i]<<"x^"<<exp[i];
     if(exp[i+1]>=0)
        cout<<"+";
     }
cout<<coeff[terms];
if(exp[terms]>1)
     cout<<"x^"<<exp[terms];
cout<<endl<<endl;
}

polynomial polynomial :: operator + (polynomial b)
{int i=0,j=0,k=-1;
polynomial c;
c.terms=0;
do
{k++;
if(exp[i]>b.exp[j])
     {c.exp[k]=exp[i];
      c.coeff[k]=coeff[i];
      i++;
      }
else
    if(b.exp[j]>exp[i])
      {c.exp[k]=b.exp[j];
      c.coeff[k]=b.coeff[j];
      j++;
      }
    else
       {c.exp[k]=b.exp[i];
       c.coeff[k]=coeff[i]+b.coeff[j];
       i++;
       j++;
       }
}while(c.exp[k]>=0);
c.terms=k-1;

return c;
}
polynomial polynomial:: operator -(polynomial b)
{int i=0,j=0,k=-1;
polynomial c;
c.terms=0;
do
{k++;

if(exp[i]>b.exp[j])
     {c.exp[k]=exp[i];
      c.coeff[k]=coeff[i];
      i++;
      }
else
    if(b.exp[j]>exp[i])
      {c.exp[k]=b.exp[j];
      c.coeff[k]=b.coeff[j]*-1;
      j++;
      }
    else
       {c.exp[k]=b.exp[i];
       c.coeff[k]=coeff[i]-b.coeff[j];
       i++;
       j++;
       }
}while(c.exp[k]>=0);
c.terms=k-1;

return c;
}