Professional Documents
Culture Documents
Degree=3
No of terms=4
Expression 2: 8x3+2x1+5
Degree=3
No of terms=3
3x3+5x2+2x1+8x0
8x3+2x1+5x0
11x3+5x2+4x1+13x0
if(expn1==expn2)
addition of coef
else if(expn1>expn2)
else
int cof[10];
int expn[10];
int nott;
public:
Poly(int t) { nott=t;}
void readpoly();
void showpoly();
void addpoly(Poly);
};
void Poly::addpoly(Poly p2)
{
Poly p3(10);
int term1=0,term2=0,term3=0;
while(term1<nott && term2<p2.nott)
{
if(expn[term1]==p2.expn[term2])
{
p3.cof[term3]=cof[term1]+p2.cof[term2];
p3.expn[term3]=expn[term1];
term1++;term2++;
term3++;
}
else if(expn[term1]>p2.expn[term2])
{
p3.cof[term3]=cof[term1];
p3.expn[term3]=expn[term1];
term1++;
term3++;
}
else
{
p3.cof[term3]=p2.cof[term2];
p3.expn[term3]=p2.expn[term2];
term2++;
term3++;
}
}
while(term1<nott)
{
p3.cof[term3]=cof[term1];
p3.expn[term3]=expn[term1];
term1++;
term3++;
}
while(term2<p2.nott)
{
p3.cof[term3]=p2.cof[term2];
p3.expn[term3]=p2.expn[term2];
term2++;
term3++;
}
p3.nott=term3;
cout<<"Addition of poly:\n";
p3.showpoly();
}
void Poly::readpoly()
{
for(int i=0;i<nott;i++)
{
cout<<"\nCoefficient :";
cin>>cof[i];
cout<<"\nPower :";
cin>>expn[i];
}
void Poly::showpoly()
{
cout<<"\nEntered Expression \n:";
for(int i=0;i<nott;i++)
{
cout<<cof[i]<<"X"<<expn[i];
if(i<nott-1)
cout<<"+";
}
}
int main()
{
Poly p1(4),p2(3);
cout<<"\nExpression 1 :";
p1.readpoly();
cout<<"\nExpression 2 :";
p2.readpoly();
p1.showpoly();
p2.showpoly();
// temp.addpoly(p1,p2)
//p3=addpoly(p1,p2) friend function
p1.addpoly(p2);
return 0;
}