Operations On Polynomials


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

-> This C++ Program is to implement the following operations
   on two polynomials
   1.Addition
   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:
 polynomial* read_polynomial(polynomial *);
 void print_polynomial(polynomial *);
 void add_polynomials(polynomial *,polynomial *);
 void sub_polynomials(polynomial *,polynomial *);
 void function();
};

polynomial* polynomial::read_polynomial(polynomial *p)
{
 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;
 }
}

void polynomial::add_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
  {
   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;
 p=read_polynomial(p);
 cout<<“\nThe entered polynomial is ::\n”;
 print_polynomial(p);
 cout<<endl<<endl;

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

 cout<<“The sum of the given two polynomials is P+Q ::\n”;
 add_polynomials(p,q);
 
 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”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s