Suresh Polynomial
Suresh Polynomial
h>
#include<stdlib.h>
#include<math.h>
struct poly
{ int cf, px, py, pz, flag;
struct poly *next;
};
node* getnode()
{ node *new1;
new1 = (node*) malloc(sizeof(node));
new1->next = new1;
return new1;
}
while(temp != head)
{ printf("\n %d x^%d y^%d z^%d",temp->cf,temp->px,temp->py,temp->pz);
if(temp->next != head)
printf(" + ");
temp=temp->next;
}
printf("\n");
}
while(p1 != h1)
{ p2 = h2->next;
while(p2 != h2)
{ if( p1->px == p2->px && p1->py == p2->py && p1->pz==p2->pz)
{ h3 = insert_rear(p1->cf + p2->cf, p2->px,p2->py, p2->pz,h3);
p1->flag = 1;
p2->flag = 1;
break;
}
p2 = p2->next;
}
if ( p1->flag ==0 )
h3 = insert_rear(p1->cf, p1->px, p1->py, p1->pz, h3);
p1 = p1->next;
}
p2 = h2->next;
while(p2 != h2)
{ if ( p2->flag ==0 )
h3 = insert_rear(p2->cf, p2->px,p2->py, p2->pz,h3);
p2 = p2->next;
}
return (h3);
}
void main()
{ node *h1,*h2,*h3,*he;
int ch;
while(1)
{ printf("\n\n1.Evaluate polynomial\n2.Add two polynomials\n3.Exit\n");
printf("Enter your choice: ");
scanf("%d",&ch);
switch(ch)
{ case 1: he = getnode();
printf("\nEnter polynomial to evaluate:\n");
he = read_poly(he);
display(he);
evaluate(he);
free(he);
break;
case 2: h1 = getnode();
h2 = getnode();
h3 = getnode();
printf("\nEnter the first polynomial:");
h1 = read_poly(h1);
printf("\nEnter the second polynomial:");
h2 = read_poly(h2);
h3 = add_poly(h1,h2,h3);
printf("\nFirst polynomial is: ");
display(h1);
printf("\nSecond polynomial is: ");
display(h2);
printf("\nThe sum of 2 polynomials is: ");
display(h3);
break;
case 3:exit(0);
break;
default:printf("\nInvalid entry");
break;
}
}
}