View the step-by-step solution to:

# #include &lt;iostream&gt; using namespace std; class Term{ friend class Polynomial; public:

#include <iostream>

using namespace std;

class Term{
friend class Polynomial;
public:
Term();
Term(float coefficient_arg,int exponent_arg);
void setvalue(int exponent_arg);
void setvalue(float coefficient_arg,int exponent_arg);
void printterm();

private:
float coefficient;
int exponent;
};
Term::Term(){coefficient=0;exponent=0;}
Term::Term(float coefficient_arg,int exponent_arg){coefficient=coefficient_arg;exponent=exponent_arg;}

void Term::setvalue(int exponent_arg){
cout<<"Enter the coefficient for x^"<<exponent_arg<<endl;cin>>coefficient;
exponent=exponent_arg;
}

void Term::setvalue(float coefficient_arg,int exponent_arg){
coefficient=coefficient_arg;
exponent=exponent_arg;
}

void Term:: printterm(){if(exponent==0)cout<<coefficient;
else if(exponent==1)cout<<coefficient<<"X";
else cout<<coefficient<<"X^"<<exponent;}

class Polynomial{
public:
Polynomial();
void setvalue();
void printpolynomial();

Polynomial operator+(const Polynomial &);
Polynomial operator-(const Polynomial &);
Polynomial operator*(const Polynomial &);
Polynomial &operator=(const Polynomial &);
Polynomial &operator+=(const Polynomial &);
Polynomial &operator-=(const Polynomial &);

private:
int degree;
Term *termarr;
};

Polynomial:: Polynomial(){int counter;
degree=0;
Term *temparr=new Term[degree+1];
Term zero;
temparr[0]=zero;
termarr=temparr;
}

void Polynomial::setvalue(){int counter;
cout<<"Enter the polynomial's degree: ";cin>>degree;
Term *temparr=new Term[degree+1];
for(counter=0;counter<=degree;counter++)
temparr[counter].setvalue(counter);
termarr=temparr;
}

void Polynomial:: printpolynomial(){int counter;
termarr[degree].printterm();
for(counter=degree-1;counter!=-1;counter--)
{
if(termarr[counter].coefficient>0)
{cout<<" ";cout<<"+";termarr[counter].printterm();cout<<" ";}

if(termarr[counter].coefficient<0)
{cout<<" ";termarr[counter].printterm();cout<<" ";}

}
}

Polynomial Polynomial:: operator+(const Polynomial &arg){
Polynomial temp;
int counter;
float coefficient_temp;
int lower_degree;

if(degree>arg.degree)
{temp.degree=degree;
lower_degree=arg.degree;}
else
{temp.degree=arg.degree;
lower_degree=degree;}

Term *temparr=new Term[temp.degree+1];

for(counter=0;counter<=lower_degree;counter++)
{coefficient_temp=termarr[counter].coefficient+arg.termarr[counter].coefficient;
temparr[counter].setvalue(coefficient_temp,counter);}

for(counter=lower_degree+1;counter<=temp.degree;counter++)
{
if(degree>arg.degree)
{coefficient_temp=termarr[counter].coefficient;
temparr[counter].setvalue(coefficient_temp,counter);}

else
{coefficient_temp=arg.termarr[counter].coefficient;
temparr[counter].setvalue(coefficient_temp,counter);}
}

temp.termarr=temparr;

return temp;
}

Polynomial Polynomial:: operator-(const Polynomial &arg){
Polynomial temp;
int counter;
float coefficient_temp;
int lower_degree;

if(degree>arg.degree)
{temp.degree=degree;
lower_degree=arg.degree;}
else
{temp.degree=arg.degree;
lower_degree=degree;}

Term *temparr=new Term[temp.degree+1];

for(counter=0;counter<=lower_degree;counter++)
{coefficient_temp=termarr[counter].coefficient-arg.termarr[counter].coefficient;
temparr[counter].setvalue(coefficient_temp,counter);}

for(counter=lower_degree+1;counter<=temp.degree;counter++)
{
if(degree>arg.degree)
{coefficient_temp=termarr[counter].coefficient;
temparr[counter].setvalue(coefficient_temp,counter);}

else
{coefficient_temp=arg.termarr[counter].coefficient;
temparr[counter].setvalue(coefficient_temp,counter);}
}

temp.termarr=temparr;

return temp;
}

