#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();
}
#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;
}
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.
#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
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 );
}
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.
Polynomial Addition Using Linked List 1. #include
Use Ascending order. You can get result. By n_senthil10@Yahoo.com
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);
}
a write the algorithm to concatenate two given string
An "algorithm" is simply a method to solve a certain problem. For example, when you use the standard method you learned in school to write down two numbers, one beneath the other, then add them, you are using an algorithm - a method that is known to give correct results in this particular case.
The algorithm will have both a constant time complexity and a constant space complexity: O(1)
Dijkstra's algorithm is used by the OSPF and the IS-IS routing protocols. The last three letters in OSPF (SPF) mean "shortest path first", which is an alternative name for Dijkstra's algorithm.
Time complexity and space complexity.
No. Even if the answer is zero, zero is still a polynomial.
homer Simpson
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 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.
The sum of two polynomials is always a polynomial. Therefore, it follows that the sum of more than two polynomials is also a polynomial.
Add together the coefficients of "like" terms. Like terms are those that have the same powers of the variables in the polynomials.
Add them up providing that the bases are the same.
Binomials and trinomials are two types of polynomials. The first has two terms and the second has three.
T. H. Koornwinder has written: 'Jacobi polynomials and their two-variable analysis' -- subject(s): Jacobi polynomials, Orthogonal polynomials
To add polynomials , simply combine similar terms. Combine similar terms get the sum of the numerical coefficients and affix the same literal coefficient .
Brian Thomas Smith has written: 'A zero finding algorithm using Laguerre's method' -- subject(s): Algorithms, 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.