 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Henry Jordon Guest
|
Posted: Mon Jun 28, 2004 2:06 am Post subject: not sure how to create the main for this program |
|
|
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
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::Polynomial( 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::Polynomial( 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::operator+( 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::operator=( 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::operator[]( 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::operator()( 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.
|
|
| Back to top |
|
 |
David White Guest
|
Posted: Mon Jun 28, 2004 5:29 am Post subject: Re: not sure how to create the main for this program |
|
|
"Henry Jordon" <buffer_88 (AT) hotmail (DOT) com> wrote
| Quote: | 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
|
It is customary not to have a space after the #.
| Quote: | using namespace std;
|
This 'using' is a bad idea. It means that any source file that includes this
file is forced to accept all names in the 'std' namespace.
| Quote: | //*********************************************
// Declare a useful constant
//
double zero = 0.0 ;
|
This needs a 'const' in front of it, firstly because it is appropriate for a
constant, and secondly because without it it is a global variable, but you
can't define a global variable in a header file if it is to be included in
more than source file because you would have multiple clashing variables
with the same name. With the 'const' the name is not treated by the compiler
as having external linkage (i.e., global, or accessible from multiple source
files).
const double zero = 0.;
| Quote: |
//*********************************************
// 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 )
;
} ;
//__________________________________________________________
|
All the functions below should be in a source file (.cpp, .cxx or whatever
your compiler uses) or left in the header and declared 'inline' if you need
the speed. You can't leave them in the header without the 'inline' or you'll
have the same multiple-copies problem you have with 'zero' above.
// Start of source file Polynomial.cpp
#include "Polynomial.h"
| Quote: | Polynomial::Polynomial( 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::Polynomial( 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::operator+( 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::operator=( 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::operator[]( 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::operator()( 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.
|
An example main, which would normally be in a new source file:
#include
#include "Polynomial.h"
int main()
{
Polynomial p(2);
p[0] = 1.;
p[1] = -2.;
p[2] = 2.;
Polynomial q(p);
Polynomial r = q + p;
std::cout << p << r;
}
I didn't check all your code, but this program seemed to work okay when I
ran it. The output was:
2 x^2 - 2 x + 14 x^2 - 4 x + 2
DW
|
|
| Back to top |
|
 |
Howard Guest
|
Posted: Mon Jun 28, 2004 3:33 pm Post subject: Re: not sure how to create the main for this program |
|
|
| Quote: | An example main, which would normally be in a new source file:
#include
#include "Polynomial.h"
int main()
{
Polynomial p(2);
p[0] = 1.;
p[1] = -2.;
p[2] = 2.;
Polynomial q(p);
Polynomial r = q + p;
std::cout << p << r;
}
I didn't check all your code, but this program seemed to work okay when I
ran it. The output was:
2 x^2 - 2 x + 14 x^2 - 4 x + 2
DW
|
How nice of you to do his homework for him!
Reading the original post, there isn't even the slightest hint as to what is
*supposed* to be in the "main" unit. I suppose we were not only supposed to
do his coding for him, but his thinking as well?
I think you've really done Henry a disservice by allowing him to do
absolutely no work on this, and thus proceed to the next part of his class
with no knowledge of how he got there.
-Howard
|
|
| Back to top |
|
 |
David White Guest
|
Posted: Mon Jun 28, 2004 10:49 pm Post subject: Re: not sure how to create the main for this program |
|
|
"Howard" <alicebt (AT) hotmail (DOT) com> wrote
| Quote: |
An example main, which would normally be in a new source file:
#include
#include "Polynomial.h"
int main()
{
Polynomial p(2);
p[0] = 1.;
p[1] = -2.;
p[2] = 2.;
Polynomial q(p);
Polynomial r = q + p;
std::cout << p << r;
}
I didn't check all your code, but this program seemed to work okay when
I
ran it. The output was:
2 x^2 - 2 x + 14 x^2 - 4 x + 2
DW
How nice of you to do his homework for him!
|
I don't think I did.
| Quote: | Reading the original post, there isn't even the slightest hint as to what
is
*supposed* to be in the "main" unit. I suppose we were not only supposed
to
do his coding for him, but his thinking as well?
I think you've really done Henry a disservice by allowing him to do
absolutely no work on this,
|
Does all that code look like "absolutely no work" to you?
and thus proceed to the next part of his class
| Quote: | with no knowledge of how he got there.
|
My policy regarding homework questions is the same as yours. Perhaps you are
right; I don't know, but it seemed to me that the 'main' was small and
trivial part of what he was asked to do.
DW
|
|
| Back to top |
|
 |
Howard Guest
|
Posted: Tue Jun 29, 2004 3:29 pm Post subject: Re: not sure how to create the main for this program |
|
|
"David White" <no (AT) email (DOT) provided> wrote
| Quote: | I think you've really done Henry a disservice by allowing him to do
absolutely no work on this,
Does all that code look like "absolutely no work" to you?
and thus proceed to the next part of his class
with no knowledge of how he got there.
My policy regarding homework questions is the same as yours. Perhaps you
are
right; I don't know, but it seemed to me that the 'main' was small and
trivial part of what he was asked to do.
|
I just re-read his post, and perhaps you are right about the work involved.
I thought that all that code was written by the professor. His run-on
sentence (below) left my brain addled. :-)
| Quote: | 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.
|
Obviously an English major! :-)
-Howard
|
|
| Back to top |
|
 |
David Rubin Guest
|
Posted: Tue Jun 29, 2004 8:12 pm Post subject: Re: not sure how to create the main for this program |
|
|
"David White" <no (AT) email (DOT) provided> wrote
| Quote: | An example main, which would normally be in a new source file:
#include <iostream
#include "Polynomial.h"
int main()
{
Polynomial p(2);
p[0] = 1.;
p[1] = -2.;
p[2] = 2.;
Polynomial q(p);
Polynomial r = q + p;
std::cout << p << r;
}
|
You need to define operator== for your Polynomial since it has value
semantics. Then, you use your 'main' as an opportunity to verify your
code. For example, here is a class definition for a Point class:
class Point
{
double d_x;
double d_y;
public:
// CREATORS
Point();
Point(const Point& p);
Point(double x, double y);
~Point();
// MANIPULATORS
Point& operator=(const Point& p);
// ACCESSORS
double x() const;
double y() const;
};
// ==============
// FREE OPERATORS
// ==============
inline bool operator==(const Point& lhs, const Point& rhs);
inline bool operator!=(const Point& lhs, const Point& rhs);
std::ostream& operator<<(std::ostream& out, const Point& p);
-----
Here is a portion of a 'main' routine which validates the value
semantics of Point:
// main.cpp
#include "point.h"
#include
#include <iostream>
using std::cout;
using std::endl;
#define ASSERT(X) aSsErT(!(X), #X, __LINE__)
int testStatus = 0;
static
void aSsErT(int c, const char *s, int i) {
if (c) {
cout << "Error " << __FILE__ << "(" << i << "): " << s
<< " (failed)" << endl;
if (testStatus >= 0 && testStatus <= 100) ++testStatus;
}
}
int main(int argc, char *argv[])
{
int test = std::atoi(argv[1]);
switch(test){
// Add new test cases here.
case 1: {
// -------------------------------------------------------------------
// Concerns:
// Verify the value semantics of the 'Point' class.
//
// Plan:
// create a default Point (0,0) {a 0,0)}
// exercise equality operator {a==: a}
// create an explicit Point (0,0) {a 0,0) b 0,0)}
// exercise equality operator {b==: a b}
// create a copy of a {a 0,0) b 0,0) c 0,0)}
// exercise equality operator {c==: a b c}
// create a Point at (1,2) {a 0,0) b 0,0) c 0,0)
d 1,2)}
// exercise equality operator {d==: a b c d}
// assign d to c {a 0,0) b 0,0) c 1,2)
d 1,2)}
// exercise equality operator {c==: a b c d}
// assign b to a temporary Point (3,4) {a 0,0) b 3,4) c 1,2)
d 1,2)}
// exercise equality operator {b==: a b c d}
// assign a to a {a 0,0) b 3,4) c 1,2)
d 1,2)}
// exercise equality operator {a==: a b c d}
//
// Tests:
// default constructor
// copy constructor
// constructor(x, y)
// destructor
// operator=()
// operator==()
// x() const
// y() const
// -------------------------------------------------------------------
cout << "case 1" << endl
<< "======" << endl;
Point a;
ASSERT(0.0 == a.x());
ASSERT(0.0 == a.y());
ASSERT(a == a);
Point b(0.0, 0.0);
ASSERT(0.0 == b.x());
ASSERT(0.0 == b.y());
ASSERT(b == a);
ASSERT(b == b);
Point c(a);
ASSERT(c == a);
ASSERT(c == b);
ASSERT(c == c);
Point d(1,2);
ASSERT(1.0 == d.x());
ASSERT(2.0 == d.y());
ASSERT(d != a);
ASSERT(d != b);
ASSERT(d != c);
ASSERT(d == d);
c = d;
ASSERT(c != a);
ASSERT(c != b);
ASSERT(c == c);
ASSERT(c == d);
b = Point(3,4); // important if you allocate from heap
ASSERT(3.0 == b.x());
ASSERT(4.0 == b.y());
ASSERT(b != a);
ASSERT(b == b);
ASSERT(b != c);
ASSERT(b != d);
a = a; // important if you allocate form heap
ASSERT(a == a);
ASSERT(a != b);
ASSERT(a != c);
ASSERT(a != d);
} break;
default: {
cerr << "WARNING: CASE " << test << " NOT FOUND" << endl;
testStatus = -1;
} break;
}
if(testStatus > 0){
cerr << "Error: non-zero test status: status=" << testStatus
<< endl;
}
return testStatus;
}
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|