Polynomail Addition
#include<stdio.h>#include<stdlib.h>
#define MAX_TERMS 100 /* size of terms array */
void Creat_polynomail(int start,int finish);
void Print_polynomial(int start,int finish);
int COMPARE(int exp1,int exp2);
void poly_add(int startA, int finishA, int startB, int finishB, int *startD,int *finishD);
typedef struct
{
float coef;
int expon;
} polynomial;
polynomial terms[ MAX_TERMS];
int avail = 0;
void Creat_polynomail(int start,int finish)
{
printf("\nEnter the polynomial:\n");
while(start <= finish)
{
printf("\n\tEnter details of term %d:",start);
printf("\n\tEnter coeff: ");
scanf("%f", &terms[start].coef);
printf("\n\tEnter exp: ");
scanf("%d", &terms[start].expon);
start++;
}
}
void Print_polynomial(int start,int finish)
{
while(start<= finish)
{
printf("(%.2f * x^%d)",terms[start].coef,terms[start].expon);
if(start != finish)
printf(" + ");
start++;
}
}
int COMPARE(int exp1,int exp2)
{
if(exp1<exp2)
return -1;
else if(exp1 == exp2)
return 0;
else
return 1;
}
void attach(float coefficient, int exponent)
{
if (avail > MAX_TERMS)
{
printf("\nToo many terms in the polynomial \n");
exit(1);
}
terms[avail].coef = coefficient;
terms[avail].expon = exponent;
avail++;
}
void poly_add(int startA, int finishA, int startB, int finishB, int *startD,int *finishD)
{
float coefficient;
avail = finishB+1;
*startD = avail;
while ( startA <= finishA && startB <=finishB)
{
switch (COMPARE(terms[startA].expon, terms[startB].expon))
{
case -1:
attach(terms[startB].coef, terms[startB].expon);
startB ++;
break;
case 0:
coefficient = terms[startA].coef + terms[startB].coef;
if (coefficient)
attach(coefficient, terms[startA].expon);
startA++;
startB ++;
break;
case 1:
attach(terms[startA].coef, terms[startA].expon);
startA++;
}
}
for (; startA <= finishA; startA++)
attach(terms[startA].coef, terms[startA].expon);
for (; startB <= finishB; startB++)
attach(terms[startB].coef, terms[startB].expon);
*finishD = avail-1;
printf("\n startD=%d", *startD);
printf("\n finishD=%d", *finishD);
printf("\n Resultant polynomial is: \n");
Print_polynomial(*startD, *finishD);
}
void main()
{
int startA, finishA;
int startB, finishB;
int startD, finishD;
printf("\n Enter start and end for A: ");
scanf("%d%d", &startA, &finishA);
Creat_polynomail(startA, finishA);
printf("\nThe polynomial A is: \n");
Print_polynomial(startA, finishA);
printf("\n\n Enter start and end for B: ");
scanf("%d%d", &startB, &finishB);
Creat_polynomail(startB, finishB);
printf("\nThe polynomial B is: \n");
Print_polynomial(startB, finishB);
poly_add(startA, finishA, startB, finishB, &startD, &finishD);
}
Output:
Enter start and end for A: 0 1
Enter the polynomial:
Enter details of term 0:
Enter coeff: 2
Enter exp: 1000
Enter details of term 1:
Enter coeff: 1
Enter exp: 0
The polynomial A is:
(2.00 * x^1000) + (1.00 * x^0)
Enter start and end for B: 2 5
Enter the polynomial:
Enter details of term 2:
Enter coeff: 1
Enter exp: 4
Enter details of term 3:
Enter coeff: 10
Enter exp: 3
Enter details of term 4:
Enter coeff: 3
Enter exp: 2
Enter details of term 5:
Enter coeff: 1
Enter exp: 0
The polynomial B is:
(1.00 * x^4) + (10.00 * x^3) + (3.00 * x^2) + (1.00 * x^0)
startD = 6
finishD = 10
Resultant polynomial is:
(2.00 * x^1000) + (1.00 * x^4) + (10.00 * x^3) + (3.00 * x^2) + (2.00 * x^0)