Polynomial Exercise

Exercise from book pdf

http://docdro.id/nrdKYMh

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
#pragma once
#include <iostream> #define MAX_DEGREE 20+1+1
class Polynomial 
{
private:

int fDegree;
double fCoeffs[MAX_DEGREE]; // the coefficients (0..10, 0..20, 0..21)

public:

// the default constructor (initializes all member variables) Polynomial();
// binary operator* to multiple to polynomials
// arguments are read-only, signified by const
// the operator* returns a fresh polynomial with degree i+j Polynomial operator*( const Polynomial& aRight ) const;
// input operator for polynomials
friend std::istream& operator>>( std::istream& aIStream,
Polynomial& aObject );
// output operator for polynomials
friend std::ostream& operator<<( std::ostream& aOStream,
const Polynomial& aObject );
// new methods in problem set 1
// calculate polynomial for a given x (i.e., parameter aX)
double calculate( double aX ) const;
// build indefinite integral
// the indefinite integral is a fresh polynomial with degree fDegree+1 // the method does not change the current object
Polynomial buildIndefiniteIntegral() const;
// build definite integral
// the method does not change the current object
// the method computes the indefinite integral and then calculates it // for xlow and xhigh and returns the difference
double buildDefiniteIntegral( double aXLow, double aXHigh ) const;
};


main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream> #include "Polynomial.h" using namespace std;
int main() {
Polynomial A;
cout << "Specify polynomial:" << endl; cin >> A;
cout << "A = " << A << endl;
double x;
cout << "Specify value of x:" << endl; cin >> x;
cout << "A(x) = " << A.calculate( x ) << endl;
cout << "Indefinite integral of A = "
<< A.buildIndefiniteIntegral() << endl;
cout << "Definite integral of A(xlow=0, xhigh=12.0) = " << A.buildDefiniteIntegral( 0, 12.0 ) << endl;
return 0; }


my implementation

poly.cpp

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
#include "poly.h"

using namespace std;

Polynomial::Polynomial()
{
	fDegree = 0;

	for (int i = 0; i < MAX_DEGREE; i++)
	{
		fCoeffs[i] = 0.0;
	}
}

Polynomial Polynomial::operator*(const Polynomial& aRight) const
{
	Polynomial Result;

	Result.fDegree = fDegree + aRight.fDegree;

	for (int i = 0; i <= fDegree; i++)
	{
		for (int j = 0; j <= aRight.fDegree; j++)
		{
			// aggregate (sum up) ith and jth coefficients
			Result.fCoeffs[i + j] += fCoeffs[i] * aRight.fCoeffs[j];
		}
	}

	return Result;
}

istream& operator>>(istream& aIStream, Polynomial& aObject)
{
	// read degree
	aIStream >> aObject.fDegree;

	// read coefficients (assume sound input)
	for (int i = 0; i <= aObject.fDegree; i++)
	{
		aIStream >> aObject.fCoeffs[i];
	}

	return aIStream;
}

ostream& operator<<(ostream& aOStream, const Polynomial& aObject)
{
	bool lMustAddPlus = false;

	for (int i = 0; i <= aObject.fDegree; i++)
	{
		if (aObject.fCoeffs[i] != 0.0)
		{
			if (lMustAddPlus)
				aOStream << " + ";

			aOStream << aObject.fCoeffs[i] << "x^" << i;
			lMustAddPlus = true;
		}
	}

	return aOStream;
}


Please help me solve this and explanation would be helpful too. Thanks
Topic archived. No new replies allowed.