-
Notifications
You must be signed in to change notification settings - Fork 0
/
polyAdd.c
79 lines (69 loc) · 1.63 KB
/
polyAdd.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
#include <stdio.h>
int p1len, p2len, starta, startb, startc;
struct poly {
int coeff;
int expo;
} p[100];
void read(struct poly p[100], int n, int start){
for(int i = start; i < n; i++){
printf("\n");
printf("Enter The Coefficient And Exponent Value Of t%d: ", i+1);
scanf("%d %d", &p[i].coeff, &p[i].expo);
}
}
void display(int n, int start){
for(int i = start; i < n; i++){
if (p[i].expo != 0){
printf("%dx^%d", p[i].coeff, p[i].expo);
}
else
printf("%d", p[i].coeff);
if((i + 1) != n)
printf(" + ");
}
}
int add(int a, int b, int c){
while((a < p1len) && (b < (p1len + p2len))){
if (p[a].expo == p[b].expo){
p[c].expo = p[a].expo;
p[c++].coeff = p[a++].coeff + p[b++].coeff;
}
else if (p[a].expo > p[b].expo){
p[c].expo = p[a].expo;
p[c++].coeff = p[a++].coeff;
}
else{
p[c].expo = p[b].expo;
p[c++].coeff = p[b++].coeff;
}
}
while(a != p1len){
p[c].expo = p[a].expo;
p[c++].coeff = p[a++].coeff;
}
while(b < (p1len + p2len)){
p[c].expo = p[b].expo;
p[c++].coeff = p[b++].coeff;
}
return c;
}
void main(){
printf("\nEnter The Number Of Terms In P1: ");
scanf("%d", &p1len);
read(p,p1len,0);
printf("\nEnter The Number Of Terms In P2: ");
scanf("%d", &p2len);
read(p,(p1len + p2len),p1len);
starta = 0;
startb = p1len;
startc = p1len + p2len ;
int endb = (p1len+p2len);
int endc = add(starta, startb, startc);
printf("\nPolynomial 1 = ");
display(startb, 0);
printf("\nPolynomial 2 = ");
display(endb, startb);
printf("\nSum Of P1 & P2 = ");
display(endc, endb);
printf("\n");
}