#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; }