Vous êtes sur la page 1sur 3

struct poly for(i=1;i<=n;i++)

{ void main() {
int c,xp,yp,zp; { printf("term %d :\n",i);
struct poly *link; int ch; printf("Enter the coefficient \n");
}; POLY *poly1,*poly2 ,*poly3; scanf("%d",&c);
printf("Enter Exponent value for x,y,and z\n");
typedef struct poly POLY; poly3 = (POLY*)malloc(sizeof(POLY));
scanf("%d%d%d",&x,&y,&z );
poly3->link = poly3;
POLY* getnode(int c, int x,int y,int z) temp = getnode(c,x,y,z);
{ clrscr(); head = attach(temp,head);
POLY *node; printf("\naddition of } polynomial");
node = (POLY*)malloc(sizeof(POLY)); return head;
printf("enter polynomial1:\n");
}
node -> c = c ; poly1=readpoly();
node -> xp = x ; int power(int x,int n)
node -> yp = y ; printf("enter polynomial
{ 2:\n");
node -> zp = z ; poly2=readpoly(); if(n==0)
node->link=NULL; return 1;
poly3 = addpoly(poly1,return poly2,xpoly3);
* power(x,n-1);
return node; }
} display(poly1);
display(poly2); void display(POLY *head )
printf("\n\nThe resultant polynomial is:\n");
POLY* attach(POLY *node,POLY *head) {
display(poly3);
{ POLY *cur;
POLY *cur; for ( cur = head ->link; cur!=head ; cur = cur->link )
getch();
cur = head->link; {
}
while(cur->link != head) if(cur->c >= 0)
{ printf("+%dx^%dy^%dz^%d
cur=cur->link; ",cur->c,cur->xp,cur->yp,cur->zp);
} else
cur->link=node; printf("%dx^%dy^%dz^%d
node->link=head; ",cur->c,cur->xp,cur->yp,cur->zp);
return head; }
} printf("\n");
}
POLY *readpoly()
{ int compare(int ax,int ay, int az, int bx, int by, int bz )
int n,i,c,x,y,z; {
POLY *temp, *head; if( ax == bx && ay == by && az == bz) return 0;
if( ax > bx ) return 1;
head= (POLY*)malloc(sizeof(POLY)); if(ax == bx && ay > by ) return 1;
head->link=head; if(ax == bx && ay == by && az > bz ) return 1;
return -1; // If Poly1 Term < Poly2 Term
printf("enter no of terms\n"); }
scanf("%d",&n);
POLY* addpoly(POLY *poly1,POLY *poly2,POLY *poly) }
{
int res;
POLY *A,*B,*temp;
A=poly1->link; B=poly2->link;

while( A != poly1 && B != poly2 )


{
res = compare( A->xp, A->yp, A->zp, B->xp, B->yp,
B->zp );

switch( res )
{
case 0: temp = getnode( A->c+B->c , A->xp,
A->yp, A->zp );
A = A -> link; B = B -> link;
break;
case 1:
temp = getnode( A->c , A->xp, A->yp,
A->zp );
A=A->link;
break;
case -1:
temp = getnode( B->c , B->xp, B->yp,
B->zp );
B = B->link;
}
poly = attach(temp,poly);
}

while( A != poly1 )
{
temp = (POLY*)malloc(sizeof(POLY));
temp = getnode(A->c,A->xp,A->yp,A->zp);
poly = attach(temp,poly);
A=A->link;
}

while(B != poly2 )
{
temp=getnode( B->c ,B->xp,B ->yp, B->zp);
poly=attach(temp,poly);
B = B->link;
}
return poly;
enter no of terms enter no of terms
3 3
term 1 : term 1 :
Enter the coefficient Enter the coefficient
3 3
Enter Exponent value for x,y,and z Enter Exponent value for x,y,and z
9 5 2 9 5 2
term 2 : term 2 :
Enter the coefficient Enter the coefficient
4 4
Enter Exponent value for x,y,and z Enter Exponent value for x,y,and z
6 4 2 6 4 2
term 3 : term 3 :
Enter the coefficient Enter the coefficient
1 1
Enter Exponent value for x,y,and z Enter Exponent value for x,y,and z
3 2 1 3 2 1

enter polynomial 2: enter polynomial 2:


enter no of terms enter no of terms
2 2
term 1 : term 1 :
Enter the coefficient Enter the coefficient
2 2
Enter Exponent value for x,y,and z Enter Exponent value for x,y,and z
6 4 2 6 4 2
term 2 : term 2 :
Enter the coefficient Enter the coefficient
3 3
Enter Exponent value for x,y,and z Enter Exponent value for x,y,and z
5 3 2 5 3 2

+3x^9y^5z^2 +4x^6y^4z^2 +1x^3y^2z^1 +3x^9y^5z^2 +4x^6y^4z^2 +1x^3y^2z^1


+2x^6y^4z^2 +3x^5y^3z^2 +2x^6y^4z^2 +3x^5y^3z^2

The resultant polynomial is: The resultant polynomial is:


+3x^9y^5z^2 +6x^6y^4z^2 +3x^5y^3z^2 +1x^3y^2z^1 +3x^9y^5z^2 +6x^6y^4z^2 +3x^5y^3z^2 +1x^3y^2z^1

Vous aimerez peut-être aussi