Polynomial Polynomial:: operator*(const Polynomial &arg)
{
int counter,counter2;
float multiplied_coefficient;
Polynomial temppoly;
Polynomial *polyarr = new Polynomial[degree+1];
Term tempterm;

for(counter2=degree;counter2!=-1;counter2--){
polyarr[-(counter2-degree)].degree=degree+arg.degree;

for(counter=arg.degree;counter!=-1;counter--)
{multiplied_coefficient=termarr[counter2].coefficient*arg.termarr[counter].coefficient;
cout<<termarr[counter2].coefficient<<"*"<<arg.termarr[counter].coefficient<<"="<<multiplied_coefficient;
}

cout<<endl<<"print polyarr["<<-(counter2-degree)<<"] : ";
polyarr[-(counter2-degree)].printpolynomial();cout<<endl;
}

for(counter=0;counter<=degree;counter++)
temppoly+=polyarr[counter];

return temppoly;
}

Polynomial &Polynomial:: operator=(const Polynomial &arg)
{
degree=arg.degree;
termarr=arg.termarr;
return *this;
}

Polynomial &Polynomial:: operator+=(const Polynomial &arg)
{
Polynomial temp;
int counter;
float coefficient_temp;
int lower_degree;

if(degree>arg.degree)
{temp.degree=degree;
lower_degree=arg.degree;}
else
{temp.degree=arg.degree;
lower_degree=degree;}

Term *temparr=new Term[temp.degree+1];

for(counter=0;counter<=lower_degree;counter++)
{coefficient_temp=termarr[counter].coefficient+arg.termarr[counter].coefficient;
temparr[counter].setvalue(coefficient_temp,counter);}

for(counter=lower_degree+1;counter<=temp.degree;counter++)
{
if(degree>arg.degree)
{coefficient_temp=termarr[counter].coefficient;
temparr[counter].setvalue(coefficient_temp,counter);}

else
{coefficient_temp=arg.termarr[counter].coefficient;
temparr[counter].setvalue(coefficient_temp,counter);}
}

temp.termarr=temparr;

degree=temp.degree;
termarr=temp.termarr;

return *this;
}

Polynomial &Polynomial:: operator-=(const Polynomial &arg)
{
Polynomial temp;
int counter;
float coefficient_temp;
int lower_degree;

if(degree>arg.degree)
{temp.degree=degree;
lower_degree=arg.degree;}
else
{temp.degree=arg.degree;
lower_degree=degree;}

Term *temparr=new Term[temp.degree+1];

for(counter=0;counter<=lower_degree;counter++)
{coefficient_temp=termarr[counter].coefficient-arg.termarr[counter].coefficient;
temparr[counter].setvalue(coefficient_temp,counter);}

for(counter=lower_degree+1;counter<=temp.degree;counter++)
{
if(degree>arg.degree)
{coefficient_temp=termarr[counter].coefficient;
temparr[counter].setvalue(coefficient_temp,counter);}

else
{coefficient_temp=arg.termarr[counter].coefficient;
temparr[counter].setvalue(coefficient_temp,counter);}
}

temp.termarr=temparr;

degree=temp.degree;
termarr=temp.termarr;

return *this;
}

int main(){
cout<<"Creating polynomial 1"<<endl<<endl;
Polynomial poly1;
poly1.setvalue();
cout<<"Creating polynomial 2"<<endl<<endl;
Polynomial poly2;
poly2.setvalue();

cout<<"Polynomial 1 is..."<<endl;poly1.printpolynomial();
cout<<endl;

cout<<"Polynomial 2 is..."<<endl;poly2.printpolynomial();
cout<<endl;
cout<<endl;

cout<<"Polynomial 1 plus Polynomial 2 is ";add.printpolynomial();cout<<endl;

Polynomial minus;
minus=poly1-poly2;
cout<<"Polynomial 1 minus Polynomial 2 is ";minus.printpolynomial();cout<<endl;

Polynomial multiply;
multiply=poly1*poly2;
cout<<"Polynomial 1 multiply Polynomial 2 is ";multiply.printpolynomial();cout<<endl;

Polynomial equal;
equal=poly1;
cout<<"Polynomial 1 equal ";equal.printpolynomial();cout<<endl;

Polynomial plusequal;
cout<<"Creating another polynomial"<<endl;
plusequal.setvalue();
plusequal.printpolynomial();
plusequal+=poly1;
cout<<" plus Polynomial 1 equal ";plusequal.printpolynomial();cout<<endl;

Polynomial minusequal;
cout<<"Creating another polynomial"<<endl;
minusequal.setvalue();
minusequal.printpolynomial();
minusequal-=poly1;
cout<<" minus Polynomial 1 equal ";minusequal.printpolynomial();cout<<endl;

cout<<"Press enter to exit"<<endl;
getchar();getchar();
return 0;
}

### Why Join Course Hero?

Course Hero has all the homework and study help you need to succeed! We’ve got course-specific notes, study guides, and practice tests along with expert tutors.

### -

Educational Resources
• ### -

Study Documents

Find the best study resources around, tagged to your specific courses. Share your own to gain free Course Hero access.

Browse Documents