Hello,
beachlounger@aol.com wrote:
Quote:
Using C++ language to complete this homework
>
1. Develop class Polynomial. The internal representation of a
Polynomial is an array of terms. Each term contains a coefficient and
an exponent. The term
>
2x4
has the coefficient 2 and the exponent 4. Develop a complete class
containing proper constructor functions as well as set and get
functions. The class should also provide the following overloaded
operator capabilities:
>
(a) Overload the addition operator (+) to add two Polynomials.
(b) Overload the multiplication operator (*) to multiply two
Polynomials.
(c) Overload the subtraction assignment operator (?=) to process two
Polynomials.
|
Think about the mathematical structure. A univariate polynomial p looks
like p(x)=sum c_i x^i. Basically the sequence of the c_i is enough to
characterize it. Let there be a maximal i for every polynomial with
non-zero c_i. Now there are two observations possible.
The first is that polynomials form a module, or a vector space if the
coefficients are from a field. The vectors have a maximal index with
the corresponding entry non-zero. All coefficients up to this index are
stored, all others not.
The second observation is, that a sequence is a mapping from the natural
numbers to the coefficients, and zero coefficients are the default, so
you need to map only those finitely many exponents with non-zero
coefficient.
If you look through the tools the C++ standard library offers to you,
you will find vector and map. If you employ vector in the most direct
way, you will get something called dense polynomials, in the other case
with map you will get sparse polynomials. When dealing with polynomials
the selection between those two has to be done carefully to get
efficient solutions to the problem in question.
Now, for addition of polynomials, you have to add the corresponding
coefficients. You will have to create the result polynomial, traverse
both data structures of the corresponding polynomials and collect the
result. Subtraction is not much different. Multiplication is only a
little bit more involved. Look for formal definitions of the product of
polynomials or folding of sequences.
In the sparse case you will find a close similarity of addition and
subtraction of polynomials to the problem of merging two sorted
seqences. Look for mergesort to get the idea.
Quote:
>
This is what I have so far
>
>
#ifndef POLYNOMIAL_H
#define POLYNOMIAL_H
|
Everything within this #ifndef ... #endif should be in polynomial.h.
Quote:
>
>
class Polynomial {
public:
Polynomial();
Polynomial operator+( const Polynomial& ) const; // addition
Polynomial operator-( const Polynomial& ) const; // subtraction
Polynomial operator*( const Polynomial& ); // multiplication
const Polynomial operator=( const Polynomial&);// assignment
Polynomial& operator+=( const Polynomial& );
Polynomial& operator-=( const Polynomial& );
Polynomial& operator*=( const Polynomial& );
void enterTerms();
void printPolynomial() const;
private:
int exponents[ 100 ]; // exponent array
int coefficients[ 100 ]; // coefficients array
|
Those arrays might be usable as dense and as sparse polynomials. What
special restrictions would the 100 imply, if you use them with dense
and with sparse in mind? If you use them as dense, what would the
content of exponents be in every case.
Quote:
void polynomialCombine( Polynomial& ); // combine common terms
}; // end class Polynomial
|
And the bodies of those member functions of Polynomial are defined
where? polynomial.cc?
You could write all the code into a single file without including other
code you wrote, but this is bad style and prevents reuse. It should be
explained in textbooks where to put what, and there or in
compiler/linker manuals how to compile and link the parts together. You
could perhaps look for the C++ FAQ.
Bernd Strieder