HOME
/*PROGRAM
FOR ADDING,SUBTRACTION,
MULTIPLYCATION OF 2 POLINOMIALS*/
#include<iostream.h>
#include<stdlib.h>
int
d,i,y;
static
int z=0;//GLOBAL
VARIABLES
class
poly//CLASS
DECLARATION
{
private:
float coeff;
int degree;
poly *next;
public:
void getdata(float ,int );
void add();
void show(poly *);
void list_add(float ,poly *);
void sub();
void list_sub(float ,poly *);
void mul();
void list_mul(float,int);
}
*head1,*head2,*head3,
*head,*head4,*head5,*head6;
//FUNCTION
FOR READING DATA
void
poly::getdata(float c,int d)
{
poly
*n,*temp;
n=new(poly);
n->degree=d;
n->coeff=c;
n->next=NULL;
if(head==NULL)
head=n;
else
{
temp=head;
while(temp->next!=NULL)
temp=temp->next;
temp->next=n;
}
if(i==0&&z==0)
{
head1=head;
head=NULL;
temp=NULL;
z++;
}
else
{
head2=head;
}
}
void
poly::add()
//FUNCTION
FOR FINDING THE ADDITION
{
poly
*t1,*t2;
float
x;
t1=head1;
t2=head2;
while(t1!=NULL||t2!=NULL)
{
if(t1->degree>t2->degree)
{
x=t1->coeff;
list_add(x,t1);
t1=t1->next;
}
else
if(t1->degree<t2->degree)
{
x=t2->coeff;
list_add(x,t2);
t2=t2->next;
}
else
{
x=t1->coeff+t2->coeff;
list_add(x,t1);
t1=t1->next;
t2=t2->next;
}
}
}
void
poly::list_add(float ele,poly *t)
/*FUNCTION
FOR CREATING
LINKEDLIST FOR ADDED COEFFICIENTS*/
{
poly
*t3,*n1;
n1=new(poly);
n1->coeff=ele;
n1->degree=t->degree;
n1->next=NULL;
if(head3==NULL)
head3=n1;
else
{
t3=head3;
while(t3->next!=NULL)
t3=t3->next;
t3->next=n1;
}
}
//FUNCTION
FOR SUBTRACTION
void
poly::sub()
{
poly
*t1,*t2;
float
x;
t1=head1;
t2=head2;
while(t1!=NULL||t2!=NULL)
{
if(t1->degree>t2->degree)
{
x=t1->coeff;
list_sub(x,t1);
t1=t1->next;
}
else
if(t1->degree<t2->degree)
{
x=t2->coeff;
list_sub(x,t2);
t2=t2->next;
}
else
{
x=t1->coeff-t2->coeff;
list_sub(x,t1);
t1=t1->next;
t2=t2->next;
}
}
}
/*FUNCTION
FOR CREATING LIST
FOR SUBTRACTED COEFFICIENTS*/
void
poly::list_sub(float ele,poly *t)
{
poly
*t3,*n1;
n1=new(poly);
n1->coeff=ele;
n1->degree=t->degree;
n1->next=NULL;
if(head4==NULL)
head4=n1;
else
{
t3=head4;
while(t3->next!=NULL)
t3=t3->next;
t3->next=n1;
}
}
void
poly::mul()
//FUNCTOIN
FOR MULTIPLICATION
{
float
x;
poly
*t1,*t2;
t1=head1;
t2=head2;
int k;
k=t1->degree+t2->degree;
while(k>=0)
{
x=0;
while(t1!=NULL)
{
while(t2!=NULL)
{
if((t1->degree+t2->degree)==k)
x=x+t1->coeff*t2->coeff;
t2=t2->next;
}
t1=t1->next;
t2=head2;
}
list_mul(x,k);
k–;
t1=head1;
t2=head2;
}
}
void
poly::list_mul(float x,int k )
/*FUNCTION
FOR CREATION OF
LIST
FOR MULTIPLIED COEFFICIENTS*/
{
poly
*t,*n;
n=new(poly);
n->coeff=x;
n->next=NULL;
n->degree=k;
if(head5==NULL)
head5=n;
else
{
t=head5;
while(t->next!=NULL)
t=t->next;
t->next=n;
}
}
void
poly::show(poly * head)
//FUNCTION
FOR DISPLAYING OUTPUT
{
poly
*temp=NULL;
if(head==NULL)
cout<<“LIST
EMPTY”;
else
{
temp=head;
while(temp!=NULL)
{
if(temp->coeff>0&&temp->degree>0)
cout<<temp->coeff
<<” x^
“<<temp->degree<<” + “;
else
if(temp->degree==0&&temp->coeff<0)
cout<<“(“<<temp->coeff<<“)”;
else
if(temp->degree==0)
cout<<temp->coeff;
else
if(temp->coeff==0);
else
cout<<“\b”<<“(“<<temp->coeff<<“)”
<<“x^”<<temp->degree<<“-“;
temp=temp->next;
}
}
}
void
main()//MAIN()
FUNCTION STARTS
{
head1=NULL;
head2=NULL;
head3=NULL;
head4=NULL;
head5=NULL;
head6=NULL;
float
c;
poly
p;//OBJECT CREATION
cout<<“PROGRAM
FOR THE
OPERATIONS ON THE POLYNOMIAL”;
cout<<“\n\nENTER
THE DEGREE
OF THE first POLYNOMIAL\n”;
cin>>d;
for(i=d;i>=0;i–)
{
if(i==0)
{
cout<<“\nENTER
THE CONSTANT\t”;
cin>>c;
}
else
{
cout<<“\nENTER
THE
COEFFICIENT OF x^”<<i<<“\t”;
cin>>c;
}
p.getdata(c,i);
}
p.show(head1);
cout<<“\n\nENTER
THE DEGREE
OF THE second POLYNOMIL\n”;
cin>>d;
for(
i=d;i>=0;i–)
{
if(i==0)
{
cout<<“\nENTER
THE CONSTANT\t”;
cin>>c;
}
else
{
cout<<“\nENTER
THE
COEFFICIENT OF x^”<<i<<“\t”;
cin>>c;
}
p.getdata(c,i);
}
p.show(head2);
p.add();
cout<<“\nAFTER
ADDING \n\n”;
p.show(head3);
p.sub();
cout<<“\nAFTER
SUBTRACTNG \n\n”;
p.show(head4);
cout<<“\nAFTER
MULTIPLICATION \n\n”;
p.mul();
p.show(head5);
}
/*OUTPUT:
case
1:
ENTER
THE DEGREE OF THE first POLYNOMIAL
3
ENTER
THE COEFFICIENT OF x^3 1
ENTER
THE COEFFICIENT OF x^2 2
ENTER
THE COEFFICIENT OF x^1 -0.5
ENTER
THE CONSTANT 5.9
1
x^ 3 + 2 x^ 2 +(-0.5)x^1-5.9
ENTER
THE DEGREE OF THE second POLYNOMIL
2
ENTER
THE COEFFICIENT OF x^2 10.5
ENTER
THE COEFFICIENT OF x^1 7
ENTER
THE CONSTANT 0
10.5
x^ 2 + 7 x^ 1 + 0
AFTER
ADDING
1
x^ 3 + 12.5 x^ 2 + 6.5 x^ 1 + 5.9
AFTER
SUBTRACTNG
1
x^ 3 +(-8.5)x^2(-7.5)x^1-5.9
AFTER
MULTIPLICATION
10.5
x^ 5 + 28 x^ 4 + 8.75 x^ 3
+ 58.45 x^ 2 + 41.3 x^ 1 + 0
Press any key to continue
*/
polynomial program is nice
It’s very nice.
i don’t know about topic basically it is useful to CSE students but i am CHEM .anyhow appearance is GOOD
try to add CHEMICAL topics
YOU DID A GOOD JOB
KEEP ON DEVELOPING IT
superb