Polynomial Class Implementation Issue

Hello. I'm currently having issues with one of my programs and I can't figure out why it doesn't work. I have tried different variations of my code, and other ways of doing it and it still won't work.

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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#ifndef __POLYNOMIAL_CPP__
#define __POLYNOMIAL_CPP__

#include "polynomial.h"
#include <iostream>
#include <list>

using namespace std;

//copy assignment
template<typename NumberType>
/*Done*/ const Polynomial<NumberType> & Polynomial<NumberType>::operator=(const Polynomial<NumberType> & rhs) {
	if (this != &rhs) {
		term_list = rhs.term_list;
		highest_degree = rhs.highest_degree;
		number_of_terms = rhs.number_of_terms;
	}
	return *this;
}

 //binary math function things start//
template<typename NumberType>
/*Done*/ Polynomial<NumberType> Polynomial<NumberType>::operator+=(const Monomial<NumberType> &m) {
	insert_in_poly(*this, m);
	return *this;
}
template<typename NumberType>
/*Done*/ Polynomial<NumberType> Polynomial<NumberType>::operator+=(const Polynomial<NumberType> & rhs) {
	Polynomial<NumberType> sumTemp;
	list<Monomial<NumberType>>::const_iterator listItr = term_list.begin();

	while (!(*listItr).end()) {
		sumTemp += rhs.*listItr;
	}
	*this = sumTemp;
	return *this;
}
template<typename NumberType>
/*Done*/ const Polynomial<NumberType> Polynomial<NumberType>::operator+ (const Monomial<NumberType> &m)const {
	return Polynomial(*this) += m;
}
template<typename NumberType>
/*Done*/ const Polynomial<NumberType> Polynomial<NumberType>::operator+ (const Polynomial<NumberType> & rhs) const {
	return Polynomial(*this) += rhs;
}
template<typename NumberType>
Polynomial<NumberType> Polynomial<NumberType>::operator-=(const Monomial<NumberType> &m) {
	insert_in_poly(*this, m);
}
template<typename NumberType>
Polynomial<NumberType> Polynomial<NumberType>::operator-=(const Polynomial<NumberType> & rhs) {

}
template<typename NumberType>
/*Done*/const Polynomial<NumberType> Polynomial<NumberType>::operator-(const Monomial<NumberType> &m)const {
	return Polynomial(*this) -= m;
}
template<typename NumberType>
/*Done*/const Polynomial<NumberType> Polynomial<NumberType>::operator- (const Polynomial<NumberType> & rhs) const {
	return Polynomial(*this) -= rhs;
}
template<typename NumberType>
Polynomial<NumberType> Polynomial<NumberType>::operator*=(const Monomial<NumberType> &m) {
	insert_in_poly(*this, m); 
}
template<typename NumberType>
Polynomial<NumberType> Polynomial<NumberType>::operator*=(const Polynomial<NumberType> & rhs) {

}
template<typename NumberType>
/*Done*/const Polynomial<NumberType> Polynomial<NumberType>::operator*(const Monomial<NumberType> &m) const {
	return (*this) *= m;
}
template<typename NumberType>
/*Done*/const Polynomial<NumberType> Polynomial<NumberType>::operator*(const Polynomial<NumberType> &rhs)const {
	return (*this) *= rhs;
}
//binary math function things end//

//evaluate things
template<typename NumberType>
const NumberType Polynomial<NumberType>::evaluate(NumberType x) const {

}

//bool operators
template<typename NumberType>
/*Done*/ bool Polynomial<NumberType>::operator==(const Polynomial<NumberType> &p) const {
	return (number_of_terms == p.number_of_terms && highest_degree == p.highest_degree);
}
template<typename NumberType>
/*Done*/ bool Polynomial<NumberType>::operator!=(const Polynomial<NumberType> &p) const {
	return (number_of_terms != p.number_of_terms || highest_degree != p.highest_degree);
}


