H
Henry Jordon
hello I was wondering if someone could help me get a main going on
this project I've completed the header file that the professor started
us on but not really sure how to get the main going. If someone could
please give me some pointers it would greatly be appreciated. Again
thanks for the help.
Henry
headerfile:
# ifndef _Polynomial_hdr_jordan
# define _Polynomial_hdr_jordan
# include <iomanip> // for the << operator
# include <math> // for fabs()
using namespace std;
//*********************************************
// Declare a useful constant
//
double zero = 0.0 ;
//*********************************************
// Declare the class. All data are private,
// all functions are public.
//
class Polynomial {
//*********************************************
// The private part of the class: the
// variables declared here cannot be used by
// functions that are not part of the class.
//
private:
int degree ;
double *coefs ;
//*********************************************
// The public part of the class: the
// functions declared here can be used in
// any context.
//
public:
// Constructors
Polynomial( const int d ) ; // constructs a zero poly with
given degree
Polynomial( const Polynomial &orig ) ; // duplicates a
polynomial
// Destructor
~Polynomial( void ) ; // releases memory
// Ring ops
Polynomial operator+( const Polynomial &right ) ;
// Assignment op
Polynomial &operator=( const Polynomial &right ) ;
// Access coefs
double &operator[]( int n ) ;
// Evaluate
double operator()( double x ) ;
// Printing
friend ostream &operator <<( ostream &to, const Polynomial p )
;
} ;
//__________________________________________________________
Polynomial:olynomial( const int d )
//*************************************************
// This constructor make a polynomial with a
// user-specified degree.
//
{
int j ; // a loop index used when
// working through the coefs
degree = d ;
coefs = new double[degree + 1] ; // dynamic allocation
// Set all coefficients to zero
for( j=0 ; j <= degree ; j++ ) {
coefs[j] = 0.0 ;
}
}
//__________________________________________________________
Polynomial:olynomial( const Polynomial &orig )
//****************************************************
// Make a copy of an existing Polynomial
//
{
int j ; // a loop index used when
// working through the coefs
degree = orig.degree ;
coefs = new double[degree + 1] ; // dynamic allocation
// Set all coefficients to zero
for( j=0 ; j <= degree ; j++ ) {
coefs[j] = orig.coefs[j] ;
}
}
//__________________________________________________________
Polynomial::~Polynomial( void )
//****************************************************
// Release the memory allocated (via new[]) that
// holds the coefficients
//
{
delete[] coefs ; // Note use of delete[], partner of new[]
}
//__________________________________________________________
Polynomial Polynomial:perator+( const Polynomial &right )
//****************************************************
// Add the Polynomials (*this) and right.
//
{
int j, max_degree ;
//****************************************************
// Discover the maximal degree of the operands.
//
if( (*this).degree > right.degree ) {
max_degree = (*this).degree ;
}
else {
max_degree = right.degree ;
}
//****************************************************
// Construct a zero polynomial whose degree is
// high enough to hold the result.
//
Polynomial sum( max_degree ) ;
//****************************************************
// Add in the terms from (*this) . . .
//
for( j=0 ; j <= (*this).degree ; j++ )
sum.coefs[j] += (*this).coefs[j] ;
//****************************************************
// . . . then those from right.
//
for( j=0 ; j <= right.degree ; j++ )
sum.coefs[j] += right.coefs[j] ;
return( sum ) ;
}
//__________________________________________________________
Polynomial &Polynomial:perator=( const Polynomial &right )
//******************************************************
// Make an assignment, taking care not to
// waste memory. In the comments below lhs (rhs)
// refer to the left-hand-side (right-hand-side)
// of an expression involving the assignment
// operator.
//
{
int j ; // loop counter
delete[] coefs ; // release old coefficient array
coefs = new double[right.degree + 1] ; // allocate new array
// Copy coefficients from rhs to lhs of =
for( j=0 ; j <= right.degree ; j++ )
coefs[j] = right.coefs[j] ;
// set degree of lhs
degree = right.degree ;
return( *this ) ;
}
//__________________________________________________________
double &Polynomial:perator[]( int n )
//***********************************************
// Return the coefficient of the x^n term.
// Take care to return a sensible result even
// when n is negative or bigger than the degree.
//
{
if( (n >= 0) && (n <= degree) ) return( coefs[n] ) ;
else return( zero ) ;
}
//__________________________________________________________
double Polynomial:perator()( double x )
//***********************************************
// Evaluate the polynomial at x and return
// the result.
//
{
int j ;
double result ;
//***********************************************
// Add the coefficients one at a time, working
// from highest order to lowest and multiplying
// by x along the way.
//
result = 0.0 ;
for( j=degree ; j >= 0 ; j-- ) {
result *= x ;
result += coefs[j] ;
}
return( result ) ;
}
//__________________________________________________________
ostream &operator <<( ostream &to, const Polynomial p )
//****************************************************************
// Sends a nicely-formatted string representing a Polynomial
// to the specified output stream.
//
{
int j ; // loop index over coefficients
int n_printed ; // counts number of terms printed
n_printed = 0 ;
for( j = p.degree ; j > 0 ; j-- ) {
//*****************************************
// Print only the nonzero coefficients
//
if( p.coefs[j] != 0.0 ) {
if( n_printed != 0 ) {
//*********************************
// This is not the leading tern:
// print a + or a - sign.
//
if( p.coefs[j] > 0 )
to << " + " ;
else // p.coefs[j] < 0
to << " - " ;
}
else if( p.coefs[j] < 0 ) {
to << " - " ;
}
//*************************************
// Print the coefficient itself,
// unless it's one. Even then, make
// an exception for the constant term.
//
if( (fabs( p.coefs[j]) != 1.0) || (j == 0) )
to << fabs( p.coefs[j] ) ;
//**************************************
// Print an appropriate power of x.
//
if( j > 1 ) to << " x^" << j ;
else if( j == 1 ) to << " x" ;
n_printed++ ;
}
}
//*************************************************
// Treat the constant term specially
//
if( n_printed > 0 ) {
if( p.coefs[0] > 0 )
to << " + " << p.coefs[0] ;
else // coefs[0] < 0
to << " - " << fabs( p.coefs[0] ) ;
}
else { // n_printed == 0
to << p.coefs[0] ;
}
return( to ) ;
}
# endif // closes # ifndef _Polynomial_hdr_jordan
PS: hopefully the comments help you to understand the program if not
please ask me to put more in, in the areas of confusion. Again thanks
for the help.
this project I've completed the header file that the professor started
us on but not really sure how to get the main going. If someone could
please give me some pointers it would greatly be appreciated. Again
thanks for the help.
Henry
headerfile:
# ifndef _Polynomial_hdr_jordan
# define _Polynomial_hdr_jordan
# include <iomanip> // for the << operator
# include <math> // for fabs()
using namespace std;
//*********************************************
// Declare a useful constant
//
double zero = 0.0 ;
//*********************************************
// Declare the class. All data are private,
// all functions are public.
//
class Polynomial {
//*********************************************
// The private part of the class: the
// variables declared here cannot be used by
// functions that are not part of the class.
//
private:
int degree ;
double *coefs ;
//*********************************************
// The public part of the class: the
// functions declared here can be used in
// any context.
//
public:
// Constructors
Polynomial( const int d ) ; // constructs a zero poly with
given degree
Polynomial( const Polynomial &orig ) ; // duplicates a
polynomial
// Destructor
~Polynomial( void ) ; // releases memory
// Ring ops
Polynomial operator+( const Polynomial &right ) ;
// Assignment op
Polynomial &operator=( const Polynomial &right ) ;
// Access coefs
double &operator[]( int n ) ;
// Evaluate
double operator()( double x ) ;
// Printing
friend ostream &operator <<( ostream &to, const Polynomial p )
;
} ;
//__________________________________________________________
Polynomial:olynomial( const int d )
//*************************************************
// This constructor make a polynomial with a
// user-specified degree.
//
{
int j ; // a loop index used when
// working through the coefs
degree = d ;
coefs = new double[degree + 1] ; // dynamic allocation
// Set all coefficients to zero
for( j=0 ; j <= degree ; j++ ) {
coefs[j] = 0.0 ;
}
}
//__________________________________________________________
Polynomial:olynomial( const Polynomial &orig )
//****************************************************
// Make a copy of an existing Polynomial
//
{
int j ; // a loop index used when
// working through the coefs
degree = orig.degree ;
coefs = new double[degree + 1] ; // dynamic allocation
// Set all coefficients to zero
for( j=0 ; j <= degree ; j++ ) {
coefs[j] = orig.coefs[j] ;
}
}
//__________________________________________________________
Polynomial::~Polynomial( void )
//****************************************************
// Release the memory allocated (via new[]) that
// holds the coefficients
//
{
delete[] coefs ; // Note use of delete[], partner of new[]
}
//__________________________________________________________
Polynomial Polynomial:perator+( const Polynomial &right )
//****************************************************
// Add the Polynomials (*this) and right.
//
{
int j, max_degree ;
//****************************************************
// Discover the maximal degree of the operands.
//
if( (*this).degree > right.degree ) {
max_degree = (*this).degree ;
}
else {
max_degree = right.degree ;
}
//****************************************************
// Construct a zero polynomial whose degree is
// high enough to hold the result.
//
Polynomial sum( max_degree ) ;
//****************************************************
// Add in the terms from (*this) . . .
//
for( j=0 ; j <= (*this).degree ; j++ )
sum.coefs[j] += (*this).coefs[j] ;
//****************************************************
// . . . then those from right.
//
for( j=0 ; j <= right.degree ; j++ )
sum.coefs[j] += right.coefs[j] ;
return( sum ) ;
}
//__________________________________________________________
Polynomial &Polynomial:perator=( const Polynomial &right )
//******************************************************
// Make an assignment, taking care not to
// waste memory. In the comments below lhs (rhs)
// refer to the left-hand-side (right-hand-side)
// of an expression involving the assignment
// operator.
//
{
int j ; // loop counter
delete[] coefs ; // release old coefficient array
coefs = new double[right.degree + 1] ; // allocate new array
// Copy coefficients from rhs to lhs of =
for( j=0 ; j <= right.degree ; j++ )
coefs[j] = right.coefs[j] ;
// set degree of lhs
degree = right.degree ;
return( *this ) ;
}
//__________________________________________________________
double &Polynomial:perator[]( int n )
//***********************************************
// Return the coefficient of the x^n term.
// Take care to return a sensible result even
// when n is negative or bigger than the degree.
//
{
if( (n >= 0) && (n <= degree) ) return( coefs[n] ) ;
else return( zero ) ;
}
//__________________________________________________________
double Polynomial:perator()( double x )
//***********************************************
// Evaluate the polynomial at x and return
// the result.
//
{
int j ;
double result ;
//***********************************************
// Add the coefficients one at a time, working
// from highest order to lowest and multiplying
// by x along the way.
//
result = 0.0 ;
for( j=degree ; j >= 0 ; j-- ) {
result *= x ;
result += coefs[j] ;
}
return( result ) ;
}
//__________________________________________________________
ostream &operator <<( ostream &to, const Polynomial p )
//****************************************************************
// Sends a nicely-formatted string representing a Polynomial
// to the specified output stream.
//
{
int j ; // loop index over coefficients
int n_printed ; // counts number of terms printed
n_printed = 0 ;
for( j = p.degree ; j > 0 ; j-- ) {
//*****************************************
// Print only the nonzero coefficients
//
if( p.coefs[j] != 0.0 ) {
if( n_printed != 0 ) {
//*********************************
// This is not the leading tern:
// print a + or a - sign.
//
if( p.coefs[j] > 0 )
to << " + " ;
else // p.coefs[j] < 0
to << " - " ;
}
else if( p.coefs[j] < 0 ) {
to << " - " ;
}
//*************************************
// Print the coefficient itself,
// unless it's one. Even then, make
// an exception for the constant term.
//
if( (fabs( p.coefs[j]) != 1.0) || (j == 0) )
to << fabs( p.coefs[j] ) ;
//**************************************
// Print an appropriate power of x.
//
if( j > 1 ) to << " x^" << j ;
else if( j == 1 ) to << " x" ;
n_printed++ ;
}
}
//*************************************************
// Treat the constant term specially
//
if( n_printed > 0 ) {
if( p.coefs[0] > 0 )
to << " + " << p.coefs[0] ;
else // coefs[0] < 0
to << " - " << fabs( p.coefs[0] ) ;
}
else { // n_printed == 0
to << p.coefs[0] ;
}
return( to ) ;
}
# endif // closes # ifndef _Polynomial_hdr_jordan
PS: hopefully the comments help you to understand the program if not
please ask me to put more in, in the areas of confusion. Again thanks
for the help.