Skip to content

Commit e702f36

Browse files
Merge pull request #617 from Ansh-Kushwaha/patch-1
Create PolynomialAddition.cpp
2 parents 1d2ad71 + 3f1ba04 commit e702f36

File tree

1 file changed

+136
-0
lines changed

1 file changed

+136
-0
lines changed

PolynomialAddition.cpp

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
// Github username: Ansh-Kushwaha
2+
// Aim: To store and add polynomials
3+
// Date: 05/10/2022
4+
5+
6+
#include <iostream>
7+
using namespace std;
8+
9+
struct term{
10+
int pow, coeff;
11+
term *next;
12+
};
13+
14+
typedef struct polynomial{
15+
term* addTerm(term *first, int p, int c){ //adding term to the polynomial
16+
if(p<0){
17+
cout << "Undefined power" << endl;
18+
return first;
19+
}
20+
if(first == NULL){
21+
term *tmp = new term;
22+
tmp->pow = p;
23+
tmp->coeff = c;
24+
tmp->next = first;
25+
first = tmp;
26+
return first;
27+
}
28+
else{
29+
int pos = 1;
30+
term *q = first;
31+
term *nxt = NULL;
32+
while(q->next!=NULL){
33+
nxt = q->next;
34+
if(p > q->pow)
35+
break;
36+
else if(p < q->pow && p > nxt->pow){
37+
pos++;
38+
break;
39+
}
40+
else{
41+
q = q->next;
42+
pos++;
43+
}
44+
45+
}
46+
if(p < q->pow && nxt == NULL){
47+
pos++;
48+
}
49+
if(pos==1){
50+
term* tmp = new term;
51+
tmp->pow = p;
52+
tmp->coeff = c;
53+
tmp->next = q;
54+
q = tmp;
55+
return q;
56+
}
57+
else{
58+
term *tmp = new term;
59+
tmp->pow = p;
60+
tmp->coeff = c;
61+
tmp->next = q->next;
62+
q->next = tmp;
63+
return first;
64+
}
65+
}
66+
return first;
67+
}
68+
69+
term* add(term* one, term* two, term* res){ //combining two polynomials
70+
while(one!=NULL && two!=NULL){
71+
if(one->pow == two->pow){
72+
res = addTerm(res, one->pow, (one->coeff+two->coeff));
73+
one = one->next;
74+
two = two->next;
75+
}
76+
else if(one->pow < two->pow){
77+
res = addTerm(res, two->pow, two->coeff);
78+
two = two->next;
79+
}
80+
else if(one->pow > two->pow){
81+
res = addTerm(res, one->pow, one->coeff);
82+
one = one->next;
83+
}
84+
}
85+
while(one!=NULL || two!=NULL){
86+
if(one!= NULL){
87+
res = addTerm(res, one->pow, one->coeff);
88+
one = one->next;
89+
}
90+
if(two!= NULL){
91+
res = addTerm(res, two->pow, two->coeff);
92+
two = two->next;
93+
}
94+
}
95+
return res;
96+
}
97+
98+
void printPoly(term *first){ //function to print the stored polynomial
99+
char var = 'x';
100+
while(first!=NULL){
101+
cout <<first->coeff << var << "^" << first->pow << " ";
102+
first = first->next;
103+
if(!(first == NULL))
104+
cout << "+ ";
105+
}
106+
cout << endl;
107+
}
108+
}poly; //Data Structure (Linked List) based polynomial addition
109+
110+
int main(){
111+
term *first1 = NULL;
112+
poly p1;
113+
first1 = p1.addTerm(first1, 0, 1);
114+
first1 = p1.addTerm(first1, 5, 2);
115+
first1 = p1.addTerm(first1, 3, 4);
116+
first1 = p1.addTerm(first1, 4, 9);
117+
118+
term *first2 = NULL;
119+
poly p2;
120+
first2 = p2.addTerm(first2, 0, 2);
121+
first2 = p2.addTerm(first2, 5, 9);
122+
first2 = p2.addTerm(first2, 3, 10);
123+
first2 = p2.addTerm(first2, 7, 3);
124+
125+
cout << "First Polynomial : " << endl;
126+
p1.printPoly(first1);
127+
cout << "Second Polynomial : " << endl;
128+
p2.printPoly(first2);
129+
cout << endl << endl;
130+
131+
term *firstR = NULL;
132+
poly result;
133+
firstR = result.add(first1, first2, firstR);
134+
cout << "Sum of polynomials : " << endl;
135+
result.printPoly(firstR);
136+
}

0 commit comments

Comments
 (0)