-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpoly.h
executable file
·267 lines (234 loc) · 11.8 KB
/
poly.h
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
//-----------------------------------------------------------------------//
// POLY.H //
// Author: Luke Selbeck //
// Date: April 9th, 2014 //
// Class: CSS 343 //
// //
// Poly holds a polynomial with non-negative exponents //
//-----------------------------------------------------------------------//
// Polynomial: defined as a sum of coefficient and exponent pairs, //
// called terms. //
// //
// Term example: Coefficient Exponent //
// \ / //
// +3 * x^5 //
// //
// Polynomial example: +3*x^5 +7*x^3 -2*x^2 +9*x +1 //
// //
// Implementation and assumptions: //
// -- terms are stored in an array //
// > array position denotates exponent value //
// > array values denotate coefficient values //
// -- does not accept non-int coefficient/exponent values //
// -- does not accept non-positive exponent values //
//-----------------------------------------------------------------------//
#ifndef POLY_H
#define POLY_H
#include <iostream>
using namespace std;
class Poly {
//----------------------------- << --------------------------------------
// Overloaded output operator for class Poly
// Preconditions: coeffPtr must point to an array
// Postconditions: prints exponent in this fashion, with no trailing endl
// +3*x^5 +7*x^3 -2*x^2 +9*x +1
// note: includes positive/negative sign for first coefficient
friend ostream& operator<<(ostream&, const Poly&);
//----------------------------- >> --------------------------------------
// Overloaded input operator for class Poly; overwrites terms into Poly
// *WARNING** must be inputted in the specified format:
// [newCoeff] [newExp] [newCoeff] [newExp] ...etc... -1 -1
// -- input "-1 -1" to end the input stream
// Preconditions:
// -- coeffPtr must point to an array
// Postconditions: takes two ints as coefficient and exponent, does no
// error checking, inserts/overwrites term into Poly
friend istream& operator>>(istream&, Poly&);
public:
//-------------------------- Constructor ----------------------------------
// Default constructor for class Poly
// Preconditions: none
// Postconditions:
// -- an array of size 1 is created
// -- one term is inserted into the array
// -- coefficient=0, and exponent=0
// -- i.e. 0*x^0
Poly();
//-------------------------- Constructor ----------------------------------
// Constructor accepting one int as the coefficient of the 0th term
// Preconditions: none
// Postconditions:
// -- an array of size 1 is created
// -- one term is inserted into the array
// -- coefficient=[int], and exponent=0
// -- i.e. [int]*x^0
Poly(int);
//-------------------------- Constructor ----------------------------------
// Constructor accepting two ints as coefficient and exponent, respectively
// of the term to be inserted
// Preconditions: none
// Postconditions:
// -- an array of size 1 is created
// -- one term is inserted into the array
// -- coefficient=[int], and exponent=[int]
// -- i.e. [coefficient]*x^[exponent]
Poly(int, int);
//------------------------- Copy Constructor ------------------------------
// Constructor accepting a Poly to be deep copied
// Preconditions: none
// Postconditions:
// -- a Poly an exact copy of the parameter is made
Poly(const Poly&);
//--------------------------- Destructor ----------------------------------
// Destructor for class Poly
// Preconditions: coeffPtr points to memory on the heap
// Postconditions:
// -- array for coeffPtr is deallocated
// -- highestExp = 0
~Poly();
//---------------------------- getCoeff -----------------------------------
// Get the coefficient of an int exponent
// Preconditions: coeffPtr points to an array
// Postconditions: Returns the coefficient value for the exponent
int getCoeff(int) const;
//---------------------------- setCoeff -----------------------------------
// Overwrite a term in Poly
// Preconditions: coeffPtr points to an array
// Postconditions:
// -- params are coefficient and exponent respectively
// -- if the exponent already exists within the Poly, that term is
// replaced with the new coefficient and exponent pair
// -- if the exponent is not already in the Poly, a new term is added
// to the Poly, consisting of the new coefficient and new exponent
void setCoeff(int, int);
//---------------------------- addCoeff -----------------------------------
// Add a term to Poly
// Preconditions: coeffPtr points to an array
// Postconditions:
// -- params are coefficient and exponent respectively
// -- if the exponent already exists within the Poly, that term is
// added with the new coefficient and exponent pair
// -- if the exponent is not already in the Poly, a new term is added
// to the Poly, consisting of the new coefficient and new exponent
void addCoeff(int, int);
//---------------------------- subCoeff -----------------------------------
// Subtract a term from Poly
// Preconditions: coeffPtr points to an array
// Postconditions:
// -- params are coefficient and exponent respectively
// -- if the exponent already exists within the Poly, the new
// coefficient and exponent pair is subtracted from the existing
// term
// -- if the exponent is not already in the Poly, a new term is added
// to the Poly, consisting of the new coefficient's inverse
// and new exponent
void subCoeff(int, int);
//------------------------------ + --------------------------------------
// Overloaded addition operator; add 2 Polys
// Preconditions: coeffPtr and rhs.coeffPtr point to arrays with size at
// least 1
// Postconditions: a Poly is returned, which is the sum of this object and
// rhs
const Poly operator+(const Poly&) const;
//------------------------------ + --------------------------------------
// Overloaded addition operator; add a Poly and an int
// Preconditions: coeffPtr point to array with size at least 1
// Postconditions: a Poly is returned, which is the sum of this object and
// the int
const Poly operator+(int) const;
//------------------------------ - --------------------------------------
// Overloaded subtraction operator; subtract 2 Polys
// Preconditions: coeffPtr and rhs.coeffPtr point to arrays with size at
// least 1
// Postconditions: a Poly is returned, which is the negation of this
// object and rhs
const Poly operator-(const Poly&) const;
//------------------------------ - --------------------------------------
// Overloaded subtraction operator; subtract an int from a Poly
// Preconditions: coeffPtr points to array with size at least 1
// Postconditions: a Poly is returned, which is the negation of this
// object and the int
const Poly operator-(int) const;
//------------------------------ * --------------------------------------
// Overloaded multiplication operator; multiply 2 Polys
// Preconditions: coeffPtr and rhs.coeffPtr point to arrays with size at
// least 1
// Postconditions: a Poly is returned, which is the product of this object
// and rhs
const Poly operator*(const Poly&) const;
//------------------------------ * --------------------------------------
// Overloaded multiplication operator; multiply a Poly and an int
// Preconditions: coeffPtr points to array with size at least 1
// Postconditions: a Poly is returned, which is the product of this object
// and the int
const Poly operator*(int) const;
//------------------------------ = --------------------------------------
// Overloaded assignment operator; current object = parameter
// Preconditions: rhs.coeffPtr points to an array of at least size 1
// Postconditions:
// -- this object's array is deleted, and a new one with size equal
// to rhs's array is created
// -- all the terms from rhs's array are deep copied to this object's
// array
Poly& operator=(const Poly&);
//----------------------------- += --------------------------------------
// current object += parameter
// Preconditions: rhs.coeffPtr points to an array of at least size 1
// Postconditions:
// -- all the terms from rhs's array are passed into this object's
// array using addCoeff()
Poly& operator+=(const Poly&);
//----------------------------- -= --------------------------------------
// current object -= parameter
// Preconditions: rhs.coeffPtr points to an array of at least size 1
// Postconditions:
// -- all the terms from rhs's array are passed into this object's
// array using subCoeff()
Poly& operator-=(const Poly&);
//----------------------------- *= --------------------------------------
// current object *= parameter
// Preconditions: rhs.coeffPtr points to an array of at least size 1
// Postconditions: *this is assigned to be the product of this and rhs
Poly& operator*=(const Poly&);
//----------------------------- == --------------------------------------
// Determine if two Polys are equal
// Preconditions: coeffPtr and rhs.coeffPtr point to arrays with size at
// least 1
// Postconditions:
// -- true is returned if all terms in this object and rhs are equal
// -- false is returned if otherwise
bool operator==(const Poly&) const;
//----------------------------- != --------------------------------------
// Determine if two Polys are not equal
// Preconditions: coeffPtr and rhs.coeffPtr point to arrays with size at
// least 1
// Postconditions:
// -- false is returned if all terms in this object and rhs are equal
// -- true is returned if otherwise
bool operator!=(const Poly&) const;
//--------------------------- expIsValid ----------------------------------
// Determines if the int passed is a valid exponent value in Poly
// Preconditions: none
// Postconditions:
// -- returns true if exponent >= 0
// -- returns false if otherwise, and throws a cerr message
bool expIsValid(int) const;
private:
//--------------------------- resizeArray ---------------------------------
// Copies coeffPtr's array into a new array of newSize
// **NOTE** -- newSize should be greater than highestExp+1, otherwise, loss
// of data
// Preconditions:
// -- coeffPtr points to an array
// Postconditions:
// -- coeffPtr points to a new array of size newSize, with all the
// old array elements copied over
// -- the old array is deallocated
void resizeArray(int);
//pointer to an array storing the coefficients of the Poly
int *coeffPtr;
//array size == highestExp + 1, because the array position is the
//exponent value
int highestExp;
};
#endif