# Operations On Polynomials

/***************************************************

-> This C++ Program is to implement the following operations
on two polynomials
2.Substraction

-> Linked list data structure is used to represent a polynomial

-> Eg. of a polynomial is : 3*x^2 + 2*x^1 + 1

-> This program works well in the microsoft vc++ 6.0 environment

***************************************************/

#include<iostream.h>

class polynomial
{
private:
int a,n;
polynomial *next;
public:
void print_polynomial(polynomial *);
void sub_polynomials(polynomial *,polynomial *);
void function();
};

{
int a,n;
polynomial *NEW;
polynomial *tail;

cout<<“Now enter coefficient followed by exponent for the first term\n”;
cin>>a>>n;
while(n!=-1)
{
NEW=new polynomial;
NEW->a=a;
NEW->n=n;
NEW->next=NULL;

if(p==NULL)
p=NEW;
else
tail->next=NEW;
tail=NEW;

cout<<“Enter coefficient followed by exponent for the next term\n”;
cin>>a>>n;
}

return p;
}

void polynomial::print_polynomial(polynomial *p)
{
while(p!=NULL)
{
cout<<p->a;
if(p->n!=0)
{
cout<<“*x^”;
cout<<p->n<<‘ ‘;
}
if(p->next!=NULL)
cout<<“+ “;

p=p->next;
}
}

{
while(p!=NULL && q!=NULL)
{
if(p->n > q->n)
{
cout<<p->a<<“*x^”<<p->n<<” + “;
p=p->next;
}
else if(p->n < q->n)
{
cout<<q->a<<“*x^”<<q->n<<” + “;
q=q->next;
}
else
{
cout<<p->a+q->a<<“*x^”<<p->n<<” + “;
p=p->next;
q=q->next;
}
}

while(p!=NULL)
{
cout<<p->a<<“*x^”<<p->n<<” + “;
p=p->next;
}

while(q!=NULL)
{
cout<<q->a<<“*x^”<<q->n<<” + “;
q=q->next;
}

cout<<0<<endl;
}

void polynomial::sub_polynomials(polynomial*p,polynomial*q)
{
while(p!=NULL && q!=NULL)
{
if(p->n > q->n)
{
cout<<p->a<<“*x^”<<p->n<<” + “;
p=p->next;
}
else if(p->n < q->n)
{
cout<<-q->a<<“*x^”<<q->n<<” + “;
q=q->next;
}
else
{
if(p->a-q->a != 0)
cout<<p->a-q->a<<“*x^”<<p->n<<” + “;
p=p->next;
q=q->next;
}
}

while(p!=NULL)
{
cout<<p->a<<“*x^”<<p->n<<” + “;
p=p->next;
}

while(q!=NULL)
{
cout<<-q->a<<“*x^”<<q->n<<” + “;
q=q->next;
}

cout<<“0″<<endl;
}

void polynomial::function()
{
cout<<“********Instructions for Entering a polynomial********\n”;
cout<<“Enter coefficient followed by exponent for each term\n”;
cout<<“Enter exponents in the decreasing order\n”;
cout<<“Enter Exponent -1 to stop\n\n\n”;

cout<<“Enter the first polynomial P::\n\n”;
polynomial *p=NULL;
cout<<“\nThe entered polynomial is ::\n”;
print_polynomial(p);
cout<<endl<<endl;

cout<<“Enter the second polynomial Q::\n\n”;
polynomial *q=NULL;
cout<<“\nThe entered polynomial is ::\n”;
print_polynomial(q);
cout<<endl<<endl;

cout<<“The sum of the given two polynomials is P+Q ::\n”;

cout<<“\nThe difference of the given two polynomials is P-Q ::\n”;
sub_polynomials(p,q);

}

int main()
{
polynomial obj;
obj.function();
return 0;
}

## 4 thoughts on “Operations On Polynomials”

1. This is very nice and informative post. I have bookmarked your site in order to find out your post in the future.

2. Really nice site you have here. IÃƒÂ¢Ã¢â€šÂ¬Ã¢â€žÂ¢ve been reading for a while but this post made me want to say 2 thumbs up. Keep up the great work

3. NPPRSV says:

thanks………….

4. aamir khan says:

what is the output of that program