Implementation of Addition of two polynomials
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
- 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
57 struct term *temp1, *temp2;
#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
186 printf(
"%d x^%d", poly->
coef, poly->
pow);
◆ free_poly()
void free_poly |
( |
struct term * |
poly | ) |
|
Frees memory space.
- Parameters
-
poly | first term of polynomial |
- Returns
- void
#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
313 struct term *poly1 = NULL, *poly2 = NULL, *poly3 = NULL;
314 test1(poly1, poly2, poly3);
315 test2(poly1, poly2, poly3);
316 test3(poly1, poly2, poly3);
static void test2(struct term *poly1, struct term *poly2, struct term *poly3)
Test function 2.
Definition: poly_add.c:239
static void test3(struct term *poly1, struct term *poly2, struct term *poly3)
Test function 3.
Definition: poly_add.c:279
static void test1(struct term *poly1, struct term *poly2, struct term *poly3)
Test function 1.
Definition: poly_add.c:204
◆ 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 |
101 while (poly1 && poly2)
107 if (poly1->
pow > poly2->
pow)
109 temp->coef = poly1->
coef;
110 temp->pow = poly1->
pow;
117 else if (poly1->
pow < poly2->
pow)
119 temp->coef = poly2->
coef;
120 temp->pow = poly2->
pow;
129 temp->coef = poly1->
coef + poly2->
coef;
130 temp->pow = poly1->
pow;
140 sizeof(
struct term));
149 while (poly1 || poly2)
152 sizeof(
struct term));
161 temp->coef = poly1->
coef;
162 temp->pow = poly1->
pow;
170 temp->coef = poly2->
coef;
171 temp->pow = poly2->
pow;
◆ 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
206 printf(
"\n----Test 1----\n");
207 printf(
"\nFirst Polynomial:\n");
213 printf(
"\nSecond Polynomial:\n");
220 printf(
"\nResultant polynomial:\n");
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
241 printf(
"\n----Test 2----\n");
242 printf(
"\nFirst Polynomial:\n");
251 printf(
"\nSecond Polynomial:\n");
260 printf(
"\nResultant polynomial:\n");
◆ 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
281 printf(
"\n----Test 3----\n");
282 printf(
"\nFirst Polynomial:\n");
289 printf(
"\nSecond Polynomial:\n");
297 printf(
"\nResultant polynomial:\n");