Friday, October 8, 2010

C Program to create,display and add polynomials

/* Program to create,display and add polynomials */
#include
#include
#include
struct polynode
{
float coeff;
int exp;
struct polynode *next;
};

void create_poly(struct polynode **,float,int);
void display(struct polynode *);
void add_poly(struct polynode *,struct polynode *,struct polynode **);
void main()
{
struct polynode *first,*second,*total;
int i=0;

first=second=total=NULL; /* Empty linked lists */
create_poly(&first,1.4,5);
create_poly(&first,1.5,4);
create_poly(&first,1.7,2);
create_poly(&first,1.8,1);
create_poly(&first,1.9,0);

clrscr();
display(first);

create_poly(&second,1.5,6);
create_poly(&second,2.5,5);
create_poly(&second,-3.5,4);
create_poly(&second,4.5,3);
create_poly(&second,6.5,1);

printf("\n\n");
display(second);

/* Draws a dashed horizontal line */
printf("\n");
while(i++<79) printf("-"); printf("\n\n"); add_poly(first,second,&total); display(total); getch(); } /* ADDs a term to polynomial */ void create_poly(struct polynode **q,float x,int y) { struct polynode *temp; temp=*q; /* Creates a new node if the list is empty */ if(*q==NULL) { (*q)=malloc(sizeof(struct polynode)); temp=*q; } else { /* Traverse the entire Linked List */ while(temp->next != NULL)
temp=temp->next;

/* Create new node at intermediate stages */
temp->next=malloc(sizeof(struct polynode));
temp=temp->next;
}

/* Assign coefficient and exponent */
temp->coeff=x;
temp->exp=y;
temp->next=NULL;
}

/* Displays the contents of linked list representing a polynomial */
void display(struct polynode *q)
{
/* Traverse till the end of the linked list */
while(q!= NULL)
{
printf("%2.1f x^ %d : ",q->coeff,q->exp);
q=q->next;
}
printf("\b\b\b"); /* Erases the last colon */
}

/* Add two polynomials */
void add_poly(struct polynode *x,struct polynode *y,struct polynode **s)
{
struct polynode *z;

/* If both linked lists are empty */
if(x==NULL && y==NULL)
return;

/*Traverse till one of the list ends */
while(x!=NULL && y!=NULL)
{
/*Create a new node if the list is empty */
if(*s==NULL)
{
*s=malloc(sizeof(struct polynode));
z=*s;
}

/* Create new nodes at intermediate stages */
else
{
z->next=malloc(sizeof(struct polynode));
z=z->next;
}



/* Store a term of larger degree polynomial */

if(x->exp < y->exp)
{
z->coeff=y->coeff;
z->exp=y->exp;
y=y->next; /* GO to next node */
}
else
{
if(x->exp > y->exp)
{
z->coeff=x->coeff;
z->exp=x->exp;
x=x->next;
}
else
{
/* Add the coefficients when exponents ae equal */
if(x->exp == y->exp)
{
/* Assigning the added coefficients */
z->coeff=x->coeff+y->coeff;
z->exp=x->exp;
/* Go to next node */
x=x->next;
y=y->next;
}
}
}
}
/* Assigning remainining terms of the first polynomial to the rssult */

while(x!=NULL)
{
if(*s == NULL)
{
*s=malloc(sizeof(struct polynode));
z=*s;
}
else
{
z->next=malloc(sizeof(struct polynode));
z=z->next;
}

/* Assign coefficient and exponent */
z->coeff=x->coeff;
z->exp=x->exp;
x=x->next; /* Go to next node */
/* Assign remainning terms of the second polynomial to the result */
while(y!=NULL)
{
if(*s==NULL)
{
*s=malloc(sizeof(struct polynode));
z=*s;
}
else
{
z->next=malloc(sizeof(struct polynode));
z=z->next;
}


/* Assign coefficient and exponent */
z->coeff=y->coeff;
z->exp=y->exp;
y=y->next;
}
z->next=NULL; /* Assign NULLL at the end of the resultoing linked list */
} }






























No comments:

Post a Comment