Saturday, February 7, 2015

Adding two mathematical function using linked list in C++

#include<iostream>
#include<cstdlib>
using namespace std;
struct node
{
int co;
int po;
node *next;
};
void display(node *s)
{
node *p;
p=s;
while(p!=NULL)
{
cout<<p->co<<"*("<<p->po<<")+";
p=p->next;
}

}
node *add(node *s)
{
node *t,*p,*q,*r,*e,*z,*f;int ctr=0;
r=NULL;
e=r;
p=s;
while(p!=NULL)
{
t=new(node);
t->po=p->po;
t->co=p->co;
t->next=NULL;
q=p;
if(q!=NULL)
q=q->next;
if(q==NULL);
else
{

while(q!=NULL)
{
if(p->po==q->po)
{
t->co+=q->co;
if(p->next->po==q->po)
p->next=p->next->next;
else if(p->next==NULL)
break;
else
{
f=p;
while(f->next->po!=q->po)
f=f->next;
f->next=f->next->next;
break;
}
}
if(q->next==NULL)
break;
else
q=q->next;
}
}
if(r==NULL)
{
r=t;
e=r;
}
else
{
r->next=t;
r=t;
}
p=p->next;
}
return e;
}
node *sub(node *s1,node *s2)
{
node *t,*p,*q,*r,*e,*s,*z,*f,*w;int ctr=0;
r=NULL;
e=r;
s=s1;
w=s2;
while(s2!=NULL)
{
s2->co*=-1;
s2=s2->next;
}
while(s1->next!=NULL)
s1=s1->next;
s1->next=w;
p=s;
while(p!=NULL)
{
t=new(node);
t->po=p->po;
t->co=p->co;
t->next=NULL;
q=p;
if(q!=NULL)
q=q->next;
if(q==NULL);
else
{

while(q!=NULL)
{
if(p->po==q->po)
{
t->co+=q->co;
if(p->next->po==q->po)
p->next=p->next->next;
else if(p->next==NULL)
break;
else
{
f=p;
while(f->next->po!=q->po)
f=f->next;
f->next=f->next->next;
break;
}
}
if(q->next==NULL)
break;
else
q=q->next;
}
}
if(r==NULL)
{
r=t;
e=r;
}
else
{
r->next=t;
r=t;
}
p=p->next;
}
return e;
}
node *mul(node *s1,node *s2)
{
node *l,*t,*s;
t=NULL;
s=s2;
while(s1!=NULL)
{
s2=s;
while(s2!=NULL)
{
if(t==NULL)
{
t=new(node);
t->po=(s1->po)+(s2->po);
t->co=(s1->co)*(s2->co);
t->next=NULL;
}
else
{
l=new(node);
l->po=(s1->po)+(s2->po);
l->co=(s1->co)*(s2->co);
l->next=t;
t=l;
}
s2=s2->next;
}
s1=s1->next;
}
t=add(l);
return t;
}
int main()
{
node *s,*l,*p1,*p2,*t,*s1,*s2;
int i,d,k;
l=new(node);
p1=l;
    cout<<"Start entering the Coeff. & Power.....Enter -1 for aborting...\n";
    cin>>d;
    cin>>k;
    l->co=d;
    l->po=k;
    l->next=NULL;
    cin>>d;
    cin>>k;
    while(d!=-1)
     {t=new(node);
      t->co=d;
      t->po=k;
      t->next=NULL;
      l->next=t;
      l=t;
      cin>>d;
      cin>>k;
     }
    display(p1);
    l=new(node);
    p2=l;
    cout<<"\nStart entering the Coeff. & Power.....Enter -1 for aborting...\n";
    cin>>d;
    cin>>k;
    l->co=d;
    l->po=k;
    l->next=NULL;
    cin>>d;
    cin>>k;
    while(d!=-1)
     {t=new(node);
      t->co=d;
      t->po=k;
      t->next=NULL;
      l->next=t;
      l=t;
      cin>>d;
      cin>>k;
     }
    display(p2);
    s2=p2;
    s1=p1;
    l=p1;
    t=p1;
    while(l->next!=NULL)
    l=l->next;
    k=l->co;
    d=l->po;
    while(s1->next!=NULL)
    s1=s1->next;
    s1->next=s2;
    cout<<"\nPress...\n1 for Addition\n2 for Subtraction\n3 for Multiplication\n";
    cin>>k;
    switch(k)
    {
    case 1:s=add(p1);
      display(s);
      break;
    case 2:while(t->co!=k && t->po!=d)
          t=t->next;
          t->next=NULL;
  s=sub(p1,p2);
      display(s);
      break;
    case 3:while(t->co!=k && t->po!=d)
          t=t->next;
          t->next=NULL;
  s=mul(p1,p2);
      display(s);
      break;
    }
}

No comments:

Post a Comment

Contributors

Translate