0% found this document useful (0 votes)
16 views3 pages

Suresh Polynomial

Uploaded by

shreyadevraj874
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views3 pages

Suresh Polynomial

Uploaded by

shreyadevraj874
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

#include<stdio.

h>
#include<stdlib.h>
#include<math.h>

struct poly
{ int cf, px, py, pz, flag;
struct poly *next;
};

typedef struct poly node;

node* getnode()
{ node *new1;
new1 = (node*) malloc(sizeof(node));
new1->next = new1;
return new1;
}

void display(node *head)


{ node *temp = head->next;
if(head->next == head)
{ printf("Polynomial does not exist\n");
return;
}

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");
}

node* insert_rear(int f,int x,int y,int z,node *head)


{ node *new1,*temp;
new1 = getnode();
new1->cf = f;
new1->px = x;
new1->py = y;
new1->pz = z;
new1->flag = 0;
temp = head->next;
while(temp->next != head)
{ temp = temp->next;
}
temp->next = new1;
new1->next = head;
return head;
}

node* read_poly(node *head)


{ int px, py, pz, cf, ch;
do
{ 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);
return head;
}

node* add_poly(node *h1, node *h2, node *h3)


{ node *p1,*p2;
p1 = h1->next;

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 evaluate(node *he)


{ node *temp = he;
int x, y, z;
float result = 0.0;
printf("\nEnter x, y, z, terms to evaluate:\n");
scanf("%d%d%d",&x,&y,&z);
temp = temp->next;
while( temp != he)
{result = result + (temp->cf * pow(x, temp->px) * pow(y, temp->py) * pow(z,
temp->pz));
temp = temp->next;
}
printf("\nPolynomial result is: %f", result);
}

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;
}
}
}

You might also like