Program
#include<stdio.h>
struct poly
{
int coeff, exp, temp_flag;
};
struct poly p1[10],p2[10],p3[10],p4[10];
void main()
{
int t1,t2,t3;
void read_poly(struct poly [],int);
void display_poly(struct poly [],int);
int mult_poly(struct poly [],int,struct poly [],int,struct poly []);
int sort_poly(struct poly [],int,struct poly []);
printf("\nEnter no of terms of first polynomial : ");
scanf("%d",&t1);
read_poly(p1,t1);
display_poly(p1,t1);
printf("\nEnter no of terms of second polynomial : ");
scanf("%d",&t2);
read_poly(p2,t2);
display_poly(p2,t2);
printf("\nMultiplication of polynomial :\n");
t3=mult_poly(p1,t1,p2,t2,p3);
t3=sort_poly(p3,t3,p4);
display_poly(p4,t3);
getch();
}
void read_poly(struct poly A[10],int terms)
{
int i;
printf("\nEnter polynomial\n");
for(i=terms-1;i>0;i--)
{
printf("\n%d term",i+1);
printf("\nEnter coefficient:");
scanf("%d",&A[i].coeff);
printf("\nEnter exponent:");
scanf("%d",&A[i].exp);
A[i].temp_flag=0;
}
printf("\nEnter constant term of polynomial:");
scanf("%d",&A[0].coeff);
A[0].exp=0;
A[0].temp_flag=0;
}
void display_poly(struct poly A[10], int terms)
{
int i;
for(i=terms-1;i>0;i--)
printf("%dX^%d+",A[i].coeff,A[i].exp);
printf("%d\n",A[0].coeff);
}
int mult_poly(struct poly A[10], int terms1, struct poly B[10],int terms2,struct poly C[10])
{
int i,j,k;
k=0;
for(i=0;i<terms1;i++)
{
for(j=0;j<terms2;j++)
{
C[k].exp =A[i].exp +B[j].exp;
C[k].coeff =A[i].coeff * B[j].coeff;
C[k].temp_flag=0;
k++;
}
}
return k;
}
int sort_poly(struct poly A[10], int terms, struct poly B[10])
{
int i,j,k=0;
for(i=0;i<terms;i++)
{
for(j=0;j<terms;j++)
{
if(i!=j)
{
if(A[j].temp_flag ==0)
{
if(A[i].exp == A[j].exp)
{
B[k].exp =A[i].exp;
B[k].coeff =A[i].coeff +A[j].coeff;
A[i].temp_flag=1;
A[j].temp_flag =1;
k++;
}
}
}
}
if(A[i].temp_flag ==0)
{
B[k].exp =A[i].exp;
B[k].coeff =A[i].coeff;
A[i].temp_flag =1;
k++;
}
}
return k;
}
#include<stdio.h>
struct poly
{
int coeff, exp, temp_flag;
};
struct poly p1[10],p2[10],p3[10],p4[10];
void main()
{
int t1,t2,t3;
void read_poly(struct poly [],int);
void display_poly(struct poly [],int);
int mult_poly(struct poly [],int,struct poly [],int,struct poly []);
int sort_poly(struct poly [],int,struct poly []);
printf("\nEnter no of terms of first polynomial : ");
scanf("%d",&t1);
read_poly(p1,t1);
display_poly(p1,t1);
printf("\nEnter no of terms of second polynomial : ");
scanf("%d",&t2);
read_poly(p2,t2);
display_poly(p2,t2);
printf("\nMultiplication of polynomial :\n");
t3=mult_poly(p1,t1,p2,t2,p3);
t3=sort_poly(p3,t3,p4);
display_poly(p4,t3);
getch();
}
void read_poly(struct poly A[10],int terms)
{
int i;
printf("\nEnter polynomial\n");
for(i=terms-1;i>0;i--)
{
printf("\n%d term",i+1);
printf("\nEnter coefficient:");
scanf("%d",&A[i].coeff);
printf("\nEnter exponent:");
scanf("%d",&A[i].exp);
A[i].temp_flag=0;
}
printf("\nEnter constant term of polynomial:");
scanf("%d",&A[0].coeff);
A[0].exp=0;
A[0].temp_flag=0;
}
void display_poly(struct poly A[10], int terms)
{
int i;
for(i=terms-1;i>0;i--)
printf("%dX^%d+",A[i].coeff,A[i].exp);
printf("%d\n",A[0].coeff);
}
int mult_poly(struct poly A[10], int terms1, struct poly B[10],int terms2,struct poly C[10])
{
int i,j,k;
k=0;
for(i=0;i<terms1;i++)
{
for(j=0;j<terms2;j++)
{
C[k].exp =A[i].exp +B[j].exp;
C[k].coeff =A[i].coeff * B[j].coeff;
C[k].temp_flag=0;
k++;
}
}
return k;
}
int sort_poly(struct poly A[10], int terms, struct poly B[10])
{
int i,j,k=0;
for(i=0;i<terms;i++)
{
for(j=0;j<terms;j++)
{
if(i!=j)
{
if(A[j].temp_flag ==0)
{
if(A[i].exp == A[j].exp)
{
B[k].exp =A[i].exp;
B[k].coeff =A[i].coeff +A[j].coeff;
A[i].temp_flag=1;
A[j].temp_flag =1;
k++;
}
}
}
}
if(A[i].temp_flag ==0)
{
B[k].exp =A[i].exp;
B[k].coeff =A[i].coeff;
A[i].temp_flag =1;
k++;
}
}
return k;
}
No comments:
Post a Comment