C++ program to multiply two polynomials.
C++ program to multiply two polynomials using array
#include<iostream>
using namespace std;
int mulpoly(int (&a)[10][2],int (&b)[10][2],int (&c)[100][2],int n1,int n2)
{
int k=0;
for(int i=0;i<n2;i++)
{
for(int j=0;j<n1;j++,k++)
{
c[k][0]=a[j][0]*b[i][0];
c[k][1]=a[j][1]+b[i][1];
}
}
for(int i=0;i<k;i++)
{
for(int j=i+1;j<k;j++)
{
if(c[i][1]==c[j][1])
{
c[i][0]+=c[j][0];
for(int l=j;l<k-1;l++)
{
c[l][0]=c[l+1][0];
c[l][1]=c[l+1][1];
}
k--;
}
}
}
return k;
}
void print(int (&d)[10][2],int n)
{
for(int i=0;i<n-1;i++)
cout<<d[i][0]<<"x^"<<d[i][1]<<" + ";
cout<<d[n-1][0]<<"x^"<<d[n-1][1]<<endl<<endl;
}
void getpoly(int (&d)[10][2],int &n)
{
cout<<"How many terms in polynomial ";
cin>>n;
cout<<"Enter coefficient and exponent in decreasing order"<<endl;
for(int i=0;i<n;i++)
cin>>d[i][0]>>d[i][1];
}
int main()
{
int a[10][2],b[10][2],c[100][2],n1,n2,n3;
getpoly(a,n1);
print(a,n1);
getpoly(b,n2);
print(b,n2);
n3=mulpoly(a,b,c,n1,n2);
cout<<"Multiplication of two polynomials"<<endl;
for(int i=0;i<n3-1;i++)
cout<<c[i][0]<<"x^"<<c[i][1]<<" + ";
cout<<c[n3-1][0]<<"x^"<<c[n3-1][1]<<endl<<endl;
return 0;
}
using namespace std;
int mulpoly(int (&a)[10][2],int (&b)[10][2],int (&c)[100][2],int n1,int n2)
{
int k=0;
for(int i=0;i<n2;i++)
{
for(int j=0;j<n1;j++,k++)
{
c[k][0]=a[j][0]*b[i][0];
c[k][1]=a[j][1]+b[i][1];
}
}
for(int i=0;i<k;i++)
{
for(int j=i+1;j<k;j++)
{
if(c[i][1]==c[j][1])
{
c[i][0]+=c[j][0];
for(int l=j;l<k-1;l++)
{
c[l][0]=c[l+1][0];
c[l][1]=c[l+1][1];
}
k--;
}
}
}
return k;
}
void print(int (&d)[10][2],int n)
{
for(int i=0;i<n-1;i++)
cout<<d[i][0]<<"x^"<<d[i][1]<<" + ";
cout<<d[n-1][0]<<"x^"<<d[n-1][1]<<endl<<endl;
}
void getpoly(int (&d)[10][2],int &n)
{
cout<<"How many terms in polynomial ";
cin>>n;
cout<<"Enter coefficient and exponent in decreasing order"<<endl;
for(int i=0;i<n;i++)
cin>>d[i][0]>>d[i][1];
}
int main()
{
int a[10][2],b[10][2],c[100][2],n1,n2,n3;
getpoly(a,n1);
print(a,n1);
getpoly(b,n2);
print(b,n2);
n3=mulpoly(a,b,c,n1,n2);
cout<<"Multiplication of two polynomials"<<endl;
for(int i=0;i<n3-1;i++)
cout<<c[i][0]<<"x^"<<c[i][1]<<" + ";
cout<<c[n3-1][0]<<"x^"<<c[n3-1][1]<<endl<<endl;
return 0;
}
OUTPUT
C++ program to multiply two polynomials using singly linear linked list
#include<iostream>
#include<conio.h>
using namespace std;
struct links
{
int exp,coef;
links *next;
};
class linkedlist
{
links *start1,*start2,*start3;
public:
void create(links **);
void mul();
void display(links**);
links** getstart1()
{
return &start1;
}
links** getstart2()
{
return &start2;
}
links** getstart3()
{
return &start3;
}
};
void linkedlist::mul()
{
links *temp3,*temp1,*temp2;
int flag=1;
temp1=start1;
temp2=start2;
while(temp2!=NULL)
{
while(temp1!=NULL)
{
if(flag)
{
start3=new links;
start3->coef=temp2->coef*temp1->coef;
start3->exp=temp2->exp+temp1->exp;
temp3=start3;
flag=0;
}
else
{
temp3->next=new links;
temp3=temp3->next;
temp3->coef=temp2->coef*temp1->coef;
temp3->exp=temp2->exp+temp1->exp;
}
temp1=temp1->next;
}
temp1=start1;
temp2=temp2->next;
}
temp3->next=NULL;
temp1=temp2=start3;
temp3=temp2->next;
while(temp2->next!=NULL)
{
while(temp3!=NULL)
{
if(temp3->exp==temp2->exp)
{
temp2->coef+=temp3->coef;
temp1->next=temp3->next;
delete temp3;
}
temp1=temp3;
temp3=temp3->next;
}
temp1=temp2=temp2->next;
temp3=temp2->next;
}
}
void linkedlist::create(links **start)
{
int exp,coef;
links *temp;
char ch;
cout<<"Enter the polynomial in descending order of exponent "<<endl;
cout<<"Enter the coefficient and Exponent of the term ";
cin>>coef>>exp;
temp=new links;
temp->coef=coef;
temp->exp=exp;
temp->next=NULL;
*start=temp;
cout<<"Want to add more terms (Y/N)"<<endl;
ch=getch();
while(ch!='n'&&ch!='N')
{
cout<<"Enter the coefficient and Exponent of the term ";
cin>>coef>>exp;
temp->next=new links;
temp=temp->next;
temp->coef=coef;
temp->exp=exp;
temp->next=NULL;
cout<<"Want to add more terms (Y/N)"<<endl;
ch=getch();
}
}
void linkedlist::display(links**start)
{
links *temp=*start;
while(temp->next!=NULL)
{
cout<<temp->coef<<"x^"<<temp->exp<<" + ";
temp=temp->next;
}
cout<<temp->coef<<"x^"<<temp->exp;
}
int main()
{
linkedlist multiplylist;
cout<<"Create First polynomial list "<<endl;
multiplylist.create(multiplylist.getstart1());
cout<<"Create a Second polynomial list "<<endl;
multiplylist.create(multiplylist.getstart2());
cout<<"\nFirst polynomial \n";
multiplylist.display(multiplylist.getstart1());
cout<<"\nSecond polynomial \n";
multiplylist.display(multiplylist.getstart2());
multiplylist.mul();
cout<<"\nMultiplication of two polynomial \n";
multiplylist.display(multiplylist.getstart3());
return 0;
}
#include<conio.h>
using namespace std;
struct links
{
int exp,coef;
links *next;
};
class linkedlist
{
links *start1,*start2,*start3;
public:
void create(links **);
void mul();
void display(links**);
links** getstart1()
{
return &start1;
}
links** getstart2()
{
return &start2;
}
links** getstart3()
{
return &start3;
}
};
void linkedlist::mul()
{
links *temp3,*temp1,*temp2;
int flag=1;
temp1=start1;
temp2=start2;
while(temp2!=NULL)
{
while(temp1!=NULL)
{
if(flag)
{
start3=new links;
start3->coef=temp2->coef*temp1->coef;
start3->exp=temp2->exp+temp1->exp;
temp3=start3;
flag=0;
}
else
{
temp3->next=new links;
temp3=temp3->next;
temp3->coef=temp2->coef*temp1->coef;
temp3->exp=temp2->exp+temp1->exp;
}
temp1=temp1->next;
}
temp1=start1;
temp2=temp2->next;
}
temp3->next=NULL;
temp1=temp2=start3;
temp3=temp2->next;
while(temp2->next!=NULL)
{
while(temp3!=NULL)
{
if(temp3->exp==temp2->exp)
{
temp2->coef+=temp3->coef;
temp1->next=temp3->next;
delete temp3;
}
temp1=temp3;
temp3=temp3->next;
}
temp1=temp2=temp2->next;
temp3=temp2->next;
}
}
void linkedlist::create(links **start)
{
int exp,coef;
links *temp;
char ch;
cout<<"Enter the polynomial in descending order of exponent "<<endl;
cout<<"Enter the coefficient and Exponent of the term ";
cin>>coef>>exp;
temp=new links;
temp->coef=coef;
temp->exp=exp;
temp->next=NULL;
*start=temp;
cout<<"Want to add more terms (Y/N)"<<endl;
ch=getch();
while(ch!='n'&&ch!='N')
{
cout<<"Enter the coefficient and Exponent of the term ";
cin>>coef>>exp;
temp->next=new links;
temp=temp->next;
temp->coef=coef;
temp->exp=exp;
temp->next=NULL;
cout<<"Want to add more terms (Y/N)"<<endl;
ch=getch();
}
}
void linkedlist::display(links**start)
{
links *temp=*start;
while(temp->next!=NULL)
{
cout<<temp->coef<<"x^"<<temp->exp<<" + ";
temp=temp->next;
}
cout<<temp->coef<<"x^"<<temp->exp;
}
int main()
{
linkedlist multiplylist;
cout<<"Create First polynomial list "<<endl;
multiplylist.create(multiplylist.getstart1());
cout<<"Create a Second polynomial list "<<endl;
multiplylist.create(multiplylist.getstart2());
cout<<"\nFirst polynomial \n";
multiplylist.display(multiplylist.getstart1());
cout<<"\nSecond polynomial \n";
multiplylist.display(multiplylist.getstart2());
multiplylist.mul();
cout<<"\nMultiplication of two polynomial \n";
multiplylist.display(multiplylist.getstart3());
return 0;
}
No comments:
Post a Comment