Implementation of [Addition of two polynomials] (https://en.wikipedia.org/wiki/Polynomial#Addition)
More...
#include <stdio.h>
#include <stdlib.h>
|
struct | term |
| identifier for single-variable polynomial coefficients as a linked list More...
|
|
|
void | free_poly (struct term *poly) |
| Frees memory space. More...
|
|
void | create_polynomial (struct term **poly, int coef, int pow) |
| The function will create a polynomial. More...
|
|
void | poly_add (struct term **pol, struct term *poly1, struct term *poly2) |
| The function will add 2 polynomials. More...
|
|
void | display_polynomial (struct term *poly) |
| The function will display the polynomial. More...
|
|
static void | test1 (struct term *poly1, struct term *poly2, struct term *poly3) |
| Test function 1. More...
|
|
static void | test2 (struct term *poly1, struct term *poly2, struct term *poly3) |
| Test function 2. More...
|
|
static void | test3 (struct term *poly1, struct term *poly2, struct term *poly3) |
| Test function 3. More...
|
|
int | main (void) |
| Main function. More...
|
|
Implementation of [Addition of two polynomials] (https://en.wikipedia.org/wiki/Polynomial#Addition)
- Author
- Ankita Roy Chowdhury
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.
◆ create_polynomial()
void create_polynomial |
( |
struct term ** |
poly, |
|
|
int |
coef, |
|
|
int |
pow |
|
) |
| |
The function will create a polynomial.
- Parameters
-
poly | stores the address of the polynomial being created |
coef | contains the coefficient of the node |
pow | contains the degree |
- Returns
- none
55{
56
57 struct term *temp1, *temp2;
58 temp1 = *poly;
59
60
61 if (temp1 == NULL)
62 {
67
68
69 *poly = temp2;
71 }
72
73 else
74 {
81 }
82}
#define malloc(bytes)
This macro replace the standard malloc function with malloc_dbg.
Definition: malloc_dbg.h:18
identifier for single-variable polynomial coefficients as a linked list
Definition: poly_add.c:20
int pow
power of the polynomial term
Definition: poly_add.c:22
int coef
coefficient value
Definition: poly_add.c:21
struct term * next
pointer to the successive term
Definition: poly_add.c:23
◆ display_polynomial()
void display_polynomial |
( |
struct term * |
poly | ) |
|
The function will display the polynomial.
- Parameters
-
poly | first term of the polynomial to be displayed |
- Returns
- none
183{
184 while (poly != NULL)
185 {
186 printf(
"%d x^%d", poly->
coef, poly->
pow);
188 if (poly != NULL)
189 {
190 printf(" + ");
191 }
192 }
193}
◆ free_poly()
void free_poly |
( |
struct term * |
poly | ) |
|
Frees memory space.
- Parameters
-
poly | first term of polynomial |
- Returns
- void
32{
33 if (!poly)
34 {
35 return;
36 }
37 else
38 {
40 {
42 }
44 }
45}
#define free(ptr)
This macro replace the standard free function with free_dbg.
Definition: malloc_dbg.h:26
◆ main()
Main function.
- Returns
- 0 on exit
312{
313 struct term *poly1 = NULL, *poly2 = NULL, *poly3 = NULL;
314 test1(poly1, poly2, poly3);
315 test2(poly1, poly2, poly3);
316 test3(poly1, poly2, poly3);
317
318 return 0;
319}
void test2()
Definition: k_means_clustering.c:356
void test1()
Test that creates a random set of points distributed in four clusters in 2D space and trains an SOM t...
Definition: kohonen_som_topology.c:406
void test3()
Test that creates a random set of points distributed in eight clusters in 3D space and trains an SOM ...
Definition: kohonen_som_topology.c:609
◆ poly_add()
void poly_add |
( |
struct term ** |
pol, |
|
|
struct term * |
poly1, |
|
|
struct term * |
poly2 |
|
) |
| |
The function will add 2 polynomials.
- Parameters
-
poly1 | first polynomial of the addition |
poly2 | second polynomial of the addition |
pol | the resultant polynomial |
92{
93
95 temp->next = NULL;
96 *pol =
97 temp;
98
99
100
101 while (poly1 && poly2)
102 {
103
104
105
106
107 if (poly1->
pow > poly2->
pow)
108 {
109 temp->coef = poly1->
coef;
110 temp->pow = poly1->
pow;
112 }
113
114
115
116
117 else if (poly1->
pow < poly2->
pow)
118 {
119 temp->coef = poly2->
coef;
120 temp->pow = poly2->
pow;
122 }
123
124
125
126
127 else
128 {
129 temp->coef = poly1->
coef + poly2->
coef;
130 temp->pow = poly1->
pow;
133 }
134
135
136
137 if (poly1 && poly2)
138 {
140 sizeof(
struct term));
141 temp = temp->next;
142 temp->next = NULL;
143 }
144 }
145
146
147
148
149 while (poly1 || poly2)
150 {
152 sizeof(
struct term));
153 temp = temp->next;
154 temp->next = NULL;
155
156
157
158
159 if (poly1)
160 {
161 temp->coef = poly1->
coef;
162 temp->pow = poly1->
pow;
164 }
165
166
167
168 else if (poly2)
169 {
170 temp->coef = poly2->
coef;
171 temp->pow = poly2->
pow;
173 }
174 }
175}
◆ test1()
static void test1 |
( |
struct term * |
poly1, |
|
|
struct term * |
poly2, |
|
|
struct term * |
poly3 |
|
) |
| |
|
static |
Test function 1.
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
205{
206 printf("\n----Test 1----\n");
207 printf("\nFirst Polynomial:\n");
212
213 printf("\nSecond Polynomial:\n");
218
220 printf("\nResultant polynomial:\n");
222 printf("\n");
223
224
228}
void create_polynomial(struct term **poly, int coef, int pow)
The function will create a polynomial.
Definition: poly_add.c:54
void free_poly(struct term *poly)
Frees memory space.
Definition: poly_add.c:31
void poly_add(struct term **pol, struct term *poly1, struct term *poly2)
The function will add 2 polynomials.
Definition: poly_add.c:91
void display_polynomial(struct term *poly)
The function will display the polynomial.
Definition: poly_add.c:182
◆ test2()
static void test2 |
( |
struct term * |
poly1, |
|
|
struct term * |
poly2, |
|
|
struct term * |
poly3 |
|
) |
| |
|
static |
Test function 2.
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
240{
241 printf("\n----Test 2----\n");
242 printf("\nFirst Polynomial:\n");
248
250
251 printf("\nSecond Polynomial:\n");
256
258
260 printf("\nResultant polynomial:\n");
262 printf("\n");
263
264
268}
◆ test3()
static void test3 |
( |
struct term * |
poly1, |
|
|
struct term * |
poly2, |
|
|
struct term * |
poly3 |
|
) |
| |
|
static |
Test function 3.
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
280{
281 printf("\n----Test 3----\n");
282 printf("\nFirst Polynomial:\n");
286
288
289 printf("\nSecond Polynomial:\n");
293
295
297 printf("\nResultant polynomial:\n");
299 printf("\n");
300
301
305}