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.
|
|
void | create_polynomial (struct term **poly, int coef, int pow) |
| The function will create a polynomial.
|
|
void | poly_add (struct term **pol, struct term *poly1, struct term *poly2) |
| The function will add 2 polynomials.
|
|
void | display_polynomial (struct term *poly) |
| The function will display the polynomial.
|
|
static void | test1 (struct term *poly1, struct term *poly2, struct term *poly3) |
| Test function 1.
|
|
static void | test2 (struct term *poly1, struct term *poly2, struct term *poly3) |
| Test function 2.
|
|
static void | test3 (struct term *poly1, struct term *poly2, struct term *poly3) |
| Test function 3.
|
|
int | main (void) |
| Main function.
|
|
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
49{
50
51 struct term **temp1 = poly;
52
53 while (*temp1)
54 {
55 temp1 = &(*temp1)->
next;
56 }
57
58
63 (*temp1)->next = NULL;
64}
#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
165{
166 while (poly != NULL)
167 {
168 printf(
"%d x^%d", poly->
coef, poly->
pow);
170 if (poly != NULL)
171 {
172 printf(" + ");
173 }
174 }
175}
◆ free_poly()
void free_poly |
( |
struct term * |
poly | ) |
|
Frees memory space.
- Parameters
-
poly | first term of polynomial |
- Returns
- void
32{
33 while (poly)
34 {
38 }
39}
#define free(ptr)
This macro replace the standard free function with free_dbg.
Definition malloc_dbg.h:26
int next(Vector *vec)
This function gets the next item from the Vector each time it's called.
Definition vector.c:102
◆ main()
Main function.
- Returns
- 0 on exit
294{
295 struct term *poly1 = NULL, *poly2 = NULL, *poly3 = NULL;
296 test1(poly1, poly2, poly3);
297 test2(poly1, poly2, poly3);
298 test3(poly1, poly2, poly3);
299
300 return 0;
301}
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 |
74{
75
78 *pol =
79 temp;
80
81
82
83 while (poly1 && poly2)
84 {
85
86
87
88
89 if (poly1->
pow > poly2->
pow)
90 {
91 temp->coef = poly1->
coef;
92 temp->pow = poly1->
pow;
94 }
95
96
97
98
99 else if (poly1->
pow < poly2->
pow)
100 {
101 temp->coef = poly2->
coef;
102 temp->pow = poly2->
pow;
104 }
105
106
107
108
109 else
110 {
111 temp->coef = poly1->
coef + poly2->
coef;
112 temp->pow = poly1->
pow;
115 }
116
117
118
119 if (poly1 && poly2)
120 {
122 sizeof(
struct term));
125 }
126 }
127
128
129
130
131 while (poly1 || poly2)
132 {
134 sizeof(
struct term));
137
138
139
140
141 if (poly1)
142 {
143 temp->coef = poly1->
coef;
144 temp->pow = poly1->
pow;
146 }
147
148
149
150 else if (poly2)
151 {
152 temp->coef = poly2->
coef;
153 temp->pow = poly2->
pow;
155 }
156 }
157}
struct node * next
List pointers.
Definition bfs.c:24
◆ 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
187{
188 printf("\n----Test 1----\n");
189 printf("\nFirst Polynomial:\n");
194
195 printf("\nSecond Polynomial:\n");
200
202 printf("\nResultant polynomial:\n");
204 printf("\n");
205
206
210}
void create_polynomial(struct term **poly, int coef, int pow)
The function will create a polynomial.
Definition poly_add.c:48
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:73
void display_polynomial(struct term *poly)
The function will display the polynomial.
Definition poly_add.c:164
◆ 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
222{
223 printf("\n----Test 2----\n");
224 printf("\nFirst Polynomial:\n");
230
232
233 printf("\nSecond Polynomial:\n");
238
240
242 printf("\nResultant polynomial:\n");
244 printf("\n");
245
246
250}
◆ 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
262{
263 printf("\n----Test 3----\n");
264 printf("\nFirst Polynomial:\n");
268
270
271 printf("\nSecond Polynomial:\n");
275
277
279 printf("\nResultant polynomial:\n");
281 printf("\n");
282
283
287}