-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
/
Copy pathpoly_add.c
301 lines (273 loc) · 8.28 KB
/
poly_add.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/**
* @file
* @brief Implementation of [Addition of two polynomials]
* (https://en.wikipedia.org/wiki/Polynomial#Addition)
* @author [Ankita Roy Chowdhury](https://github.com/Ankita19ms0010)
* @details
* This code takes two polynomials as input
* and prints their sum using linked list.
* The polynomials must be in increasing or decreasing order of degree.
* Degree must be positive.
*/
#include <stdio.h> // for io operations
#include <stdlib.h>
/**
* @brief identifier for single-variable polynomial coefficients as a linked
* list
*/
struct term
{
int coef; /**< coefficient value */
int pow; /**< power of the polynomial term */
struct term *next; /**< pointer to the successive term */
};
/**
* @brief Frees memory space
* @param poly first term of polynomial
* @returns void
*/
void free_poly(struct term *poly)
{
while (poly)
{
struct term *next = poly->next;
free(poly);
poly = next;
}
}
/**
* The function will create a polynomial
* @param poly stores the address of the polynomial being created
* @param coef contains the coefficient of the node
* @param pow contains the degree
* @returns none
*/
void create_polynomial(struct term **poly, int coef, int pow)
{
// Creating the polynomial using temporary linked lists
struct term **temp1 = poly;
while (*temp1)
{
temp1 = &(*temp1)->next;
}
// Now temp1 reaches to the end of the list
*temp1 = (struct term *)malloc(
sizeof(struct term)); // Create the term and linked as the tail
(*temp1)->coef = coef;
(*temp1)->pow = pow;
(*temp1)->next = NULL;
}
/**
* The function will add 2 polynomials
* @param poly1 first polynomial of the addition
* @param poly2 second polynomial of the addition
* @param pol the resultant polynomial
*/
void poly_add(struct term **pol, struct term *poly1, struct term *poly2)
{
// Creating a temporary linked list to store the resultant polynomial
struct term *temp = (struct term *)malloc(sizeof(struct term));
temp->next = NULL;
*pol =
temp; //*pol always points to the 1st node of the resultant polynomial
// Comparing the powers of the nodes of both the polynomials
// until one gets exhausted
while (poly1 && poly2)
{
/* If the power of the first polynomial is greater than the power of the
second one place the power and coefficient of that node of the first
polynomial in temp and increase the pointer poly1
*/
if (poly1->pow > poly2->pow)
{
temp->coef = poly1->coef;
temp->pow = poly1->pow;
poly1 = poly1->next;
}
/* If the power of the second polynomial is greater than the power of
the first one place the power and coefficient of that node of the
second polynomial in temp and increase the pointer poly2
*/
else if (poly1->pow < poly2->pow)
{
temp->coef = poly2->coef;
temp->pow = poly2->pow;
poly2 = poly2->next;
}
/* If both of them have same power then sum the coefficients
place both the summed coefficient and the power in temp
increase both the pointers poly1 and poly2
*/
else
{
temp->coef = poly1->coef + poly2->coef;
temp->pow = poly1->pow;
poly1 = poly1->next;
poly2 = poly2->next;
}
/* If none of the polynomials are exhausted
dynamically create a node in temp
*/
if (poly1 && poly2)
{
temp->next = (struct term *)malloc(
sizeof(struct term)); // Dynamic node creation
temp = temp->next; // Increase the pointer temp
temp->next = NULL;
}
}
/* If one of the polynomials is exhausted
place the rest of the other polynomial as it is in temp
by creating nodes dynamically
*/
while (poly1 || poly2)
{
temp->next = (struct term *)malloc(
sizeof(struct term)); // Dynamic node creation
temp = temp->next; // Increasing the pointer
temp->next = NULL;
/* If poly1 is not exhausted
place rest of that polynomial in temp
*/
if (poly1)
{
temp->coef = poly1->coef;
temp->pow = poly1->pow;
poly1 = poly1->next;
}
/* If poly2 is not exhausted
place rest of that polynomial in temp
*/
else if (poly2)
{
temp->coef = poly2->coef;
temp->pow = poly2->pow;
poly2 = poly2->next;
}
}
}
/**
* The function will display the polynomial
* @param poly first term of the polynomial to be displayed
* @returns none
*/
void display_polynomial(struct term *poly)
{
while (poly != NULL)
{
printf("%d x^%d", poly->coef, poly->pow);
poly = poly->next;
if (poly != NULL)
{
printf(" + ");
}
}
}
/**
* @brief Test function 1
*
* @details
* Polynomial 1 is 5 x^2 + 3 x^1 + 2 x^0
* Polynomial 2 is 7 x^3 + 9 x^1 + 10 x^0
* Resultant polynomial is 7 x^3 + 5 x^2 + 12 x^1 + 12 x^0
* @returns void
*/
static void test1(struct term *poly1, struct term *poly2, struct term *poly3)
{
printf("\n----Test 1----\n");
printf("\nFirst Polynomial:\n"); // Defining the 1st polynomial
create_polynomial(&poly1, 5, 2);
create_polynomial(&poly1, 3, 1);
create_polynomial(&poly1, 2, 0);
display_polynomial(poly1);
printf("\nSecond Polynomial:\n"); // Defining the 2nd polynomial
create_polynomial(&poly2, 7, 3);
create_polynomial(&poly2, 9, 1);
create_polynomial(&poly2, 10, 0);
display_polynomial(poly2);
poly_add(&poly3, poly1, poly2); // Adding the two polynomials
printf("\nResultant polynomial:\n");
display_polynomial(poly3);
printf("\n");
// Frees memory space
free_poly(poly1);
free_poly(poly2);
free_poly(poly3);
}
/**
* @brief Test function 2
*
* @details
* Polynomial 1 is 3 x^5 + 1 x^4 + 2 x^3 + -2 x^1 + 5 x^0
* Polynomial 2 is 2 x^5 + 3 x^3 + 7 x^1 + 2 x^0
* Resultant polynomial is 5 x^5 + 1 x^4 + 5 x^3 + 5 x^1 + 7 x^0
* @returns void
*/
static void test2(struct term *poly1, struct term *poly2, struct term *poly3)
{
printf("\n----Test 2----\n");
printf("\nFirst Polynomial:\n"); // Defining the 1st polynomial
create_polynomial(&poly1, 3, 5);
create_polynomial(&poly1, 1, 4);
create_polynomial(&poly1, 2, 3);
create_polynomial(&poly1, -2, 1);
create_polynomial(&poly1, 5, 0);
display_polynomial(poly1);
printf("\nSecond Polynomial:\n"); // Defining the 2nd polynomial
create_polynomial(&poly2, 2, 5);
create_polynomial(&poly2, 3, 3);
create_polynomial(&poly2, 7, 1);
create_polynomial(&poly2, 2, 0);
display_polynomial(poly2);
poly_add(&poly3, poly1, poly2); // Adding the two polynomials
printf("\nResultant polynomial:\n");
display_polynomial(poly3);
printf("\n");
// Frees memory space
free_poly(poly1);
free_poly(poly2);
free_poly(poly3);
}
/**
* @brief Test function 3
*
* @details
* Polynomial 1 is -12 x^0 + 8 x^1 + 4 x^3
* Polynomial 2 is 5 x^0 + -13 x^1 + 3 x^3
* Resultant polynomial is -7 x^0 + -5 x^1 + 7 x^3
* @returns void
*/
static void test3(struct term *poly1, struct term *poly2, struct term *poly3)
{
printf("\n----Test 3----\n");
printf("\nFirst Polynomial:\n"); // Defining the 1st polynomial
create_polynomial(&poly1, -12, 0);
create_polynomial(&poly1, 8, 1);
create_polynomial(&poly1, 4, 3);
display_polynomial(poly1);
printf("\nSecond Polynomial:\n"); // Defining the 2nd polynomial
create_polynomial(&poly2, 5, 0);
create_polynomial(&poly2, -13, 1);
create_polynomial(&poly2, 3, 3);
display_polynomial(poly2);
poly_add(&poly3, poly1, poly2); // Adding the two polynomials
printf("\nResultant polynomial:\n");
display_polynomial(poly3);
printf("\n");
// Frees memory space
free_poly(poly1);
free_poly(poly2);
free_poly(poly3);
}
/**
* @brief Main function
* @returns 0 on exit
*/
int main(void)
{
struct term *poly1 = NULL, *poly2 = NULL, *poly3 = NULL;
test1(poly1, poly2, poly3);
test2(poly1, poly2, poly3);
test3(poly1, poly2, poly3);
return 0;
}