struct poly { poly(){} poly(int n); // Initializes an array of n 0's ~poly(); int GetDegree() { return degree; } double GetC(int r) { int y = degree; if ( r > y ) { return 0;} else { return c[r]; } } void SetC(int o, double t) { c[o] = t; } private: int *degree; double *c; }; poly::poly(int n) { degree = n; c = new double[n+1]; for (int v = 0; v < n+1; v++) c[v] = 0; } poly::~poly() { delete degree; delete [] c; } void Showpoly(poly f) { int i, k, n; double coeff; n = f.GetDegree(); for ( i = 0; i < n+1; i++) { k = n - i; coeff = f.GetC(k); if ( i == 0) { if ( coeff != 1) { cout << coeff; } cout << "x^" << k; } else { if ( coeff != 0 ) { if ( coeff > 0) { cout << " + "; } else { cout << " - "; } if ( k >= 2 ) { if ( coeff != 1) { cout << abs (coeff); } cout << "x^" << k; } else if ( k == 1) { if ( coeff != 1) { cout << abs (coeff); } cout << "x"; } else { cout << abs (coeff); } } if ( k == 0) { cout << "\n"; } } } } poly Multpoly(poly f, poly g) { int n = f.GetDegree(); int m = g.GetDegree(); int k = n + m; int l; poly h(k); double sum; cout << f.GetC(0) << " is f(0) \n"; cout << g.GetC(0) << " is g(0) (Huh?)\n"; // What is going on here cout << "The degree of f is " << n << " and the degree of g is " << m << "\n"; for (l = 0; l < n+1; l++){cout << f.GetC(l) << " is f sub "<< l << "\n";} // some kind of access problem? for (l = 0; l < m+1; l++){cout << g.GetC(l) << " is g sub "<< l << "\n";} for (int i = 0; i < k+1; i++) { // cout << "I have made it to i = " << i << "\n"; sum = 0; // cout << "and j = "; for (int j = 0; j < i+1; j++) { // cout << j << ","; l = i-j; // cout << "Multiplying f["<<j<<"] by g["<<l<<"] = " << f.GetC(j) << " * " << g.GetC(l) << " = " << (f.GetC(j) * g.GetC(l)) << "\n"; sum += (f.GetC(j) * g.GetC(l)); } h.SetC(i,sum); // cout << "\n I'm writing sum of " << sum << " to h.c[" << i << "] \n"; } return (h); } void sup() { poly h(2); poly p(3); h.SetC(0,1); h.SetC(1,1); h.SetC(2,1); p.SetC(0,5); p.SetC(1,-3); p.SetC(2,0); p.SetC(3,1); Showpoly(h); Showpoly(p); poly s(5); s=Multpoly(h,p); // want: x^5 + x^4 - 2x^3 + 2x^2 + 2x + 5 Showpoly(s); } |
>> sup() x^2 + x + 1 x^3 - 3x + 5 1 is f(0) 2.85918e-316 is g(0) (Huh?) The degree of f and the degree of g is 3 1 is f sub 0 1 is f sub 1 1 is f sub 2 3.17096e+180 is g sub 0 1.27665e-152 is g sub 1 2.31635e-152 is g sub 2 0 is g sub 3 0x^5 + 0x^2 + 0x + 0 |
x^5 |
h.SetC(0,1);
if you multiply anything by 0, you are going to get a zero returned.
|
|
*degree = n;