Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

//Addition of Two Polynomial

#include <iostream.h>
#include <iomanip.h>
#include <conio.h>

struct poly{
int coeff;
int pow;
poly *next;
};

class add2poly
{
poly *poly1, *poly2, *poly3;
public:
add2poly(){poly1=poly2=poly3=NULL;}
void addpoly();
void display();
};

void add2poly :: addpoly(){


int i,p;
poly *newl=NULL,*end=NULL;
cout<<"Enter highest power for x\n";
cin>>p;
//Read first poly
cout<<"\nFirst Polynomial\n";
for(i=p;i>=0;i--)
{
newl=new poly;
newl->pow=p;
cout<<"Enter Co-efficient for degree"<<i<<":: ";
cin>>newl->coeff;
newl->next=NULL;
if(poly1==NULL)
poly1=newl;
else
end->next=newl;
end=newl;
}

//Read Second poly


cout<<"\n\nSecond Polynomial\n";
end=NULL;
for(i=p;i>=0;i--)
{
newl=new poly;
newl->pow=p;
cout<<"Enter Co-efficient for degree"<<i<<":: ";
cin>>newl->coeff;
newl->next=NULL;
if(poly2==NULL)
poly2=newl;
else
end->next=newl;
end=newl;
}

//Addition Logic
poly *p1=poly1,*p2=poly2;
end=NULL;
while(p1 !=NULL && p2!=NULL){
if(p1->pow == p2->pow){
newl=new poly;
newl->pow=p--;
newl->coeff=p1->coeff + p2->coeff;
newl->next=NULL;
if(poly3==NULL)
poly3=newl;
else
end->next=newl;
end=newl;
}
p1=p1->next;
p2=p2->next;
}
}

void add2poly :: display(){


poly *t=poly3;
cout<<"\n\nAnswer after addition is : ";
while(t!=NULL){
cout.setf(ios::showpos);
cout<<t->coeff;
cout.unsetf(ios::showpos);
cout<<"X"<<t->pow;
t=t->next;
}
}

void main(){
clrscr();
add2poly obj;
obj.addpoly();
obj.display();
getch();
}
#include<stdio.h>
#include<conio.h>
struct polys
{
int coff[6];
int exp[6];

};
struct polymm
{
int coff[8];
int exp[8];
int cout;

}p3;
struct polys p1,p2;
void polym();
void polym()
{
int val;
printf("************Addition of two Polynomial************************\n");
printf("Enter the Cofficent and Exp for Polynomial 1\n");
for(int a=1;a<=3;a++)
{
printf("Enter the Exp of Polynomial1\n");
scanf("%d",&p1.exp[a]);
printf("Enter the cofficent of Polynomial1\n");
scanf("%d",&p1.coff[a]);
}
printf("Enter the Cofficent and Exp for Polynomial 2\n");
for(int b=1;b<=3;b++)
{
printf("Enter the Exp of Polynomial1\n");
scanf("%d",&p2.exp[b]);
printf("Enter the cofficent of Polynomial1\n");
scanf("%d",&p2.coff[b]);
}
printf("Display Polynomial one \n");
for(int c=1;c<=3;c++)
{
printf("+%d^%d",p1.exp[c],p1.coff[c]);
}
printf("\n");
printf("Display Polynomial two\n");
for(int d=1;d<=3;d++)
{
printf("+%d^%d",p2.exp[d],p2.coff[d]);
}
printf("\n");
printf("Polynomial After Adddition is\n");
for(int f=1;f<=3;f++)
{
for(int g=1;g<=3;g++)
{
if(p1.coff[f]==p2.coff[g])
{
p3.coff[++p3.cout]=p1.coff[f];
p3.exp[p3.cout]=p1.exp[f]+p2.exp[g];
}
}
if(p3.coff[f]==0)
{
p3.coff[++p3.cout]=p1.coff[f];
p3.exp[p3.cout]=p1.exp[f];
}
}

int fq=1;
for(int k=1;k<=3;k++)
{
fq=1;
for(int l=1;l<=3;l++)
{
if(p2.coff[k]==p1.coff[l])
{
fq=++fq;
}
}
if(fq!=2)
{
p3.coff[++p3.cout]=p2.coff[k];
p3.exp[p3.cout]=p2.exp[k];
}
}

printf("%d",p3.cout);
for(int h=1;h<=p3.cout;h++)
{
printf("+%d^%d",p3.exp[h],p3.coff[h]);
}
}
void main()
{
polym();
getch();
}

You might also like