answersLogoWhite

0

Algorithm to add two polynomials

Updated: 8/11/2023
User Avatar

Wiki User

9y ago

Best Answer

#include<iostream.h>

#include<stdlib.h>

#include<conio.h>

struct poly

{

int coeff;

int x;

int y;

int z;

struct poly * next;

};

class polynomial

{

private :

poly *head;

public:

polynomial():head(NULL)

{

}

void getdata();

void display();

void insert(poly *prv,poly *curr,poly *p);

polynomial operator + (polynomial );

};

polynomial polynomial :: operator +(polynomial px2)

{

polynomial px;

poly *t1,*t2,*t3,*last;

t1 = head;

t2 = px2.head;

px.head = NULL;

while(t1 != NULL && t2 != NULL)

{

t3 = new poly;

t3->next = NULL;

if(t1->x NULL)

{

head->next = node;

node->next = NULL;

}

else

insert(head,head->next,node);

}

}

void polynomial :: display()

{

poly *temp;

temp = head;

cout << endl << "Polynomial :: ";

while(temp != NULL)

{

if(temp->coeff < 0)

cout << " - ";

cout << abs(temp->coeff);

if(temp->x != 0)

cout << "x^" << temp->x;

if(temp->y != 0)

cout << "y^" << temp->y;

if(temp->z != 0)

cout << "z^" << temp->z;

if(temp->next->coeff > 0)

cout << " + ";

temp = temp->next;

}

cout << " = 0";

}

void main()

{

polynomial px1,px2,px3;

clrscr();

px1.getdata();

px2.getdata();

px3 = px1 + px2;

px1.display();

px2.display();

px3.display();

getch();

}

User Avatar

Wiki User

10y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

13y ago

#include "stdio.h"

#include "conio.h"

struct barbie

{

int coff;

int pow;

struct barbie *link;

}*ptr,*start1,*node,*start2,*start3,*ptr1,*ptr2;

typedef struct barbie bar;

int temp1,temp2;

void main()

