-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpolynomial.cc
252 lines (236 loc) · 8.05 KB
/
polynomial.cc
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
/*******************************************************************
* Name: Hayden Ivatts
* Date: 10-23-18
* Course: CSCI 132
* Assignment: Assignment 4
* Program: polynomial.cc
* Purpose: Implementation of polynomial class
***************************************************************************/
#include <iostream>
#include <cmath>
#include "polynomial.h"
using namespace std;
void Polynomial :: equals_difference(Polynomial p, Polynomial q)
/**********************************************************************
* Pre: None
* Post: The Polynomial object is reset as the difference of the two parameters.
**********************************************************************/
{
clear();
while (!p.empty( ) || !q.empty( )) {
//Temporary variables to hold info of each term
Term p_term, q_term;
if (p.degree( ) > q.degree( )) {
p.serve_and_retrieve(p_term);
p_term.coefficient = p_term.coefficient * -1;
append(p_term);
} else if (q.degree( ) > p.degree( )) {
q.serve_and_retrieve(q_term);
append(q_term);
} else {
p.serve_and_retrieve(p_term);
q.serve_and_retrieve(q_term);
if(q_term.coefficient - p_term.coefficient != 0) {
//Adds answer to new queue
Term answer_term(p_term.degree,
q_term.coefficient - p_term.coefficient);
append(answer_term);
}
}
}
}
void Polynomial :: equals_product(Polynomial p, Polynomial q)
/**********************************************************************
* Pre: None
* Post: The Polynomial object is reset as the product of the two parameters.
**********************************************************************/
{
clear();
//Temporary polynomial to hold terms while others are multiplied
Polynomial equals_temp;
while(!q.empty()) {
//Holds current term of q
Term q_term;
//holds result of mult_term function
Polynomial temp;
q.serve_and_retrieve(q_term);
temp.mult_term(p,q_term);
equals_temp.equals_sum(equals_temp, temp);
} //end while
while(!equals_temp.empty()) {
//holds term to be added to new polynomial
Term answer;
equals_temp.serve_and_retrieve(answer);
append(answer);
} //end while
} //end equals_product
void Polynomial :: mult_term(Polynomial p, Term t)
/**********************************************************************
* Pre: None
* Post: The Polynomial object is reset as the product of the polynomial,
* p, and the single term, t.
**********************************************************************/
{
clear();
while(!p.empty()) {
//Holds current term of p
Term p_term;
p.serve_and_retrieve(p_term);
//Holds term to be added to new polynomial
Term answer_term(p_term.degree + t.degree,
p_term.coefficient * t.coefficient);
append(answer_term);
}
}
double Polynomial :: evaluate (double number)
/**********************************************************************
* Pre: None
* Post: Evaluates the Polynomial object at number.
**********************************************************************/
{
Polynomial p = *this; //uses the object "receiving the message", but
//does not change the object data.
//Holds running total of evaluation
double answer = 0;
while(!p.empty()) {
//Current p term
Term p_term;
p.serve_and_retrieve(p_term);
//Holds result of number to the power of degree
double power = pow(number,p_term.degree);
//Holds result of multiplication of power and coefficient to get answer
double term_total = power * p_term.coefficient;
answer+=term_total;
}
return answer;
} //end evaluate
Error_code Polynomial :: equals_quotient(Polynomial p, Polynomial q)
/**********************************************************************
* Pre: None
* Post: The Polynomial object is reset as the quotient of the two parameters.
**********************************************************************/
{
cout << "This function not yet implemented." << endl;
return success;
} //end equals_quotient()
void Polynomial :: print( )
/**********************************************************************
* Pre: None
* Post: The Polynomial is printed to cout. If the polynomial is empty,
* 0 is printed.
**********************************************************************/
{
double r;
Polynomial temp;
bool first_term = true;
while (!empty( )) {
Term print_term;
serve_and_retrieve(print_term);
temp.append(print_term);
if (first_term) { // In this case, suppress printing an initial +.
first_term = false;
if (print_term.coefficient < 0) {
cout << "- ";
}
} else if (print_term.coefficient < 0) {
cout << " - ";
} else {
cout << " + ";
}
if (print_term.coefficient >= 0) {
r = print_term.coefficient;
} else {
r = -(print_term.coefficient);
}
if (r != 1) {
cout << r;
}
if (print_term.degree > 1) {
cout << " X^" << print_term.degree;
}
if (print_term.degree == 1) {
cout << " X";
}
if (r == 1 && print_term.degree == 0) {
cout << " 1";
}
} //end while
if (first_term) {
cout << "0"; // Print 0 for an empty Polynomial.
} //end if
cout << endl;
while(!temp.empty( )) {
Term temp_term;
temp.serve_and_retrieve(temp_term);
append(temp_term);
}
} // end print()
void Polynomial :: read( )
/**********************************************************************
* Pre: None
* Post: The Polynomial is read from cin.
**********************************************************************/
{
clear( ); /* An Extended_queue function */
double coefficient;
int last_exponent, exponent;
bool first_term = true;
cout << "Enter the coefficients and exponents for the polynomial, "
<< "one pair per line. Exponents must be in descending order." << endl
<< "Enter a coefficient of 0 or an exponent of 0 to terminate." << endl;
do {
cout << "coefficient? " << flush;
cin >> coefficient;
if (coefficient != 0.0) {
cout << "exponent? " << flush;
cin >> exponent;
if ((!first_term && exponent >= last_exponent) || exponent < 0) {
exponent = 0;
cout << "Bad exponent: Polynomial terminates without its last term."
<< endl;
} else {
Term new_term(exponent, coefficient);
append(new_term);
first_term = false;
} //end if
last_exponent = exponent;
}
} while (coefficient != 0.0 && exponent != 0); //end do-while
} // end read()
void Polynomial :: equals_sum(Polynomial p, Polynomial q)
/**********************************************************************
* Pre: None
* Post: The Polynomial object is reset as the sum of the two parameters.
**********************************************************************/
{
clear( );
while (!p.empty( ) || !q.empty( )) {
Term p_term, q_term;
if (p.degree( ) > q.degree( )) {
p.serve_and_retrieve(p_term);
append(p_term);
} else if (q.degree( ) > p.degree( )) {
q.serve_and_retrieve(q_term);
append(q_term);
} else {
p.serve_and_retrieve(p_term);
q.serve_and_retrieve(q_term);
if (p_term.coefficient + q_term.coefficient != 0) {
Term answer_term(p_term.degree, p_term.coefficient + q_term.coefficient);
append(answer_term);
} //end inner if
} //end if-else
} // end while
} //end equals_sum()
int Polynomial :: degree( ) const
/**********************************************************************
* Pre: None
* Post:If the Polynomial is identically 0, a result of -1 is returned.
* Otherwise the degree of the Polynomial is returned.
**********************************************************************/
{
if (empty( )) return -1;
Term lead;
retrieve(lead);
return lead.degree;
} //end degree( )