#include<stdio.h>
#include<conio.h>
struct poly
{
int coef;
int xexp;
int yexp;
int zexp;
struct poly *link;
}*ply1,*ply2,*sply,*rear,*nw,*ptr,*locp,*loc,*loca;
int cho;
void ins_poly()
{
//cho = 0;
do
{
nw = new poly;
printf("\n Enter the coefficient : ");
scanf("%d",&nw->coef);
printf("\n Enter the power for 'x' : ");
scanf("%d",&nw->xexp);
printf("\n Enter the power for 'y' : ");
scanf("%d",&nw->yexp);
printf("\n Enter the power for 'z' : ");
scanf("%d",&nw->zexp);
nw->link = NULL;
if(ply1 == NULL)
ply1 = nw;
else
{
locp = NULL;
loc = ply1;
while(loc != NULL && ( nw->xexp < loc->xexp || (nw->xexp == loc->xexp && nw->yexp < loc->yexp ) || (nw->xexp == loc->xexp && nw->yexp == loc->yexp && nw->zexp < loc->zexp )) )
{
locp = loc; loc = loc->link;
}
if(nw->xexp == loc->xexp && nw->yexp == loc->yexp && nw->zexp == loc->zexp )
{
loc->coef += nw->coef;
delete nw;
}
else
{
if(locp == NULL)
{
nw->link = ply1;
ply1 = nw;
}
else
{
nw->link = loc;
locp->link = nw;
}
}
}
printf("\n\n Enter '1' to continue : ");
scanf("%d",&cho);
}while(cho == 1);
do
{
nw = new poly;
printf("\n Enter the coefficient : ");
scanf("%d",&nw->coef);
printf("\n Enter the power for 'x' : ");
scanf("%d",&nw->xexp);
printf("\n Enter the power for 'y' : ");
scanf("%d",&nw->yexp);
printf("\n Enter the power for 'z' : ");
scanf("%d",&nw->zexp);
nw->link = NULL;
if(ply2 == NULL)
ply2 = nw;
else
{
locp = NULL;
loc = ply2;
while(loc != NULL && ( nw->xexp < loc->xexp || (nw->xexp == loc->xexp && nw->yexp < loc->yexp ) || (nw->xexp == loc->xexp && nw->yexp == loc->yexp && nw->zexp < loc->zexp )) )
{
locp = loc; loc = loc->link;
}
if(nw->xexp == loc->xexp && nw->yexp == loc->yexp && nw->zexp == loc->zexp )
{
loc->coef += nw->coef;
delete nw;
}
else
{
if(locp == NULL)
{
nw->link = ply2;
ply2 = nw;
}
else
{
nw->link = loc;
locp->link = nw;
}
}
}
printf("\n\n Enter '1' to continue : ");
scanf("%d",&cho);
}while(cho == 1);
}
void add_poly()
{
loc = ply1;
loca = ply2;
sply = NULL;
while(loc != NULL && loca != NULL)
{
nw = new poly;
if(loc->xexp > loca->xexp || loc->yexp > loca->yexp || loc->zexp > loca->zexp )
{
nw = loc;
loc = loc->link;
}
else if(loc->xexp < loca->xexp || loc->yexp < loca->yexp || loc->zexp < loca->zexp )
{
nw = loca;
loca = loca->link;
}
else
{
nw = loc;
nw->coef = loca->coef + loc->coef;
loc = loc ->link;
loca = loca ->link;
}
nw->link = NULL;
if(sply == NULL)
sply = rear = nw;
else
{
rear->link = nw;
rear = nw;
}
}
while(loc != NULL)
{
nw = new poly;
nw = loc;
rear->link = nw;
rear = nw;
loc = loc->link;
}
while(loca != NULL)
{
nw = new poly;
nw = loca;
rear->link = nw;
rear = nw;
loca = loca->link;
}
}
void main()
{
clrscr();
ins_poly();
printf(" (%d)*pow(x,%d)*pow(y,%d)*pow(z,%d)",ply1->coef,ply1->xexp,ply1->yexp,ply1->zexp);
ptr = ply1->link;
while(ptr != NULL)
{
printf(" + (%d)*pow(x,%d)*pow(y,%d)*pow(z,%d)",ptr->coef,ptr->xexp,ptr->yexp,ptr->zexp);
ptr = ptr->link;
}
printf("\n\n (%d)*pow(x,%d)*pow(y,%d)*pow(z,%d)",ply2->coef,ply2->xexp,ply2->yexp,ply2->zexp);
ptr = ply2->link;
while(ptr != NULL)
{
printf(" + (%d)*pow(x,%d)*pow(y,%d)*pow(z,%d)",ptr->coef,ptr->xexp,ptr->yexp,ptr->zexp);
ptr = ptr->link;
}
add_poly();
printf("\n\n\n (%d)*pow(x,%d)*pow(y,%d)*pow(z,%d)",sply->coef,sply->xexp,sply->yexp,sply->zexp);
ptr = sply->link;
while(ptr != NULL)
{
printf(" + (%d)*pow(x,%d)*pow(y,%d)*pow(z,%d)",ptr->coef,ptr->xexp,ptr->yexp,ptr->zexp);
ptr = ptr->link;
}
getch();
}