Design & Develop and Implement a Program in C for evaluate a Polynomial

In the circular linked list the last node of the list contains the address of the first node and forms a circular chain.A polynomial equation is an equation that can be written in the form. axn + bxn-1 + . . . + rx + s = 0, where a, b, . . . , r and s are constants. We call the largest exponent of x appearing in a nonzero
term of a polynomial the degree of that polynomial. As with polynomials with one variable, you must pay attention to the rules of exponents and the order of operations so that you correctly evaluate an expression with two or more variables.

Evaluate x2 + 3y3for x = 7 and y = −2. Substitute the given values for x and y.

Evaluate 4x2y – 2xy2 + x – 7 for x = 3 and y = −1.

When a term contains both a number and a variable part, the number part is called the “coefficient”. The coefficient on the leading term is called the “leading” coefficient.

Steps required for Evaluating Polynomial Functions:
Step 1: Replace each x in the expression with the given value.
Step 2: Use the order of operation to simplify the expression

Steps required for addition of two polynomials.
Step 1
 Arrange the Polynomial in standard form
 Standard form of a polynomial just means that the term with highest degree is first and each of the following terms
Step 2
 Arrange the like terms in columns and add the like terms

				
					#include<stdio.h>
#include<alloc.h>
#include<math.h>
struct node
{
int cf, px, py, pz; int flag;
struct node *link;
};
typedef struct node NODE;
NODE* getnode()
{
NODE *x;
x=(NODE*)malloc(sizeof(NODE));
if(x==NULL)
{
printf("Insufficient memory\n"); exit(0);
}
return x;
}
void display(NODE *head)
{
NODE *temp;
if(head->link==head)
{
printf("Polynomial does not exist\n"); return;
}
temp=head->link;
printf("\n");

while(temp!=head)
{
printf("%d x^%d y^%d z^%d",temp->cf,temp->px,temp->py,temp->pz);
if(temp->link != head)
printf(" + ");
temp=temp->link;
}
printf("\n");
}
NODE* insert_rear(int cf,int x,int y,int z,NODE *head)
{
NODE *temp,*cur;
temp=getnode();
temp->cf=cf;
temp->px=x;
temp->py=y;
temp->pz=z;
cur=head->link;
while(cur->link!=head)
{
cur=cur->link;
}
cur->link=temp;
temp->link=head;
return head;
}
NODE* read_poly(NODE *head)
{
int px, py, pz, cf, ch;
printf("\nEnter coeff: ");
scanf("%d",&cf);
printf("\nEnter x, y, z powers(0-indiacate NO term): ");
scanf("%d%d%d", &px, &py, &pz);
head=insert_rear(cf,px,py,pz,head);
printf("\nIf you wish to continue press 1 otherwise 0: ");
scanf("%d", &ch);
while(ch != 0)
{

printf("\nEnter coeff: ");
scanf("%d",&cf);
printf("\nEnter x, y, z powers(0-indiacate NO term): "); scanf("%d%d%d",
&px, &py, &pz); head=insert_rear(cf,px,py,pz,head);
printf("\nIf you wish to continue press 1 otherwise 0: "); scanf("%d", &ch);
}
return head;
}
NODE* add_poly(NODE *h1,NODE *h2,NODE *h3)
{
NODE *p1,*p2;
int x1,x2,y1,y2,z1,z2,cf1,cf2,cf;
p1=h1->link;
while(p1!=h1)
{
x1=p1->px;
y1=p1->py;
z1=p1->pz;
cf1=p1->cf;
p2=h2->link;
while(p2!=h2)
{
x2=p2->px;
y2=p2->py;
z2=p2->pz;
cf2=p2->cf;
if(x1==x2 && y1==y2 && z1==z2)
break;
p2=p2->link;
}
if(p2!=h2)
{
cf=cf1+cf2;
p2->flag=1;
if(cf!=0)
h3=insert_rear(cf,x1,y1,z1,h3);
}
else
h3=insert_rear(cf1,x1,y1,z1,h3);
p1=p1->link;

}
p2=h2->link;
while(p2!=h2)
{
if(p2->flag==0)
h3=insert_rear(p2->cf,p2->px,p2->py,p2->pz,h3);
p2=p2->link;
}
return h3;
}
void evaluate(NODE *h1)
{
NODE *head; int
x, y, z;
float result=0.0;
head=h1;
printf("\nEnter x, y, z, terms to evaluate:\n");
scanf("%d%d%d", &x, &y, &z);
while(h1->link != head)
{
result = result + (h1->cf * pow(x,h1->px) * pow(y,h1->py) * pow(z,h1->pz));
h1=h1->link;
}
result = result + (h1->cf * pow(x,h1->px) * pow(y,h1->py) * pow(z,h1->pz));
printf("\nPolynomial result is: %f", result);
}
void main() {
NODE *h1,*h2,*h3; int
ch; clrscr();
h1=getnode();
h2=getnode();
h3=getnode();
h1->link=h1;
h2->link=h2;
h3->link=h3;
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:
printf("\nEnter polynomial to evaluate:\n");
h1=read_poly(h1);
display(h1);
evaluate(h1);
break;
case 2:
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;
} getch();
}
}
				
			
OUTPUT:
1. Evaluate polynomial P(x,y,z) = 6x2y2z-4yz5+3x3yz+2xy5z-2xyz3 

2. Add two polynomials
3. Exit
Enter your choice: 1
Enter polynomial to
evaluate: Enter coeff:
6

Enter x, y, z powers (0-indiacate NO term:
2 2 1
If you wish to continue press 1 otherwise 0:
1 Enter coeff: -4
Enter x, y, z powers (0-indiacate NO term: 0
1 5
If you wish to continue press 1 otherwise 0:
1
Enter coeff: 3
Enter x, y, z powers (0-indiacate NO term: 3 1 1
If you wish to continue press 1
otherwise 0: 1
Enter coeff: 2
Enter x, y, z powers (0-indiacate NO term: 1 5 1
If you wish to continue press 1
otherwise 0: 1
Enter coeff: -2
Enter x, y, z powers (0-indiacate NO term: 1 1 3
If you wish to continue press 1 otherwise 0: 0
Polynomial is:
6 x^2 y^2 z^1 + -4 x^0 y^1 z^5 + 3 x^3 y^1 z^1 + 2 x^1 y^5 z^1 + -2 x^1 y^1 z^3
Enter x, y, z, terms to
evaluate: 1 1 1
Polynomial result is: 5.000000
1. Evaluate polynomial P(x,y,z) = 6x2y2z-
4yz5+3x3yz+2xy5z-2xyz3 2. Add two polynomials
3. Exit
Enter your choice: 2
Enter 1st polynomial:
Enter coeff: 4
Enter x, y, z powers (0-indiacate NO term: 2 2 2
If you wish to continue press 1
otherwise 0: 1 Enter coeff: 3
Enter x, y, z powers (0-indiacate NO term: 1 1 2
If you wish to continue press 1 otherwise 0: 0
Enter 2nd polynomial:
Enter coeff: 6

Enter x, y, z powers (0-indiacate NO term:
2 2 2
If you wish to continue press 1 otherwise 0:
0
Polynomial is:
1st Polynomial is:
4 x^2 y^2 z^2 + 3 x^1 y^1 z^2
2nd Polynomial
is: 6 x^2 y^2
z^2
Added polynomial: 10 x^2 y^2 z^2 + 3 x^1 y^1 z^2
1. Evaluate polynomial P(x,y,z) = 6x2y2z-
4yz5+3x3yz+2xy5z-2xyz3 2. Add two polynomials
3. Exit
Enter your choice: 3

Leave a Reply