0% found this document useful (0 votes)
20 views

Dsa Polyinomial

The document contains C code to represent and evaluate polynomials in three variables (x, y, z). It defines a polynomial node structure and functions to read polynomials, add polynomials, evaluate a polynomial, and display a polynomial. The main function drives a menu to allow representing, evaluating, and adding polynomials.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Dsa Polyinomial

The document contains C code to represent and evaluate polynomials in three variables (x, y, z). It defines a polynomial node structure and functions to read polynomials, add polynomials, evaluate a polynomial, and display a polynomial. The main function drives a menu to allow representing, evaluating, and adding polynomials.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

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

h> scanf("%d", &yexp); scanf("%d", &zexp); a = a->link; b = b->link; break; }

#define COMPARE(x, y) ( (x == y) ? 0 : (x > y) ? 1 : -1) head = attach(coef, xexp, yexp, zexp, head); } if(a->xexp!=0 || b->xexp!=0) {

struct node { int coef; polynomial 9 return head; } switch(COMPARE(a->xexp, b->xexp)) {

int xexp, yexp, zexp; void display(NODE head) { case -1 : head3 = attach(b->coef, b->xexp, b->yexp, b->zexp, head3);

struct node *link; }; NODE temp; b = b->link; break;

typedef struct node *NODE; if(head->link == head) { case 0 : if(a->yexp > b->yexp) {

NODE getnode() { printf("\nPolynomial does not exist."); head3 = attach(a->coef, a->xexp, a->yexp, a->zexp, head3);

NODE x; return; } temp = head->link; a = a->link;break; }

x = (NODE) malloc(sizeof(struct node)); while(temp != head) { else if(a->yexp < b->yexp) {

if(x == NULL) { printf("%dx^%dy^%dz^%d", temp->coef, temp->xexp, temp->yexp, temp>zexp); head3 = attach(b->coef, b->xexp, b->yexp, b->zexp,

printf("Running out of memory \n"); temp = temp->link; if(temp != head) head3); b = b->link;break; }

return NULL; } return x; } printf(" + "); }} else if(a->zexp > b->zexp) {

NODE attach(int coef, int xexp, int yexp, int zexp, NODE head) { int poly_evaluate(NODE head) { head3 = attach(a->coef, a->xexp, a->yexp, a->zexp, head3);

NODE temp, cur; int x, y, z, sum = 0; a = a->link; break; } else if(a->zexp < b->zexp) {

temp = getnode(); NODE poly; head3 = attach(b->coef, b->xexp, b->yexp, b->zexp, head3);

temp->coef = coef; printf("\nEnter the value of x,y and z: "); b = b->link; break; }

temp->xexp = xexp; scanf("%d %d %d", &x, &y, &z); case 1 : head3 = attach(a->coef,a->xexp,a->yexp,a->zexp,head3);

temp->yexp = yexp; poly = head->link; a = a->link; break; }

temp->zexp = zexp; while(poly != head) { break; }

cur = head->link; sum += poly->coef * pow(x,poly->xexp)* pow(y,poly->yexp) * pow(z,poly if(a->yexp!=0 || b->yexp!=0) {

while(cur->link != head) { >zexp);poly = poly->link; } switch(COMPARE(a->yexp, b->yexp)) {

cur = cur->link; } cur->link = temp; return sum; } case -1 : head3 = attach(b->coef, b->xexp, b->yexp, b->zexp, head3);

temp->link = head; return head; } NODE poly_sum(NODE head1, NODE head2, NODE head3) { b = b->link; break;

NODE read_poly(NODE head) { NODE a, b; case 0 : if(a->zexp > b->zexp) {

int i, j, coef, xexp, yexp, zexp, n; int coef; head3 = attach(a->coef, a->xexp, a->yexp, a->zexp, head3);

printf("\nEnter the no of terms in the polynomial: "); a = head1->link; a = a->link; break; }else if(a->zexp < b->zexp)

scanf("%d", &n); for(i=1; i<=n; i++) { b = head2->link; { head3 = attach(b->coef, b->xexp, b->yexp, b->zexp, head3);

printf("\n\tEnter the %d term: ",i); while(a!=head1 && b!=head2) {while(1) { b = b->link; break; }

printf("\n\t\tCoef = "); scanf("%d", &coef); if(a->xexp == b->xexp && a->yexp == b->yexp && a->zexp == b->zexp) { case 1 : head3 = attach(a->coef, a->xexp, a->yexp, a->zexp, head3);

printf("\n\t\tEnter Pow(x) Pow(y) and Pow(z): "); coef = a->coef + b->coef; a = a->link;break; } break; }

scanf("%d", &xexp); head3 = attach(coef, a->xexp, a->yexp, a->zexp, head3); if(a->zexp!=0 || b->zexp!=0) {

switch(COMPARE(a->zexp,b->zexp)) {

case -1 : head3 = attach(b->coef,b->xexp,b->yexp,b->zexp,head3);

b = b->link; break;

case 1 : head3 = attach(a->coef, a->xexp, a->yexp, a->zexp, head3);

a = a->link; break; } break; } } } while(a!= head1) {

head3 = attach(a->coef,a->xexp,a->yexp,a->zexp,head3);a = a->link; }

while(b!= head2) {

head3 = attach(b->coef,b->xexp,b->yexp,b->zexp,head3); b = b->link;}

return head3; } void main() {

NODE head, head1, head2, head3;

int res, ch;

head = getnode(); head1 = getnode();

head2 = getnode(); head3 = getnode();

head->link=head; head1->link=head1;

head2->link=head2; head3->link= head3;

while(1) {

printf("\n~~~Menu~~~");

printf("\n1.Represent and Evaluate a Polynomial P(x,y,z)");

printf("\n2.Find the sum of two polynomials POLY1(x,y,z)");

printf("\nEnter your choice:"); scanf("%d",&ch);

switch(ch) { case 1: printf("\n~~~~Polynomial evaluation P(x,y,z)~~~\n");

head = read_poly(head);

printf("\nRepresentation of Polynomial for evaluation: \n"); display(head);

res = poly_evaluate(head); printf("\nResult of polynomial evaluation is : %d \n", res);

break; case 2: printf("\nEnter the POLY1(x,y,z): \n");

head1 = read_poly(head1); printf("\nPolynomial 1 is: \n");

display(head1);

printf("\nEnter the POLY2(x,y,z): \n"); head2 = read_poly(head2);

printf("\nPolynomial 2 is: \n"); display(head2);

printf("\nPolynomial addition result: \n"); head3 = poly_sum(head1,head2,head3);

display(head3); break; case 3: exit(0); } } }

You might also like