Welcome to W3Courses
 Like Us on Facebook

Polynomial Addition Source Code in C++

The following code adds two polynomials

#include <iostream.h>

typedef struct node
{
 int coef;       // coeffecient
 char var;   //variable
 char exp;  //exponent sign
 int power; //power
 struct node *next;
}list_node;
typedef list_node *list;

list cons(int a, char b, char c, int d, list L)          // to construct the list
{
      list_node *insert;      
      insert=new(struct node);
      insert->coef=a;
 insert->var=b;
 insert->exp=c;
 insert->power=d;
      insert->next=L;
 return(insert);
}

list sort(list L)        // this sorts the list in decreasing order of the power of polynomials
{
 struct node *p=NULL, *q=NULL;
 list R=NULL, K=NULL;
 p=L->next;
 R=cons(L->coef,L->var,L->exp,L->power,NULL);
 while(p!=NULL)
 {
  if(p->power>=R->power)
   R=cons(p->coef,p->var,p->exp,p->power,R);
  else
  {
   q=R;
start:
   if(q->next==NULL)
    goto second;
   else if(q->next->power>p->power)
   {
    q=q->next;
    goto start;
   }
   else
    goto second;
   
second:
   if(q->next==NULL)
   {
    K=cons(p->coef,p->var,p->exp,p->power,NULL);
    q->next=K;
    K->next=NULL;
   }
   else
   {
    K=cons(p->coef,p->var,p->exp,p->power,NULL);
    K->next=q->next;
    q->next=K;
    
   }
   
   
  }
  p=p->next;
 }
 return R;
}

list search(list L)               // this adds two polynomial with same power
{
 struct node *p=NULL,*q=NULL;
 p=L;
 while(p!=NULL)
 {
  q=p->next;
  while(q!=NULL)
  {
   if(q->power==p->power)
   {
    p->coef=p->coef+q->coef;
    p->next=p->next->next;
   }
   q=q->next;
  }
  p=p->next;
 }
 L=sort(L);
 return L;
}

list read()                          // this reads in the list of polynomial
{
 list L=NULL; int a; char b; char c; int d;
 cin>>a>>b>>c>>d;
 L = cons(a,b,c,d,L);
 L->next=NULL;
 while(cin.peek()!='\n')
 {
  cin>>a>>b>>c>>d;
  L = cons(a,b,c,d,L);
 }
 return L;
}

list add(list A, list B)                       // this adds the two polynomial and returns the sum
{
 list p, q, R=NULL;
 p=A;
 q=B;
 while((p!=NULL) || (q!=NULL))
 {
  if(p==NULL)
  {
   R=cons(q->coef,q->var,q->exp,q->power,R);
   q=q->next;
  }
  else if(q==NULL)
  {
    R=cons(p->coef,p->var,p->exp,p->power,R);
    p=p->next;
  }
  else if(q->power>p->power)
  {
   R=cons(q->coef,q->var,q->exp,q->power,R);
   q=q->next;
  }
  else if(p->power>q->power)
  {
    R=cons(p->coef,p->var,p->exp,p->power,R);
    p=p->next;
  }
  else
  {
   if(p->power==q->power)
   {
    R=cons(p->coef+q->coef,q->var,q->exp,q->power,R);
    q=q->next;p=p->next;
   }
  }
 }
 return R;
}

void print(list P)                    // this prints the list of polynomials
{
 list L = P;
 if (L==NULL)
  cout<<"0";
 else
 {
  if(L)
  {
   if(L->coef!=0)
    cout<<L->coef<<L->var<<L->exp<<L->power<<" ";
   L=L->next;
  }
  while(L!=NULL)
  {
   if(L->coef>0)
    cout<<'+'<<L->coef<<L->var<<L->exp<<L->power<<" ";
   else if(L->coef<0)
    cout<<L->coef<<L->var<<L->exp<<L->power<<" ";
   L=L->next;
  }
 }
}

void main()

 list L,M,Result;
 cout<<"Welcome to the polynomial addition"<<endl;
 cout<<"Note: polynomials should be in the form of 5x^2+1x^1-20x^0"<<endl;
 cout<<"Please enter the first polynomial"<<endl;
 L=read();
 cout<<"Please enter the second polynomial"<<endl;
 M=read();
 cout<<endl;
 L = sort(L);
 cout<<"The first polynomial is:"<<'\t';
 print(L);
 cout<<endl;
 M = sort(M);
 cout<<"The Second polynomial is:"<<'\t';
 print(M);
 cout<<endl;
 cout<<"The sum of two polynomial is:"<<'\t';
 Result=add(L,M);
 Result=sort(Result);
 Result=search(Result);
 print(Result);
 cout<<endl;
}

Share