-
Notifications
You must be signed in to change notification settings - Fork 1
/
poly_add.c
128 lines (110 loc) · 3.31 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
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
// Structure to represent a term in a polynomial
struct Node {
int coefficient;
int exponent;
struct Node* next;
};
typedef struct Node Node;
// Function to insert a term into a polynomial
void insertTerm(Node** poly, int coeff, int exp) {
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->coefficient = coeff;
newNode->exponent = exp;
newNode->next = NULL;
if (*poly == NULL) {
*poly = newNode;
} else {
Node* current = *poly;
while (current->next != NULL) {
current = current->next;
}
current->next = newNode;
}
}
// Function to add two polynomials
Node* addPolynomials(Node* poly1, Node* poly2) {
Node* result = NULL;
while (poly1 != NULL && poly2 != NULL) {
if (poly1->exponent > poly2->exponent) {
insertTerm(&result, poly1->coefficient, poly1->exponent);
poly1 = poly1->next;
} else if (poly2->exponent > poly1->exponent) {
insertTerm(&result, poly2->coefficient, poly2->exponent);
poly2 = poly2->next;
} else {
int sumCoeff = poly1->coefficient + poly2->coefficient;
if (sumCoeff != 0) {
insertTerm(&result, sumCoeff, poly1->exponent);
}
poly1 = poly1->next;
poly2 = poly2->next;
}
}
// Append any remaining terms from poly1 and poly2
while (poly1 != NULL) {
insertTerm(&result, poly1->coefficient, poly1->exponent);
poly1 = poly1->next;
}
while (poly2 != NULL) {
insertTerm(&result, poly2->coefficient, poly2->exponent);
poly2 = poly2->next;
}
return result;
}
// Function to display a polynomial
void displayPolynomial(Node* poly) {
if (poly == NULL) {
printf("0");
return;
}
while (poly != NULL) {
printf("%dx^%d", poly->coefficient, poly->exponent);
if (poly->next != NULL) {
printf(" + ");
}
poly = poly->next;
}
}
int main() {
Node* poly1 = NULL;
Node* poly2 = NULL;
int n, coeff, exp;
//printf("Enter the number of terms in the first polynomial: ");
scanf("%d", &n);
//printf("Enter the coefficients and exponents for the first polynomial:\n");
for (int i = 0; i < n; i++) {
scanf("%d %d", &coeff, &exp);
insertTerm(&poly1, coeff, exp);
}
//printf("Enter the number of terms in the second polynomial: ");
scanf("%d", &n);
//printf("Enter the coefficients and exponents for the second polynomial:\n");
for (int i = 0; i < n; i++) {
scanf("%d %d", &coeff, &exp);
insertTerm(&poly2, coeff, exp);
}
Node* sum = addPolynomials(poly1, poly2);
//printf("Sum of the two polynomials: ");
displayPolynomial(sum);
// Free the memory
while (poly1 != NULL) {
Node* temp = poly1;
poly1 = poly1->next;
free(temp);
}
while (poly2 != NULL) {
Node* temp = poly2;
poly2 = poly2->next;
free(temp);
}
while (sum != NULL) {
Node* temp = sum;
sum = sum->next;
free(temp);
}
return 0;
}