-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPolynomial.cpp
More file actions
113 lines (94 loc) · 2.5 KB
/
Polynomial.cpp
File metadata and controls
113 lines (94 loc) · 2.5 KB
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
#include<bits/stdc++.h>
using namespace std;
class node{
public:
int coefficient;
int exponent;
node* next;
node(int coeff,int exp){
coefficient = coeff;
exponent = exp;
next = NULL;
}
};
node* addPolynomial(node* poly1,node* poly2){
node* result = new node(0,0);
node* current = result;
while (poly1 != NULL || poly2 != NULL)
{
/* code */
int coeff1 = (poly1 != NULL) ? poly1->coefficient : 0;
int exp1 = (poly1 != NULL) ? poly1->exponent : 0;
int coeff2 = (poly2 != NULL) ? poly2->coefficient : 0;
int exp2 = (poly2 != NULL) ? poly2->exponent : 0;
if (exp1 == exp2){
int sumCoeff = coeff1 + coeff2;
if (sumCoeff != 0){
current -> next = new node(sumCoeff,exp1);
current = current -> next;
}
if (poly1 != NULL)
poly1 = poly1 -> next;
if (poly2 != NULL)
poly2 = poly2 -> next;
}
else if (exp1 > exp2){
current -> next = new node(coeff1,exp1);
current = current -> next;
if (poly1 != NULL)
poly1 = poly1 -> next;
}
else{
current -> next = new node(coeff2,exp2);
current = current -> next;
if (poly2 != NULL)
poly2 = poly2 -> next;
}
}
return result->next;
}
void deletePoly(node* poly){
node* current = poly;
while (current != NULL)
{
/* code */
node* newNode = current -> next;
free(current);
current = newNode;
}
}
void displayPolynomial(node* poly){
while (poly != NULL)
{
/* code */
cout<<poly->coefficient;
if (poly->exponent != 0){
cout<<"X^"<<poly->exponent;
}
poly = poly -> next;
if (poly != NULL){
cout<<" + ";
}
}
cout<<endl;
}
int main(){
node* poly1 = new node(2,3);
poly1->next = new node(3,2);
poly1->next->next = new node(1,1);
poly1->next->next->next = new node(5,0);
node* poly2 = new node(7,2);
poly2->next = new node(-3,1);
poly2->next->next = new node(7,0);
cout<<"Polynomial 1: ";
displayPolynomial(poly1);
cout<<"Polynomial 2: ";
displayPolynomial(poly2);
node* result = addPolynomial(poly1,poly2);
cout<<"Result: ";
displayPolynomial(result);
// Free up memory
deletePoly(poly1);
deletePoly(poly2);
deletePoly(result);
}