-
Notifications
You must be signed in to change notification settings - Fork 0
/
polynomial-friends.cpp
129 lines (115 loc) · 3.27 KB
/
polynomial-friends.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
#include "polynomial.h"
polynomial firstDerivative(const polynomial &other)
{
polynomial temp;
term newTerm;
for(unsigned int i = 0; i < other.poly.size(); ++i)
{
newTerm.setTerm(
other[i].getCoeff()*other[i].getPower() ,
other[i].getPower()-1
);
temp.poly.push_back(newTerm);
}
temp.combineTerms();
return temp;
}
polynomial nthDerivative(const polynomial &other, const int n) {
polynomial temp(other);
for(int i = 0; i < n; ++i) {
temp = firstDerivative(temp);
}
return temp;
}
polynomial operator+(const polynomial &x, const polynomial &y)
{
polynomial temp;
for(unsigned int i = 0; i < x.poly.size(); ++i)
temp.poly.push_back(x[i]);
for(unsigned int i = 0; i < y.poly.size(); ++i)
temp.poly.push_back(y[i]);
temp.sort();
// std::cout << "term combining .. 1" << std::endl;
temp.combineTerms();
return temp;
}
polynomial operator-(const polynomial &x, const polynomial &y)
{
polynomial _y(y);
for(unsigned int i = 0; i < _y.poly.size(); ++i) {
_y.poly[i].setTerm(fraction(0)-_y.poly[i].getCoeff(),
_y.poly[i].getPower(),
_y.poly[i].getVar());
}
polynomial temp = x + _y;
return temp;
}
polynomial operator*(const polynomial &x, const polynomial &y)
{
polynomial temp;
for(unsigned int i = 0; i < x.poly.size(); ++i)
for(unsigned int j = 0; j < y.poly.size(); ++j)
temp.poly.push_back(x[i] * y[j]);
temp.sort();
temp.combineTerms();
return temp;
}
polynomial operator/(const polynomial &x, const polynomial &y)
{
//do this with synthetic division.... hint...
//Also, does polynomial need to be changed to support division??
return x;
}
std::ostream& operator<<(std::ostream& out, const polynomial &other) //TESTED
{
for(unsigned int i = 0; i < other.poly.size(); ++i){
if(i!=0){
if(other[i].getCoeff()<0) {
out<<" - "<<term(fraction(0) - other[i].getCoeff(),
other[i].getPower(),
other[i].getVar());
}
else
out<<" + "<<other[i];
}
if(i==0)
out<<other[i];
}
return out;
}
std::istream& operator>>(std::istream& in, polynomial &p)
{
//still have problems with line endings
//plan to take the line as a string
//and parse the string as a couple of
//terms...
p.poly.clear();
term _term;
std::string line;
std::getline(in,line);
// p = polynomial(line)
size_t pos=0, prev = 1;
while((pos = line.find(' ')) != std::string::npos) {
line.erase(pos,1);
}
line+="+";
while((pos = line.find_first_of("+-",prev)) != std::string::npos) {
if(line[pos-1]=='^')
prev++;
else {
prev = 1;
std::string sterm = line.substr(0, pos);
// std::cout<<sterm<<std::endl;
std::stringstream ssterm;
ssterm<<sterm;
ssterm>>_term;
p.poly.push_back(_term);
line.erase(0, pos);
if(line.length()==1)
line.erase(0,1);
}
}
p.sort();
p.combineTerms();
return in;
}