-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpoly.cpp
More file actions
206 lines (185 loc) · 5.03 KB
/
poly.cpp
File metadata and controls
206 lines (185 loc) · 5.03 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
//poly.cpp
//implementation of poly.h
#include "poly.h"
#include "linkedList.h"
#include "linkedListIterator.h"
#include "term.h"
#include <iostream>
#include <string>
#include <vector>
poly::poly()
{
polynomial.initializeList();
numOfTerms=0;
}
poly::poly(int c[], int e[], int size)
{
for (int i=0; i<size; i++)
insertTerm(c[i], e[i]);
//polynomial.selectionSortList(); //orders the link in descending order based on exponent
}
poly::poly(const poly& p)
{
polynomial=p.polynomial;
numOfTerms=p.numOfTerms;
}
poly::~poly()
{
polynomial.initializeList();
}
void poly::insertTerm(int c, int e)
{
term temp(c, e);
polynomial.addLast(temp);
numOfTerms++;
}
void poly::insertTerm(const term& t)
{
polynomial.addLast(t);
numOfTerms++;
}
void poly::printPoly() const
{
linkedListIterator<term> printer;
printer = this->polynomial.begin();
while (printer != NULL)
{
std::cout<< (*printer);
++printer;
if (printer!= NULL && (*printer).coeff>=0)
std::cout<<"+";
if (printer!= NULL && (*printer).coeff<0)
std::cout << "";
}
}
int poly::getNumOfTerms() const
{
return numOfTerms;
}
void poly::sortPoly()
{
polynomial.bubbleSortList();
}
poly poly::operator+ (const poly& p)
{
poly result;
linkedListIterator<term> left;
left =(polynomial.begin()); //start iterator for left operand at begining of the list
linkedListIterator<term> right;
right =(p.polynomial.begin()); //start iterator for right operand at begining of the list
while ((left!=NULL)&&(right!=NULL))
{
//see if exponents are equal (addition of like terms)
if ((*left).exp == (*right).exp)
{
result.insertTerm (((*left).coeff+(*right).coeff),(*left).exp);
++right;
++left;
}
//if the left term is of a higher magnitude than the right term
//add left term to the result list and advance to next left term
else if ((*left).exp > (*right).exp)
{
result.insertTerm((*left).coeff,(*left).exp);
++left;
}
//if the left term is of a lower magnitude than the right term
//add right term to the result list and advance to the next right term
else if ((*left).exp < (*right).exp)
{
result.insertTerm((*right).coeff, (*right).exp);
++right;
}
//if the end of the left polynomial is reached first
//add all remaining terms from the right polynomial to result
if ((left==NULL)&&(right!=NULL))
{
result.insertTerm((*right).coeff, (*right).exp);
++right;
}
//if the end of the right polynomial is reached first
//add all remaining terms from the left polynomial to result
if ((left!=NULL)&&(right==NULL))
{
result.insertTerm((*left).coeff, (*left).exp);
++left;
}
}
return result;
}
poly poly::operator- (const poly& p)
{
poly result;
linkedListIterator<term> left;
left =(this->polynomial.begin()); //start iterator for left operand at begining of the list
linkedListIterator<term> right;
right =(p.polynomial.begin()); //start iterator for right operand at begining of the list
while ((left!=NULL)&&(right!=NULL))
{
//see if exponents are equal (subtraction of like terms)
if ((*left).exp == (*right).exp)
{
result.insertTerm (((*left).coeff-(*right).coeff),(*left).exp);
++right;
++left;
}
//if the left term is of a higher magnitude than the right term
//add left term to the result list and advance to next left term
else if ((*left).exp > (*right).exp)
{
result.insertTerm((*left).coeff,(*left).exp);
++left;
}
//if the left term is of a lower magnitude than the right term
//subtract right term to the result list and advance to the next right term
else if ((*left).exp < (*right).exp)
{
result.insertTerm(-(*right).coeff, (*right).exp);
++right;
}
//if the end of the left polynomial is reached first
//subtract all remaining terms from the right polynomial to result
if ((left==NULL)&&(right!=NULL))
{
result.insertTerm(-(*right).coeff, (*right).exp);
++right;
}
//if the end of the right polynomial is reached first
//add all remaining terms from the left polynomial to result
if ((left!=NULL)&&(right==NULL))
{
result.insertTerm((*left).coeff, (*left).exp);
++left;
}
}
return result;
}
poly poly::operator* (const poly& p) //multiplies polynomials using the vertical method
{
poly result;
linkedListIterator<term> left;
left =(this->polynomial.begin()); //start iterator for left operand at begining of the list
linkedListIterator<term> right;
right =(p.polynomial.begin()); //start iterator for right operand at begining of the list
//create a vector<poly> to hold the steps of the multiplication process
std::vector<poly> temp;
//multiply each right term by the whole left polynomial
while (right != NULL)
{
poly section;
while (left!=NULL)
{
section.insertTerm((*left)*(*right));
++left;
}
temp.push_back(section);
left =(this->polynomial.begin()); //start iterator for left operand at begining of the list
++right;
}
//add together all temporary polynomials created in the last step
int size=temp.size();
for (int j=0; j<(size-1); j++)
temp[j+1]=(temp[j]+temp[j+1]);
result = temp[size-1];
return result;
}