Hello, below is my polynomial code. I had to add and multiply 2 polynomials.
1) All i have to do more is to sort decreasingly the adding() and product() functions. Eg: 4x^6 + 6x^4 + 2x^2... etc.
2) Second, to add the coefficient with the similar exponents, same in the adding() and product() functions.
3) I need to know how to delete the terms of the 2nd polinomial in the product() function. See the attached image.
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct nod
{
int co,ex;
struct nod *urm;
};
struct nod *start,*ptrn,*ptrp;
typedef struct nod *polinom;
polinom get_pol1 ();
polinom get_pol2 ();
void afiseaza ();
polinom add (polinom p1, polinom p2);
polinom multiply (polinom p1, polinom p2);
polinom get_pol ()
{
char c='x';
ptrn=ptrp=start=NULL;
while (c=='x' || c=='X')
{
ptrn=(struct nod*)malloc(sizeof(struct nod));
ptrp->urm=ptrn;
if (start==NULL)
start=ptrn;
ptrp=ptrn;
printf("\nFirst coefficient:");
scanf("%d",&ptrn->co);
printf("Exponent:");
scanf("%d",&ptrn->ex);
ptrn->urm=NULL;
printf ("\nAdd x for more nodes:");
scanf(" %c",&c);
}
return start;
}
void display (polinom start)
{
struct nod *ptr;
ptr=start;
while (ptr!=NULL)
{
printf(" %d X^ %d + ",ptr->co,ptr->ex);
ptr=ptr->urm;
}
printf(" ");
}
polinom add(polinom p1,polinom p2)
{
struct nod *p1ptr,*p2ptr;
int co,ex;
ptrn=ptrp=start=NULL;
p1ptr=p1;
p2ptr=p2;
while (p1ptr!=NULL && p2ptr!=NULL)
{
if (p1ptr->ex==p2ptr->ex)
{
co=p1ptr->co+p2ptr->co;
ex=p1ptr->ex;
p1ptr=p1ptr->urm;
p2ptr=p2ptr->urm;
}
else if (p1ptr->ex>p2ptr->ex)
{
co=p1ptr->co;
ex=p1ptr->ex;
p1ptr=p1ptr->urm;
}
else if (p1ptr->ex<p2ptr->ex)
{
co=p2ptr->co;
ex=p2ptr->ex;
p2ptr=p2ptr->urm;
}
ptrn=(struct nod*)malloc(sizeof(struct nod));
if (start==NULL)
start=ptrn;
ptrn->co=co;
ptrn->ex=ex;
ptrn->urm=NULL;
ptrp->urm=ptrn;
ptrp=ptrn;
}
if (p1ptr==NULL)
{
while (p2ptr!=NULL)
{
co=p2ptr->co;
ex=p2ptr->ex;
p2ptr=p2ptr->urm;
ptrn=(struct nod*)malloc(sizeof(struct nod));
if (start==NULL)
start=ptrn;
ptrn->co=co;
ptrn->ex=ex;
ptrn->urm=NULL;
ptrp->urm=ptrn;
ptrp=ptrn;
}
}
else if (p2ptr==NULL)
{
while (p1ptr!=NULL)
{
co=p1ptr->co;
ex=p1ptr->ex;
p1ptr=p1ptr->urm;
ptrn=(struct nod*)malloc(sizeof(struct nod));
if (start==NULL)
start=ptrn;
ptrn->co=co;
ptrn->ex=ex;
ptrn->urm=NULL;
ptrp->urm=ptrn;
ptrp=ptrn;
}
}
return start;
}
polinom multiply(polinom p1,polinom p2)
{
struct nod *p1ptr,*p2ptr;
int co,ex,k;
ptrn=ptrp=start=NULL;
p1ptr=p1;
p2ptr=p2;
while (p1ptr!=NULL )
{
while(p2ptr!=NULL)
{
co=p2ptr->co*p1ptr->co;
ex=p2ptr->ex+p1ptr->ex;
p2ptr=p2ptr->urm;
ptrn=(struct nod*)malloc(sizeof(struct nod));
if (start==NULL)
start=ptrn;
ptrn->co=co;
ptrn->ex=ex;
ptrn->urm=NULL;
ptrp->urm=ptrn;
ptrp=ptrn;
}
p1ptr=p1ptr->urm;
p2ptr=p2;
}
if (p1ptr==NULL)
{
while (p2ptr!=NULL)
{
co=p2ptr->co;
ex=p2ptr->ex;
p2ptr=p2ptr->urm;
ptrn=(struct nod*)malloc(sizeof(struct nod));
if (start==NULL)
start=ptrn;
ptrn->co=co;
ptrn->ex=ex;
ptrn->urm=NULL;
ptrp->urm=ptrn;
ptrp=ptrn;
}
}
else if (p2ptr==NULL)
{
while (p1ptr!=NULL)
{
co=p1ptr->co;
ex=p1ptr->ex;
p1ptr=p1ptr->urm;
ptrn=(struct nod*)malloc(sizeof(struct nod));
if (start==NULL)
start=ptrn;
ptrn->co=co;
ptrn->ex=ex;
ptrn->urm=NULL;
ptrp->urm=ptrn;
ptrp=ptrn;
}
}
return start;
}
void main ()
{
polinom p1,p2,adding,product;
clrscr ();
printf("\n Polinom 1: ");
p1=get_pol ();
printf("\n\n Polinom 2: ");
p2=get_pol ();
clrscr ();
printf("\n First polinomial:");
afiseaza (p1);
printf("\n\n Second polinomial:");
afiseaza (p2);
printf("\n\n Sum is:");
adding=add(p1,p2);
display (adding);
printf("\n\nProduct is:");
product=multiply(p1,p2);
display (product);
getch ();
}