Advertise With us

 

Polynomial Source Code

#include<stdio.h>
#include<conio.h>
#include<malloc.h>
struct poly
{
int exp;
float coeff;
struct poly *next;
};
struct poly *first,*second,*third,*start=NULL,*start1=NULL,*start2=NULL;
int n,n1;
void create();
void merge();
void printpoly();
void create()
{
int i,m;
float m1;
printf("\nenter the values of exp. and coeff. for the first polynomial");
printf("\nthe exponential values must be in the descending order");
printf("\nenter the no. of nodes for the first polynomial");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("\nenter the exponent and coefficient for the %d node",i);
scanf("%d%f",&m,&m1);
first=(struct poly*)malloc (sizeof(struct poly));
first->exp=m;
first->coeff=m1;
first->next=start;
start=first;
}
printf("\nenter the values of exp. and coeff. for the second polynomial");
printf("\nthe exponential values must be in the descending order");
printf("\nenter the no. of nodes for the second polynomial");
scanf("%d",&n1);
for(i=1;i<=n1;i++)
{
printf("\nenter the exponent and coefficient for the %d node",i);
scanf("%d%f",&m,&m1);
second=(struct poly*)malloc (sizeof(struct poly));
second->exp=m;
second->coeff=m1;
second->next=start1;
start1=second;
}
}
void merge()
{
struct poly *p,*p1;
int t,t1,c=0;
p=start;
p1=start1;
while(p!=NULL)
{
t=p->exp;
t1=p1->exp;
third=(struct poly*)malloc(sizeof(struct poly));
third->next=start2;
start2=third;
if(p1==NULL)
{
c=1;
third->exp=t;
third->coeff=p->coeff;
third=third->next;
p=p->next;
}
else
{
if(t>t1)
{
third->exp=t;
third->coeff=p->coeff;
third=third->next;
p=p->next;
}
else if(t<t1)
{
third->exp=t1;
third->coeff=p1->coeff;
third=third->next;
p1=p1->next;
}
else
{
third->exp=t;
third->coeff=p->coeff+p1->coeff;
third=third->next;
p=p->next;
p1=p1->next;
}
}
}
if(c==0)
{
while(p1!=NULL)
{
third=(struct poly*)malloc(sizeof(struct poly));
third->next=start2;
start2=third;
third->exp=second->exp;
third->coeff=second->coeff;
third=third->next;
p1=p1->next;
}
}
}
void printpoly()
{
int r,j=0;
float r1;
struct poly *p2;
p2=start2;
while(p2!=NULL)
{
j++;
r=p2->exp;
r1=p2->coeff;
printf("\nthe %d exponent and coefficient is=%d and %f",j,r,r1);
p2=p2->next;
}
}
void main()
{
int ch;
clrscr();
while(1)
{
printf("\nenter 1. for creation of the first and second polynomials");
printf("\nenter 2. for merging of the above two polynomials");
printf("\nenter 3. for printing of the merged or third polynomial");
printf("\nenter 4. for exit");
printf("\nenter your choice");
scanf("%d",&ch);
switch(ch)
{
case 1:
create();
break;
case 2:
merge();
break;
case 3:
printpoly();
break;
case 4:
exit(0);
default:
printf("\nyou have entered a wrong choice");
}
}
}

Google