-
Notifications
You must be signed in to change notification settings - Fork 0
/
polynomial.cpp
176 lines (150 loc) · 3.71 KB
/
polynomial.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
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
#include "polynomial.h"
//PUBLIC
//CTORS
polynomial::polynomial()
{
}
polynomial::~polynomial()
{
poly.clear();
}
polynomial::polynomial(const polynomial &other)
{
copy(other);
}
polynomial::polynomial(const term &other)
{
poly.push_back(other);
}
polynomial::polynomial(std::string expString) { //TESTED
setExp(expString);
}
//PUBLIC FUNCTIONS
void polynomial::setExp(std::string expString) { //TESTED
std::stringstream ss;
ss<<expString;
ss>>*this;
}
fraction polynomial::evaluate(const fraction &other) { //TESTED
fraction temp(0);
for(std::vector<term>::iterator it = poly.begin(); it != poly.end(); ++it) {
temp += it->evaluate(other);
}
return temp;
}
double polynomial::evaluate(const double value) {
double temp = 0;
for(std::vector<term>::iterator it = poly.begin(); it != poly.end(); ++it) {
temp += it->evaluate(value);
}
return temp;
}
void polynomial::addterm(const term &term_arg) { //TESTED
poly.push_back(term_arg);
}
std::string polynomial::polyString() {
std::stringstream ss;
std::string temp;
ss<<*this;
std::getline(ss, temp);
return temp;
}
//OPERATORS
polynomial& polynomial::operator=(const polynomial &other)
{
if(this != &other)
{
poly.clear();
copy(other);
}
return *this;
}
polynomial& polynomial::operator+=(const polynomial &other)
{
polynomial ans = *this + other;
*this = ans;
return *this;
}
polynomial& polynomial::operator-=(const polynomial &other)
{
polynomial ans = *this - other;
*this = ans;
return *this;
}
polynomial& polynomial::operator*=(const polynomial &other)
{
polynomial ans = *this * other;
*this = ans;
return *this;
}
polynomial& polynomial::operator/=(const polynomial &other)
{
polynomial ans = *this / other;
*this = ans;
return *this;
}
fraction polynomial::operator()(const fraction &other) //TESTED
{
// fraction result;
// for(unsigned int i = 0; i < poly.size(); ++i)
// result += poly[i](other);
return this->evaluate(other);
}
term polynomial::operator[](unsigned int index) const
{
return poly[index];
}
/*
* Suppose that there is a polynomial named f.
* then the operator below will allow:
* f[3] = term(3,2);
* Meaning, we can index the polynomial like and array and
* assign new values to each term.
*/
term& polynomial::operator[](unsigned int index)
{
return poly[index];
}
void polynomial::copy(const polynomial &other)
{
for(unsigned int i = 0; i < other.poly.size(); ++i)
poly.push_back(other[i]);
}
void polynomial::sort()
{
term temp;
for(unsigned int i = 0; i < poly.size(); ++i)
for(unsigned int j = 0; j < poly.size(); ++j)
if(poly[i] > poly[j])
{
temp = poly[i];
poly[i] = poly[j];
poly[j] = temp;
}
}
void polynomial::combineTerms()
{
for(unsigned int i = 0; i < poly.size()-1; ++i)
if(poly[i].getPower() == poly[i+1].getPower())
{
// std::cout << "term combining .. 2" << std::endl;
poly[i].setTerm(poly[i].getCoeff() + poly[i + 1].getCoeff(),
poly[i].getPower());
poly.erase(poly.begin() + i + 1);
}
for(unsigned int i = 0; i < poly.size(); ++i)
if(poly[i].getCoeff() == fraction(0)) {
poly.erase(poly.begin()+i);
}
if(poly.size() == 0)
poly.push_back(term(0));
}
/*
* *(x + 2) is the same as x[2] in an array!!!
*
* for(vector<term>::iterator i = poly.begin(); i != poly.end(); ++i
* cout<<*i<<endl;
*
* for(vector<term>::iterator i = poly.rbegin(); i != poly.rend(); --i
* cout<<*i<<endl;
*/