forked from DipankarSaha94/Hacktoberfest20200
-
Notifications
You must be signed in to change notification settings - Fork 0
/
polynomialMmultiplicationCPP.cpp
97 lines (82 loc) · 1.55 KB
/
polynomialMmultiplicationCPP.cpp
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
#include<iostream>
using namespace std;
struct Node{
int coeff;
int power;
Node* next;
};
Node* addNode(Node* n, int c, int p) {
Node* newNode = new Node();
newNode->coeff = c;
newNode->power = p;
newNode->next = NULL;
if (!n)return newNode;
Node* pt = n;
while(pt->next != NULL) {
pt = pt->next;
}
pt->next = newNode;
return n;
}
void printPolynomial(Node* n) {
while (n->next) {
cout << n->coeff << "x^" << n->power << "+";
n = n->next;
}
cout << n->coeff << "\n";
}
void removeduplicate(Node* a) {
Node* p1, * p2, * dup;
p1 = a;
while (p1 && p1->next)
{
p2 = p1;
while (p2->next) {
if (p1->power == p2->next->power) {
p1->coeff = p1->coeff + p2->next->coeff;
dup = p2->next;
p2->next = p2->next->next;
delete(dup);
}
else
p2 = p2->next;
}
p1 = p1->next;
}
}
Node* multiply(Node* a, Node* b, Node* c) {
Node* p1, * p2;
p1 = a;
p2 = b;
while (p1)
{
while (p2)
{
int co, po;
co = p1->coeff * p2->coeff;
po = p1->power + p2->power;
c = addNode(c, co, po);
p2 = p2->next;
}
p2 = b;
p1 = p1->next;
}
removeduplicate(c);
return c;
}
int main() {
Node* poly1 = NULL; Node* poly2 = NULL; Node* poly3 = NULL;
poly1 = addNode(poly1, 3, 2);
poly1 = addNode(poly1, 5, 1);
poly1 = addNode(poly1, 6, 0);
poly2 = addNode(poly2, 6, 1);
poly2 = addNode(poly2, 8, 0);
cout << "1st Polynomial :";
printPolynomial(poly1);
cout << "2nd Polynomial :";
printPolynomial(poly2);
poly3 = multiply(poly1, poly2, poly3);
cout << "resultant Polynomial : ";
printPolynomial(poly3);
return 0;
}