-
Notifications
You must be signed in to change notification settings - Fork 357
/
PolynomialAddition.cpp
136 lines (125 loc) · 3.63 KB
/
PolynomialAddition.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
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
129
130
131
132
133
134
135
136
// Github username: Ansh-Kushwaha
// Aim: To store and add polynomials
// Date: 05/10/2022
#include <iostream>
using namespace std;
struct term{
int pow, coeff;
term *next;
};
typedef struct polynomial{
term* addTerm(term *first, int p, int c){ //adding term to the polynomial
if(p<0){
cout << "Undefined power" << endl;
return first;
}
if(first == NULL){
term *tmp = new term;
tmp->pow = p;
tmp->coeff = c;
tmp->next = first;
first = tmp;
return first;
}
else{
int pos = 1;
term *q = first;
term *nxt = NULL;
while(q->next!=NULL){
nxt = q->next;
if(p > q->pow)
break;
else if(p < q->pow && p > nxt->pow){
pos++;
break;
}
else{
q = q->next;
pos++;
}
}
if(p < q->pow && nxt == NULL){
pos++;
}
if(pos==1){
term* tmp = new term;
tmp->pow = p;
tmp->coeff = c;
tmp->next = q;
q = tmp;
return q;
}
else{
term *tmp = new term;
tmp->pow = p;
tmp->coeff = c;
tmp->next = q->next;
q->next = tmp;
return first;
}
}
return first;
}
term* add(term* one, term* two, term* res){ //combining two polynomials
while(one!=NULL && two!=NULL){
if(one->pow == two->pow){
res = addTerm(res, one->pow, (one->coeff+two->coeff));
one = one->next;
two = two->next;
}
else if(one->pow < two->pow){
res = addTerm(res, two->pow, two->coeff);
two = two->next;
}
else if(one->pow > two->pow){
res = addTerm(res, one->pow, one->coeff);
one = one->next;
}
}
while(one!=NULL || two!=NULL){
if(one!= NULL){
res = addTerm(res, one->pow, one->coeff);
one = one->next;
}
if(two!= NULL){
res = addTerm(res, two->pow, two->coeff);
two = two->next;
}
}
return res;
}
void printPoly(term *first){ //function to print the stored polynomial
char var = 'x';
while(first!=NULL){
cout <<first->coeff << var << "^" << first->pow << " ";
first = first->next;
if(!(first == NULL))
cout << "+ ";
}
cout << endl;
}
}poly; //Data Structure (Linked List) based polynomial addition
int main(){
term *first1 = NULL;
poly p1;
first1 = p1.addTerm(first1, 0, 1);
first1 = p1.addTerm(first1, 5, 2);
first1 = p1.addTerm(first1, 3, 4);
first1 = p1.addTerm(first1, 4, 9);
term *first2 = NULL;
poly p2;
first2 = p2.addTerm(first2, 0, 2);
first2 = p2.addTerm(first2, 5, 9);
first2 = p2.addTerm(first2, 3, 10);
first2 = p2.addTerm(first2, 7, 3);
cout << "First Polynomial : " << endl;
p1.printPoly(first1);
cout << "Second Polynomial : " << endl;
p2.printPoly(first2);
cout << endl << endl;
term *firstR = NULL;
poly result;
firstR = result.add(first1, first2, firstR);
cout << "Sum of polynomials : " << endl;
result.printPoly(firstR);
}