0% found this document useful (0 votes)
5 views3 pages

Polynomial Addition

Polynomial addition

Uploaded by

fanfare642
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views3 pages

Polynomial Addition

Polynomial addition

Uploaded by

fanfare642
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

#include<iostream>

#include<stdio.h>
#include<stdlib.h>
using namespace std;
struct polynomial
{
float coef;
int exp;
struct polynomial *next;
};
typedef struct polynomial poly;
poly *start,*first,*sec;
class list
{
public:
poly * create();
poly * add(poly *,poly *);
void display(poly *);
list()
{
start=NULL;
}
};
int main()
{
int ch;
list l;

do
{
cout<<"\n 1.create first";
cout<<"\n 2.create second";
cout<<"\n 3.add";
cout<<"\n 4.display";
cout<<"\n 5.exit";
cout<<"\n enter your choice";
cin>>ch;
switch(ch)
{
case 1:first=l.create();
continue;
case 2:sec=l.create();
continue;
case 3:start=l.add(first,sec);

continue;
case 4:l.display(start);
continue;
case 5:exit(0);
}
}
while(ch!=5);
return 0;
}
poly * list :: create()
{
int c=1;
poly *t,*temp,*x;
temp=new poly;
t=x=temp;
cout<<"\n enter polynomial is in descending order";
do
{
cout<<"\n enter coefficient and exponent";
cin>>temp->coef>>temp->exp;
cout<<"\n do you want more (type 1 for yes and 0 for no)\n";
cin>>c;
temp=new poly;
t->next=temp;
t=temp;
}
while(c==1);
temp->next=NULL;
return(x);
}
poly * list :: add(poly *first,poly *sec)
{
poly *p1,*p2,*t,*temp;
p1=first;
p2=sec;
temp=new poly;
start=temp;
while(p1!=NULL && p2!=NULL)
{
t=temp;
if(p1->exp==p2->exp)
{
temp->coef=p1->coef+p2->coef;
temp->exp=p1->exp;
p1=p1->next;
p2=p2->next;
}
else if(p1->exp<p2->exp)
{
temp->coef=p2->coef;
temp->exp=p2->exp;
p2=p2->next;
}
else if(p1->exp > p2->exp)
{
temp->coef=p1->coef;
temp->exp=p1->exp;
p1=p1->next;
}
temp=new poly;
t->next=temp;
}
while(p1!=NULL)
{
t=temp;
temp=new poly;

temp->coef=p1->coef;
temp->exp=p1->exp;
p1=p1->next;
t->next=temp;
}
while(p2!=NULL)
{
t=temp;
temp=new poly;

temp->coef=p2->coef;
temp->exp=p2->exp;
p2=p2->next;
t->next=temp;
}
temp->next=NULL;
return(start);
}
void list :: display(poly * start)
{
poly * temp;
temp=start;
while(temp->next!=NULL)
{
cout<<temp->coef<<"x"<<temp->exp;
cout<<"+"<<"\t";
temp=temp->next;
}
}

You might also like