-
Notifications
You must be signed in to change notification settings - Fork 1
/
6_18_polysub.c
144 lines (127 loc) · 3.25 KB
/
6_18_polysub.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include <stdio.h>
struct poly
{
int coeff;
int exp;
};
int main()
{
struct poly p1[10], p2[10], p3[10];
int t1, t2, i, j, k;
printf("Enter the Number of elements in P1: ");
scanf("%d", &t1);
for (i = 0; i < t1; i++)
{
printf("Enter the coefficient and exponent for %d term of P1: ", i + 1);
scanf("%d^%d", &p1[i].coeff, &p1[i].exp);
}
printf("Enter the Number of elements in P2: ");
scanf("%d", &t2);
for (i = 0; i < t2; i++)
{
printf("Enter the coefficient and exponent for %d term of P2: ", i + 1);
scanf("%d^%d", &p2[i].coeff, &p2[i].exp);
}
i = 0;
j = 0;
k = 0;
while (i < t1 && j < t2)
{
if (p1[i].exp == p2[j].exp)
{
p3[k].coeff = p1[i].coeff - p2[j].coeff; // Subtract coefficients
p3[k].exp = p1[i].exp;
k++;
i++;
j++;
}
else if (p1[i].exp > p2[j].exp)
{
p3[k].coeff = p1[i].coeff;
p3[k].exp = p1[i].exp;
k++;
i++;
}
else
{
p3[k].coeff = -p2[j].coeff; // Negate the coefficient for subtraction
p3[k].exp = p2[j].exp;
k++;
j++;
}
}
while (i < t1)
{
p3[k].coeff = p1[i].coeff;
p3[k].exp = p1[i].exp;
k++;
i++;
}
while (j < t2)
{
p3[k].coeff = -p2[j].coeff;
p3[k].exp = p2[j].exp;
k++;
j++;
}
printf("======POLYNOMIAL SUBTRACTION=====\n");
#include <stdio.h>
int main() {
int p1[100], p2[100], p3[100], i, j, k, t1, t2;
printf("Enter the total number of terms in the first polynomial: ");
scanf("%d", &t1);
printf("Enter the coefficient and exponent of each term in the first polynomial:\n");
for (i = 0; i < t1; i++) {
scanf("%dx^%d", &p1[i], &j);
}
printf("\nEnter the total number of terms in the second polynomial: ");
scanf("%d", &t2);
printf("Enter the coefficient and exponent of each term in the second polynomial:\n");
for (i = 0; i < t2; i++) {
scanf("%dx^%d", &p2[i], &j);
}
i = 0;
j = 0;
k = 0;
while (i < t1 && j < t2) {
if (p1[i + 1] == p2[j + 1]) {
p3[k] = p1[i] + p2[j];
p3[k + 1] = p1[i + 1];
i += 2;
j += 2;
} else if (p1[i + 1] > p2[j + 1]) {
p3[k] = p1[i];
p3[k + 1] = p1[i + 1];
i += 2;
} else {
p3[k] = p2[j];
p3[k + 1] = p2[j + 1];
j += 2;
}
k += 2;
}
while (i < t1) {
p3[k] = p1[i];
p3[k + 1] = p1[i + 1];
i += 2;
k += 2;
}
while (j < t2) {
p3[k] = p2[j];
p3[k + 1] = p2[j + 1];
j += 2;
k += 2;
}
printf("\nThe sum of the two polynomials is:\n");
for (i = 0; i < k; i++) {
if (p3[i].coeff != 0) {
printf("%dx^%d", p3[i].coeff, i);
if (i != k - 1 && p3[i + 1].coeff >= 0) {
printf(" + ");
}
}
}
}
printf("\n");
return 0;
}