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(" ");
}
{
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 ()
{
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:");
printf("\n\nProduct is:");
product=multiply(p1,p2);
display (product);
getch ();
}``````
2
Contributors
1
2
Views
8 Years
Discussion Span
Last Post by Lerner

First, welcome to the board. Then not necessarily in order of importance:
1) Your syntax is more C than C++. You might get better response posting on the C board section of this site rather than the C++ board.
2) Thanks for using code tags when posting but
3) Hopefuly your indentation practices in real life are better than what is presented in the post.
4) main() should return type int, not type void.
5) the last two terms of the 4th line of the jpg image don't make sense if the polynomial listed is supposed to be the product of the first two polynomials in the image.
6) It looks as though you are using a list to represent terms of a polynomial. Therefore, to eliminate terms from the list when adding like tems you will need to be able to delete any given node from the list.
7) One way to display a polynomial from term with highest variable exponent to lowest is to sort the list into desried order and then print the list, though there are other ways, too.
8) using a list to represent a polynomial isn't the only way to represent a polynomial. If that's what your instructions indicate you need to do, then so be it. Another way to represent polynomials, particularly if there is only a single variable, is to use an array of cofficients with each index of the array used as the exponent of the coefficient. That eliminates the need to sort a list and can simplify (in my mind at least) the addition of like terms.
9) If you must use a list to represent polynomials and you wish to add like terms to simplify the polynomial you can use a nested loop. The outer loop starts at the front of the list. Determine the exponent of that term. Then use an inner loop to search the all terms to right of this term for any other terms with that exponent. If any are found add the exponents of the two terms storing the sum in one of the two terms and delete the other term. I would strongly advise deleteing the term you find during the search, not the one you started out with. Then use the outer loop to go to the next term in the list and repeat the inner loop. Once you have come to the end of the list using the outer loop all terms should have unique exponents. Then you can sort the list or do whatever you want with it to display the result.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.