-
Notifications
You must be signed in to change notification settings - Fork 6
/
Program6.c
114 lines (114 loc) · 3.02 KB
/
Program6.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include<stdio.h>
#include<stdlib.h>
#define MAX_TERMS 100
typedef struct
{
float coef;
int expon;
}polynomial;
polynomial terms[MAX_TERMS];
int avail=0;
void attach(float coefficent,int exponent)
{
if(avail>=MAX_TERMS)
fprintf(stderr,"Too many terms in the polynomial\n");
exit(1);
terms[avail].coef=coefficent;
terms[avail++].expon=exponent;
}
int COMPARE(int a,int b)
{
if(a>b)
return 1;
if(a==b)
return 0;
if(b>a)
return -1;
}
void padd(int startA,int finishA,int startB,int finishB,int *startD,int *finishD)
{
float coefficent;
*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:
{
coefficent=terms[startA].coef+terms[startB].coef;
if(coefficent)
attach(coefficent,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;
}
void polyprint(int start,int finish)
{
int i;
printf("\n%.2fx^%d ",terms[start].coef,terms[start].expon);
for(i=start+1;i<=finish;i++)
{
if(terms[i].expon==0)
printf("+ %.2f ",terms[i].coef);
else
printf("+ %.2fx^%d ",terms[i].coef,terms[i].expon);
}
printf("\n");
}
void main()
{
int n1,n2,startA,finishA,startB,finishB,startD,finishD;
int i,j;
printf("\nEnter number of terms in polynomial 1: ");
scanf("%d",&n1);
printf("\nEnter number of terms in polynomial 2: ");
scanf("%d",&n2);
startA=0;
finishA=n1-1;
startB=n1;
finishB=n1+n2-1;
avail=n1+n2;
printf("\nEnter terms in POLYNOMIAL 1\n");
for(i=startA;i<=finishA;i++)
{
printf("\nCoefficient: ");
scanf("%f",&terms[i].coef);
printf("\nExponent: ");
scanf("%d",&terms[i].expon);
}
printf("\nEnter terms in POLYNOMIAL 2\n");
for(i=startB;i<=finishB;i++)
{
printf("\nCoefficient: ");
scanf("%f",&terms[i].coef);
printf("\nExponent: ");
scanf("%d",&terms[i].expon);
}
printf("\n\tPOLYNOMIAL 1\n");
polyprint(startA,finishA);
printf("\n\tPOLYNOMIAL 2\n");
polyprint(startB,finishB);
padd(startA,finishA,startB,finishB,&startD,&finishD);
printf("\n\tPOLYNOMIAL SUM\n");
polyprint(startD,finishD);
printf("\n");
}