//Read and Print Functions
template<typename NumberType>
void Polynomial<NumberType>::read(istream & in = cin) {
	Monomial<NumberType> newMonom;
	int coeff;
	int degree;
	while (true) {
		in >> coeff;
		in >> degree;
		if (coeff == 0) {
			break;
		}
		else if (degree < 0) {
			cout << "No negatives, no bueno.";
			break;
		}
		else {
			newMonom.assign_coefficient(coeff);
			newMonom.assign_degree(degree);
			insert_in_poly(*this, newMonom);
		}
	}
	return;
}
template<typename NumberType>
void Polynomial<NumberType>::print(ostream & out = cout) const {
	Monomial<NumberType> mono;
	list<Monomial<NumberType>>::const_iterator listItr = term_list.begin();
	
	while (listItr != term_list.end()) {
		if ((*listItr).coefficient() == 0) {
			break;
		}
		else if ((*listItr).degree() == 0 && (*listItr).coefficient() > 0) {
			out << " + " << (*listItr).coefficient();
			if (listItr == term_list.end()) {
				break;
			}
		}
		else if ((*listItr).degree() == 0 && (*listItr).coefficient() < 0) {
			out << " - " << (*listItr).coefficient();
			if (listItr == term_list.end()) {
				break;
			}
		}
		else if ((*listItr).degree() == 1 && (*listItr).coefficient() > 0) {
			out << " + " << (*listItr).coefficient() << 'x';
		}
		else if ((*listItr).degree() == 1 && (*listItr).coefficient() < 0) {
			out << " - " << (*listItr).coefficient() << 'x';
		}
		else if ((*listItr).coefficient() > 0 && (*listItr).degree() != 0){
			out << " + " << (*listItr).coefficient() << "x^" << (*listItr).degree();
		}
		else if ((*listItr).coefficient() < 0 && (*listItr).degree() != 0) {
			out << " - " << (*listItr).coefficient() << "x^" << (*listItr).degree();
		}
		else if (gethighestdegree() && (*listItr).coefficient() > 0) {
			out << (*listItr).coefficient() << "x^" << (*listItr).degree();
		}
		else if (gethighestdegree() && (*listItr).coefficient() < 0) {
			out << "- " << (*listItr).coefficient() << "x^" << (*listItr).degree();
		}
	}
	// if degree == 1 print x
	//if degree == 0 print coeff
	//else print coeff x^ degree
	return;
}


//Insert Monomial into Polynomial
template<typename NumberType>
/*Done*/ void Polynomial<NumberType>::insert_in_poly(Polynomial<NumberType> & p, const Monomial<NumberType> & m) {
	int m_degree = m.degree();
	if (p == Polynomial<NumberType>::ZERO) {
		p.term_list.pop_front(); //get rid of (0, 0) term
		p.term_list.push_front(m);
		p.highest_degree = m.degree();
		p.number_of_terms = 1;
	}
	else {
		list<Monomial<NumberType>>::iterator itr = p.term_list.begin();
		while (itr != p.term_list.end()) {
			Monomial<NumberType> current_m = *itr;
			if (current_m == m_degree) {
				if ((current_m.coefficient() + m.coefficient()) == 0) {
					itr = p.term_list.erase(itr);
					p.number_of_terms--;
					if (p.number_of_terms == 0) {
						p.highest_degree = 0;
						p.term_list.push_back(Monomial<NumberType>(0, 0));
					}
					else {
						if (p.highest_degree == m_degree) {
							p.highest_degree = (*itr).degree();
						}
					}
				}
				else {
					(*itr).assign_coefficient(current_m.coefficient() + m.coefficient());
				}
				return;
			}
		}

	}
}


//Power Function
template<typename NumberType>
/*Done*/ NumberType Polynomial<NumberType>::power(NumberType x, int n) const {
	NumberType product;
	for (int i = 0; i < n; i++) {
		product *= x;
	}
	return product;
}

//istream and ostream operators
template<typename NumberType> 
/*Done*/ istream& operator>>(istream & in, Polynomial<NumberType> & rhs) {
	rhs.read(in);
	return in;
}
template<typename NumberType>
/*Done*/ ostream& operator<<(ostream & out, const Polynomial<NumberType> & rhs) {
	rhs.print(out);
	return out;
}

#endif 


I am having a problem with my print and read functions (mostly the print function), because trying to print everything will go into an infinite loop and putting breaks only prints up to 2 monomials. Can someone explain what is wrong with them? My insert_in_poly works because my professor gave it to us in class.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
//Main Driver Code
#include "polynomial.cpp"
#include <iostream>

using namespace std;

int main() {
	Polynomial<int> a(9, 3);
	Polynomial<int> c;
	cin >> c;
	cout << a << endl;
	cout << c << endl;
	return 0;
}
Topic archived. No new replies allowed.