{

void create(void);

void prnt(void);

void suml(void);

void sort(void);

clrscr();

printf("Enrter the elements of the first poly :");

node = malloc(sizeof (bar));

start1=node;

if (start1==NULL)

{

printf("Unable to create memory.");

getch();

exit();

}

create();

printf("

Enrter the elements of the second poly :");

node = malloc(sizeof (bar));

start2=node;

if (start2==NULL)

{

printf("Unable to create memory.");

getch();

exit();

}

create();

clrscr();

//printing the elements of the lists

printf("The elements of the poly first are :");

ptr=start1;

prnt();

printf("The elements of the poly second are :");

ptr=start2;

prnt();

printf("The first sorted list is :");

ptr=start1;

sort();

ptr=start1;

prnt();

printf("

The second sorted list is :");

ptr=start2;

sort();

ptr=start2;

prnt();

printf("The sum of the two lists are :");

suml();

ptr=start3;

prnt();

getch();

}

/*-----------------------------------------------------------------------------*/

void create()

{

char ch;

while(1)

{

printf("Enter the coff and pow :");

scanf("d",&node->coff,&node->pow);

if (node->pow==0 )

{

ptr=node;

node=malloc(sizeof(bar));

node=NULL;

ptr->link=node;

break;

}

printf("

Do u want enter more coff ?(y/n)");

fflush(stdin);

scanf("%c",&ch);

if (ch=='n' )

{

ptr=node;

node=malloc(sizeof(bar));

node=NULL;

ptr->link=node;

break;

}

ptr=node;

node=malloc(sizeof(bar));

ptr->link=node;

}

}

/*-------------------------------------------------------------------------*/

void prnt()

{ int i=1;

while(ptr!=NULL )

{

if(i!=1)

printf("+ ");

printf(" %dx^%d ",ptr->coff,ptr->pow);

ptr=ptr->link;

i++;

}

//printf(" %d^%d",ptr->coff,ptr->pow);

}

/*---------------------------------------------------------------------------*/

void sort()

{

for(;ptr->coff!=NULL;ptr=ptr->link)

for(ptr2=ptr->link;ptr2->coff!=NULL;ptr2=ptr2->link)

{

if(ptr->pow>ptr2->pow)

{

temp1=ptr->coff;

temp2=ptr->pow;

ptr->coff=ptr2->coff;

ptr->pow=ptr2->pow;

ptr2->coff=temp1;

ptr2->pow=temp2;

}

}

}

/*---------------------------------------------------------------------------*/

void suml()

{

node=malloc (sizeof(bar));

start3=node;

ptr1=start1;

ptr2=start2;

while(ptr1!=NULL && ptr2!=NULL)

{

ptr=node;

if (ptr1->pow > ptr2->pow )

{

node->coff=ptr2->coff;

node->pow=ptr2->pow;

ptr2=ptr2->link; //update ptr list B

}

else if ( ptr1->pow < ptr2->pow )

{

node->coff=ptr1->coff;

node->pow=ptr1->pow;

ptr1=ptr1->link; //update ptr list A

}

else

{

node->coff=ptr2->coff+ptr1->coff;

node->pow=ptr2->pow;

ptr1=ptr1->link; //update ptr list A

ptr2=ptr2->link; //update ptr list B

}

node=malloc (sizeof(bar));

ptr->link=node; //update ptr list C

}//end of while

if (ptr1==NULL) //end of list A

{

while(ptr2!=NULL)

{

node->coff=ptr2->coff;

node->pow=ptr2->pow;

ptr2=ptr2->link; //update ptr list B

ptr=node;

node=malloc (sizeof(bar));

ptr->link=node; //update ptr list C

}

}

else if (ptr2==NULL) //end of list B

{

while(ptr1!=NULL)

{

node->coff=ptr1->coff;

node->pow=ptr1->pow;

ptr1=ptr1->link; //update ptr list B

ptr=node;

node=malloc (sizeof(bar));

ptr->link=node; //update ptr list C

}

}

node=NULL;

ptr->link=node;

}

This answer is:
User Avatar

User Avatar

Wiki User

9y ago

Consider using an array where each index represents the value for the equivalent exponent. So 4x^4 + 5x^3 - 4 would be [-4, 0, 0, 5, 4]. Use a parsing method to separate bases, signs and exponents to properly distribute the elements, such as the String Class.

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

#include

#include

struct poly

{

int coef,exp;

struct poly *link;

};

struct poly *pptr=NULL,*qptr=NULL,*rptr=NULL,*q,*p,*head,*phead,*qhead,*rhead,*a;

struct poly*create();

void disp(struct poly*);

main()

{

/*This Program was created by Hanly.Y.Nadackal For Indiastudychannel.com*/

p=create();

phead=p;

disp(p);

q=create();

qhead=q;

disp(q);

rhead=(struct poly*)malloc(sizeof(struct poly));

rptr=rhead;

rptr->link=NULL;

pptr=phead;

qptr=qhead;

while((pptr!=NULL)&&(qptr!=NULL))

{

if(pptr->exp==qptr->exp)

{

a=(struct poly*)malloc(sizeof(struct poly));

rptr->link=a;

rptr=a;

rptr->coef=pptr->coef+qptr->coef;

rptr->exp=pptr->exp;

rptr->link=NULL;

pptr=pptr->link;

qptr=qptr->link;

}

else if(pptr->exp > qptr->exp)

{

a=(struct poly*)malloc(sizeof(struct poly));

rptr->link=a;

rptr=a;

rptr->coef=pptr->coef;

rptr->exp=pptr->exp;

rptr->link=NULL;

pptr=pptr->link;

}

else if(pptr->exp < qptr->exp)

{

a=(struct poly*)malloc(sizeof(struct poly));

rptr->link=a;

rptr=a;

rptr->coef=qptr->coef;

rptr->exp=qptr->exp;

rptr->link=NULL;

qptr=qptr->link;

}

if((pptr!=NULL)&&(qptr==NULL))

{

a=(struct poly*)malloc(sizeof(struct poly));

rptr->link=a;

rptr=a;

rptr->coef=pptr->coef;

rptr->exp=pptr->exp;

rptr->link=NULL;

pptr=pptr->link;

}

else if((pptr==NULL)&&(qptr!=NULL))

{

a=(struct poly*)malloc(sizeof(struct poly));

rptr->link=a;

rptr=a;

rptr->coef=qptr->coef;

rptr->exp=qptr->exp;

rptr->link=NULL;

qptr=qptr->link;

}

}

rptr=rhead->link;

printf("resultant polynomial is:\n");

while(rptr!=NULL)

{

printf("%d\t%d\n",rptr->coef,rptr->exp);

rptr=rptr->link;

}

}

struct poly*create()

{

int c,e;

struct poly *p,*x;

head=(struct poly*)malloc(sizeof(struct poly));

p=head;

do

{

printf("\n enter the coef & exp : (enter -1 as coef & exp to terminate)\n");

scanf("%d\t%d",&c,&e);

if(c==-1)

{

p=head;

head=head->link;

free(p);

return(head);

}

else

{

x=(struct poly*)malloc(sizeof(struct poly));

x->coef=c;

x->exp=e;

x->link='\0';

p->link=x;

p=x;

}

}while(1);

}

void disp(struct poly*q)

{

if(q==NULL)

printf("list is empty\n");

while(q!=NULL)

{

printf("%d\t%d\n",q->coef,q->exp);

q=q->link;

}

}

Nikita

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

The question is about writing a programme that will accept two polynomial equasions and add them. As an example:

3x^2 + 2x + 1

2x^2 - x +1

to yield:

3x^2 + x + 2

Further, the programme should represent the equasions as a linked list.

If you are still scratching your crust over this, then have a butcher's at the following code which reads two equasions from the command line and adds them together. A sample run is at the bottom. Best of luck.

#include <stdlib.h>

#include <unistd.h>

#include <string.h>

struct ele {

struct ele *next;

int coef;

int power;

};

/* read an equasion and build a linked list

equasion must be of the form nxp {+\-} nxp...

where n is the coefficent of x and p is the power (3x2 meaning

3-x-squared). The equasion must be oredered in decending power

and things like 0x3 can be omitted, and the last (constant)

does not need x0. 1x2 and x2 are the same.

*/

struct ele *build( char *buf )

{

struct ele *expr = NULL;

struct ele *tail = NULL;

struct ele *cep = NULL;

char *tok;

char *tp;

int sign = 1;

tok = strtok( buf, " \t" );

while( tok )

{

if( !isdigit( *tok ) && !*(tok+1) )

{

switch( *tok )

{

case '+': sign = 1; break;

case '-': sign = -1; break;

}

}

else

{

cep = malloc( sizeof( struct ele ) );

memset( cep, 0, sizeof( *cep ) );

if( !tail )

expr = tail = cep;

else

{

tail->next = cep;

tail = cep;

}

if( *tok NULL e1->power > e2->power) )

{

cep->power = e1->power;

cep->coef = e1->coef;

e1 = e1->next;

}

else

{

cep->power = e2->power;

cep->coef = e2->coef;

e2 = e2->next;

}

}

}

return result;

}

void print( struct ele *ep )

{

while( ep )

{

if( ep->coef != 0 )

switch( ep->power )

{

default:

printf( "%+dx^%d ", ep->coef, ep->power );

break;

case 1:

printf( "%+dx ", ep->coef );

break;

case 0:

printf( "%+d", ep->coef );

break;

}

ep = ep->next;

}

printf( "\n" );

}

int main( int argc, char **argv )

{

struct ele *expr1 = NULL;

struct ele *expr2 = NULL;

struct ele *sum = NULL;

expr1 = build( argv[1] );

expr2 = build( argv[2] );

sum = add( expr1, expr2 );

print( expr1 );

print( expr2 );

print( sum );

}

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

To add two polynomials using a linked list, you could build a list of coefficients to the polynomial. Remember that a polynomial is in the form ax0 + bx1 + cx2 + dx3 ... and so on. The linked list would contain the coefficients a, b, c, d, and etc. Since a linked list is variable in length, you could handle polynomials of arbitrary degree, up to the limits of memory. To add two polynomials, simply iterate through the coefficients and add them, a1+a2, b1+b2, c1+c2 and so on.

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

Polynomial Addition Using Linked List 1. #include 2. #include 3. #include 4. struct link{ 5. int coeff; 6. int pow; 7. struct link *next; 8. }; 9. struct link *poly1=NULL,*poly2=NULL,*poly=NULL; 10. void create(struct link *node) 11. { 12. char ch; 13. do 14. { 15. printf("\n enter coeff:"); 16. scanf("%d",&node->coeff); 17. printf("\n enter power:"); 18. scanf("%d",&node->pow); 19. node->next=(struct link*)malloc(sizeof(struct link)); 20. node=node->next; 21. node->next=NULL; 22. printf("\n continue(y/n):"); 23. ch=getch(); 24. } 25. while(ch=='y' ch=='Y'); 26. } 27. void show(struct link *node) 28. { 29. while(node->next!=NULL) 30. { 31. printf("%dx^%d",node->coeff,node->pow); 32. node=node->next; 33. if(node->next!=NULL) 34. printf("+"); 35. } 36. } 37. void polyadd(struct link *poly1,struct link *poly2,struct link *poly) 38. { 39. while(poly1->next && poly2->next) 40. { 41. if(poly1->pow>poly2->pow) 42. { 43. poly->pow=poly1->pow; 44. poly->coeff=poly1->coeff; 45. poly1=poly1->next; 46. } 47. else if(poly1->powpow) 48. { 49. poly->pow=poly2->pow; 50. poly->coeff=poly2->coeff; 51. poly2=poly2->next; 52. } 53. else 54. { 55. poly->pow=poly1->pow; 56. poly->coeff=poly1->coeff+poly2->coeff; 57. poly1=poly1->next; 58. poly2=poly2->next; 59. } 60. poly->next=(struct link *)malloc(sizeof(struct link)); 61. poly=poly->next; 62. poly->next=NULL; 63. } 64. while(poly1->next poly2->next) 65. { 66. if(poly1->next) 67. { 68. poly->pow=poly1->pow; 69. poly->coeff=poly1->coeff; 70. poly1=poly1->next; 71. } 72. if(poly2->next) 73. { 74. poly->pow=poly2->pow; 75. poly->coeff=poly2->coeff; 76. poly2=poly2->next; 77. } 78. poly->next=(struct link *)malloc(sizeof(struct link)); 79. poly=poly->next; 80. poly->next=NULL; 81. } 82. } 83. main() 84. { 85. char ch; 86. do{ 87. poly1=(struct link *)malloc(sizeof(struct link)); 88. poly2=(struct link *)malloc(sizeof(struct link)); 89. poly=(struct link *)malloc(sizeof(struct link)); 90. printf("\nenter 1st number:"); 91. create(poly1); 92. printf("\nenter 2nd number:"); 93. create(poly2); 94. printf("\n1st Number:"); 95. show(poly1); 96. printf("\n2nd Number:"); 97. show(poly2); 98. polyadd(poly1,poly2,poly); 99. printf("\nAdded polynomial:"); 100. show(poly); 101. printf("\n add two more numbers:"); 102. ch=getch(); 103. } 104. while(ch=='y' ch=='Y'); 105. }

Use Ascending order. You can get result. By n_senthil10@Yahoo.com

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

PROGRAM TO ADD TWO POLYNOMIALS USING LINKED LIST USING C

#include <stdio.h>

typedef struct pnode

{

float coef;

int exp;

struct pnode *next;

}p;

p *getnode();

void main()

{

p *p1,*p2,*p3;

p *getpoly(),*add(p*,p*);

void display(p*);

clrscr();

printf("\n enter first polynomial");

p1=getpoly();

printf("\n enter second polynomial");

p2=getpoly();

printf("\nthe first polynomial is");

display(p1);

printf("\nthe second polynomial is");

display(p2);

p3=add(p1,p2);

printf("\naddition of two polynomial is :\n");

display(p3);

}

p *getpoly()

{

p *temp,*New,*last;

int flag,exp;

char ans;

float coef;

temp=NULL;

flag=1;

printf("\nenter the polynomial in descending order of exponent");

do

{

printf("\nenter the coef & exponent of a term");

scanf("%f%d",&coef,&exp);

New=getnode();

if(New==NULL)

printf("\nmemory cannot be allocated");

New->coef=coef;

New->exp=exp;

if(flag==1)

{

temp=New;

last=temp;

flag=0;

}

else

{

last->next=New;

last=New;

}

printf("\ndou want to more terms");

ans=getch();

}

while(ans=='y');

return(temp);

}

p *getnode()

{

p *temp;

temp=(p*) malloc (sizeof(p));

temp->next=NULL;

return(temp);

}

void display(p*head)

{

p*temp;

temp=head;

if(temp==NULL)

printf("\npolynomial empty");

while(temp->next!=NULL)

{

printf("%0.1fx^%d+",temp->coef,temp->exp);

temp=temp->next;

}

printf("\n%0.1fx^%d",temp->coef,temp->exp);

getch();

}

p*add(p*first,p*second)

{

p *p1,*p2,*temp,*dummy;

char ch;

float coef;

p *append(int,float,p*);

p1=first;

p2=second;

temp=(p*)malloc(sizeof(p));

if(temp==NULL)

printf("\nmemory cannot be allocated");

dummy=temp;

while(p1!=NULL&&p2!=NULL)

{

if(p1->exp==p2->exp)

{

coef=p1->coef+p2->coef;

temp=append(p1->exp,coef,temp);

p1=p1->next;

p2=p2->next;

}

else

if(p1->expexp)

{

coef=p2->coef;

temp=append(p2->exp,coef,temp);

p2=p2->next;

}

else

if(p1->exp>p2->exp)

{

coef=p1->coef;

temp=append(p1->exp,coef,temp);

p1=p1->next;

}

}

while(p1!=NULL)

{

temp=append(p1->exp,p1->coef,temp);

p1=p1->next;

}

while(p2!=NULL)

{

temp=append(p2->exp,p2->coef,temp);

p2=p2->next;

}

temp->next=NULL;

temp=dummy->next;

free(dummy);

return(temp);

}

p*append(int Exp,float Coef,p*temp)

{

p*New,*dum;

New=(p*)malloc(sizeof(p));

if(New==NULL)

printf("\ncannot be allocated");

New->exp=Exp;

New->coef=Coef;

New->next=NULL;

dum=temp;

dum->next=New;

dum=New;

return(dum);

}

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Algorithm to add two polynomials
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Is it possible to add 2 polynomials together and your answer is not a polynomial?

No. Even if the answer is zero, zero is still a polynomial.


How do you add polynomials?

homer Simpson


Is 4 - 3x plus 5x2 a polynomial?

Yes. If you add, subtract or multiply (but not if you divide) any two polynomials, you will get a polynomial.


Hellllp meee. How do you add polynomials when you don't have any like terms?

Hellllp meee, how do you add polynomials when you don't have any like terms is a very common questions when it comes to this type of math. However, the polynomials can only be added if all terms are alike. No unlike terms can be added within the polynomials.


Can the sum of three polynomials again be a polynomial?

The sum of two polynomials is always a polynomial. Therefore, it follows that the sum of more than two polynomials is also a polynomial.


What are the rules in addition of polynomials?

Add together the coefficients of "like" terms. Like terms are those that have the same powers of the variables in the polynomials.


When you multiply polynomials what do you do with the exponents?

Add them up providing that the bases are the same.


Give examples of some kinds of polynomials?

Binomials and trinomials are two types of polynomials. The first has two terms and the second has three.


What has the author T H Koornwinder written?

T. H. Koornwinder has written: 'Jacobi polynomials and their two-variable analysis' -- subject(s): Jacobi polynomials, Orthogonal polynomials


Rules in adding polynomials?

To add polynomials , simply combine similar terms. Combine similar terms get the sum of the numerical coefficients and affix the same literal coefficient .


What has the author Brian Thomas Smith written?

Brian Thomas Smith has written: 'A zero finding algorithm using Laguerre's method' -- subject(s): Algorithms, Polynomials


How do you multiply three or more polynomials?

To multiply TWO polynomials, you multiply each term in the first, by each term in the second. This can be justified by a repeated application of the distributive law. Two multiply more than two polynomials, you multiply the first two. Then you multiply the result with the third polynomial. If there are any more, multiply the result with the fourth polynomial, etc. Actually the polynomials can be multiplied in any order; both the communitative and associate laws apply.