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 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
|
#include <cmath>
#include <iostream>
using namespace std;
class polynomial
{
public:
friend ostream& operator<<(ostream& outfile, const polynomial&);
friend polynomial operator+(int, const polynomial&);
friend polynomial operator*(int, const polynomial&);
friend polynomial operator-(int, const polynomial&);
polynomial();
polynomial(const polynomial&);
polynomial(int*, int);
~polynomial();
polynomial operator*(const polynomial&);
polynomial operator*(int);
polynomial operator+(const polynomial&);
polynomial operator+(int);
void operator=(const polynomial&);
void operator=(int);
polynomial operator-();
polynomial operator-(const polynomial&);
polynomial operator-(int);
private:
int *expr;
int degree;
};
int main()
{
int poly1[] = {-2,4,6};
int poly2[] = {1,3,-5};
int poly3[] = {3, 0, 4, 0, 5};
polynomial p1(poly1, 2);
polynomial p2(poly2, 2);
polynomial p3(poly3, 4);
polynomial result;
cout << "p1 = " << p1 << endl;
cout << "p2 = " << p2 << endl;
cout << "p3 = " << p3 << endl << endl;
cout << endl;
result = p1 * p2;
cout << "p1 * p2 = " << result << endl;
result = p2 + p1;
cout << "p2 + p1 = " << result << endl;
result = p1 + p3;
cout << "p1 + p3 = " << result << endl;
result = p1 * p3;
cout << "p1 * p3 = " << result << endl;
result = p1 + 5;
cout << "p1 + 5 = " << result << endl;
result = p2 * 6;
cout << "p2 * 6 = " << result << endl;
result = 4 + p3;
cout << "4 + p3 = " << result << endl;
result = 3 * p1;
cout << "3 * p1 = " << result << endl;
result = 5 - p2;
cout << "5 - p2 = " << result << endl;
result = 10;
cout << "result = 10: " << result << endl << endl;
return 0;
}
ostream& operator<<(ostream& outfile, const polynomial& p) //display the polynomial
{
for (int i = p.degree; i >= 0; i--)
{
if (i == p.degree && p.expr[i] < 0)
outfile << "-";
else if (i != p.degree)
{
if (p.expr[i] > 0)
outfile << " + ";
if (p.expr[i] < 0)
outfile << " - ";
}
if (p.expr[i] != 0)
{
outfile << abs(p.expr[i]);
if (i > 0)
outfile << "x";
if (i > 1)
outfile << "^" << i;
}
}
return outfile;
}
polynomial operator+(int n, const polynomial& p) //add an int to the copy
{
polynomial temp;
temp.expr = new int [p.degree];
temp.degree = p.degree;
*temp.expr = p.expr[0] + n;
return temp;
}
polynomial operator*(int n, const polynomial& p) //multiply the copy by the int
{
polynomial temp;
temp.expr = new int [p.degree];
temp.degree = p.degree;
for (int i=0; i< p.degree; i++)
{
temp.expr[i] = n * p.expr[i];
}
return temp;
}
polynomial operator-(int n, const polynomial& p) //subtract int from copy if int is first
{
polynomial temp;
temp.expr = new int [p.degree];
temp.degree = p.degree;
*temp.expr = p.expr[0] - n;
return temp;
}
polynomial::polynomial() //cnstrctr, set priv int degree to 0, set priv expr pointer to NULL
{
degree = 0;
expr = NULL;
}
polynomial::polynomial(const polynomial& p) //copy constructor
{
degree = p.degree;
expr = new int [degree];
for (int i=0; i<degree; i++)
expr[i] = p.expr[i];
}
polynomial::polynomial(int *poly_expr, int d) //cnstrctr, takes int pointer and int value.
//sets degree to the int value, and the int
//pointer will be a dynamic array so a deep
//copy of that to expr will be done
{
degree = d;
expr = new int [degree];
for (int i=0; i < degree+1; i++)
{
expr[i] = poly_expr[i];
}
}
polynomial::~polynomial() //deallocate the array that expr is pointing to
{
delete [] expr;
}
polynomial polynomial::operator*(const polynomial& p) //multiply poly by poly
{
polynomial temp;
if (p.degree > degree)
{
temp.expr = new int [p.degree];
temp.degree = p.degree;
}
else
{
temp.expr = new int [degree];
temp.degree = degree;
}
for (int i=0; i < degree; i++)
{
for (int j=0; j < temp.degree; j++)
temp.expr[i+j] += expr[i] * p.expr[j];
}
return temp;
}
polynomial polynomial::operator*(int n) //multiply poly by int
{
polynomial temp;
temp.expr = new int [degree];
temp.degree = degree;
for (int i=0; i < degree; i++)
{
temp.expr[i] = expr[i] * n;
}
return temp;
}
polynomial polynomial::operator+(const polynomial& p) //add invoking object's poly to poly
{
polynomial temp;
if (p.degree > degree)
{
temp.expr = new int [p.degree];
temp.degree = p.degree;
for (int i=0; i < p.degree; i++)
{
temp.expr[i] = expr[i] + p.expr[i];
}
}
else
{
temp.expr = new int [degree];
temp.degree = degree;
for (int i=0; i < degree; i++)
{
temp.expr[i] = expr[i] + p.expr[i];
}
}
return temp;
}
polynomial polynomial::operator+(int n) //add invoking object's poly to int
{
polynomial temp;
temp.expr = new int [degree];
temp.degree = degree;
temp.expr[0] = n + expr[0];
return temp;
}
void polynomial::operator=(const polynomial& p) //assignment operator to perform a copy
{
if (this != &p)
{
delete [] expr;
degree = p.degree;
expr = new int [degree];
for (int i=0; i < degree; i++)
expr[i] = p.expr[i];
}
}
void polynomial::operator=(int n) //polynomial of degree 0 is created with 1 coefficient
{
*expr = n;
}
polynomial polynomial::operator-() //make the polynomial a negative
{
polynomial temp;
for (int i= 0; i < degree+1; i++)
{
temp.expr[i] = expr[i] * -1;
}
return temp;
}
polynomial polynomial::operator-(const polynomial& p)//subtract invoking object's polynomial
//from another polynomial
{
polynomial temp;
if (p.degree > degree)
{
temp.expr = new int [p.degree];
temp.degree = p.degree;
for (int i=0; i < p.degree; i++)
{
temp.expr[i] = expr[i] - p.expr[i];
}
}
else
{
temp.expr = new int [degree];
temp.degree = degree;
for (int i=0; i < degree; i++)
{
temp.expr[i] = expr[i] - p.expr[i];
}
}
return temp;
}
polynomial polynomial::operator-(int n) //subtract an invoking object's polynomial from int
{
polynomial temp;
temp.expr = new int [degree];
temp.degree = degree;
*temp.expr = n - expr[0];
return temp;
}
|