CS
215 - Fundamentals of Programming II
Spring 2008 - Project 6
40
points
Out:
April 2, 2008
Due: April 16, 2008
A polynomial is a mathematical expression involving a sum of powers in one or more variables multiplied by coefficients. A polynomial of degree n in one variable with constant coefficients is given by
cnxn + cn-1xn-1 + ... + c1x + c0
Generally the terms where the integer coefficients ci are 0 are not written, except for the polynomial of a single constant term of value 0. There are various implementation of polynomials. One possible implementation is to represent a polynomial as a vector of integers where the value at index i is the coefficient ci of the term civi. If the polynomial is of low degree and few coefficients are 0, this is a reasonable representation. However, if a polynomial is sparse and its degree is high, this representation wastes a lot of space storing the 0 coefficients. E.g., x99 + 1 would require a vector of 100 elements, 98 of which would be 0.
For sparse polynomials, instead of storing all of the coefficients, we would like to store only the terms that exist, so that for the example above, only two terms would be stored. Consider the following specification for a Polynomial class that models mathematical polynomials using a singly-linked list. First, we define a struct for each term.
The Term struct is used to represent a single polynomial term. The term coefficient is generally non-zero and the exponent must be non-negative. For example, a Term with coefficient of 10 and an exponent of 3 represents the term 10x3. The next data member points to a following Term.
|
Object |
Type |
Name |
|
coefficient |
int |
coeff |
|
exponent |
int |
expo |
|
link to next term |
Term* |
next |
There is just one operation other than direct access, the explicit-value constructor that creates a term (node) by initializing the coefficient, exponent, and link to the given arguments. Note that the default values create the constant term 0 value and is the only term that is allowed to have a coefficient of 0.
Analysis
|
Objects |
Default |
Type |
Kind |
Movement |
Name |
|
coefficient |
0 |
int |
variable |
received |
aCoeff |
|
exponent |
0 |
int |
variable |
received |
anExpo |
|
link to next term |
0 |
Term* |
variable |
received |
aLink |
The definition for the Term struct and its constructor should be placed in-line in the private area of the Polynomial class definition.
Specification for Polynomial Class
|
Object |
Type |
Name |
|
pointer to first term |
Term* |
myFirst |
A polynomial is modeled using a linked list of Term nodes. The only attribute of the class is a pointer variable to the first Term of the polynomial. The Terms in the list are to be kept in decreasing exponent order. Only terms with non-zero coefficients should be part of a Polynomial object. However, all Polynomial objects must have at least one term, which implies that there is one exception to the non-zero coefficient rule - the constant term 0 value. The diagram below illustrates the structure of the polynomial 10x3 - 3x + 6.

Explicit-value constructor - creates a polynomial from a vector of coefficients and a vector of exponents. You should assume that both vectors are the same size and that the exponents are in decreasing order. (I.e., you do not need to do error checking or sort the terms.) The ith coefficient is matched with the ith power to form a term of the polynomial. If the vectors are empty, the default constructed polynomial should be the constant term 0 value.
Analysis
|
Objects |
Default |
Type |
Kind |
Movement |
Name |
|
vector of coefficients |
vector<int>( ) |
vector<int> |
variable |
received |
initialCoefficients |
|
vector of exponents |
vector<int>( ) |
vector<int> |
variable |
received |
initialExponents |
Copy constructor - creates a new Polynomial that is identical to an existing one.
Analysis
|
Objects |
Type |
Kind |
Movement |
Name |
|
original polynomial object |
Polynomial |
variable |
received |
originalPoly |
Destructor - deallocates the Term nodes
Analysis - no objects
operator= - overloaded assignment operator function. Makes an existing Polynomial object identical to the original Polynomial object.
Analysis
|
Objects |
Type |
Kind |
Movement |
Name |
|
original polynomial object |
Polynomial |
variable |
received |
originalPoly |
|
this polynomial object |
Polynomial |
variable |
returned |
*this |
Degree - returns the highest exponent of the polynomial
Analysis
|
Objects |
Type |
Kind |
Movement |
Name |
|
highest exponent |
int |
variable |
returned |
------- |
Evaluate - evaluate the polynomial at particular point, return result
Analysis
|
Objects |
Type |
Kind |
Movement |
Name |
|
point of evaluation |
double |
variable |
received |
x |
|
result of evaluation |
double |
variable |
returned |
result |
operator+ - friend overloaded arithmetic operator+. Returns a Polynomial object that is the sum of the parameters. The terms of the sum of two polynomials is the sum of the coefficients of the terms with the same exponent in each polynomial. Be aware that terms may drop out. Note: for this to work, the copy constructor must be working.
Analysis
|
Objects |
Type |
Kind |
Movement |
Name |
|
first operand |
Polynomial |
variable |
received |
leftPoly |
|
second operand |
Polynomial |
variable |
received |
rightPoly |
|
sum polynomial |
Polynomial |
variable |
returned |
sumPoly |
operator* - friend overloaded arithmetic operator*. Returns a Polynomial object that is the product of the parameters. The product of two polynomials is obtained by multiplying term by term and combining the results . Be aware that terms may drop out. Note: for this to work, the copy constructor must be working.
Analysis
|
Objects |
Type |
Kind |
Movement |
Name |
|
first operand |
Polynomial |
variable |
received |
leftPoly |
|
second operand |
Polynomial |
variable |
received |
rightPoly |
|
product polynomial |
Polynomial |
variable |
returned |
prodPoly |
operator<< - friend overloaded operator function that writes Polynomial to an output stream in mathematical format with exponentiation represented by ^ and no spaces between the terms. For example, x^3-2x^2+2x+1. Note that negative coefficients are written as subtraction (unless it is the first term), non-constant terms with coefficient 1 or -1 do not show the coefficient, terms with power 1 do not show an exponent, and the constant term does not show x or an exponent.
Analysis
|
Objects |
Type |
Kind |
Movement |
Name |
|
output stream |
ostream |
variable |
received, passed back, and returned |
out |
|
polynomial object |
Polynomial |
variable |
received |
aPoly |
operator>> - friend overloaded operator function that reads Polynomial values from an input stream without prompting in mathematical format with exponentiation represented by ^ and without spaces. For example, x^3-2x^2+2x+1. Note that negative coefficients are written as subtraction (unless it is the first term), non-constant terms with coefficient 1 or -1 do not show the coefficient, terms with power 1 do not show an exponent, and the constant term does not show x or an exponent. Assumes that the powers are in decreasing order. (I.e., you do not need to do error checking or sort the terms.)
Analysis
|
Objects |
Type |
Kind |
Movement |
Name |
|
input stream |
istream |
variable |
received, passed back, and returned |
in |
|
polynomial object |
Polynomial |
variable |
passed back |
aPoly |
Notes: Since both coefficients and exponents may be missing, you may want to consider using in.peek() to look ahead into the input stream or in.putback(ch), which will put a character back into the input stream. E.g., a character could be read and determined to be a numeric digit, then put back into the input stream to be read again as part of an integer. Also, this operator should be able to skip any whitespace occurring before the input and not remove the whitespace occurring after the input. For the purposes of this project, you may assume that there will always be whitespace after a Polynomial input (one or more spaces, tabs, newlines, or the end of file).
Write the implementation for this Polynomial class. Note that the function names for this class must be as specified above. The Polynomial class definition and friend operator function prototypes should be put in header file polynomial.h with suitable compilation guards. The implementations of the Polynomial class and friend operator functions should be put in source file polynomial.cpp. See implementation notes below.
Write a main program that adequately tests your Polynomial class in file polydriver.cpp. This program should demonstrate that your Polynomial class meets all of the specifications given above. Part of your grade will depend on how well you test your class. In addition, the submission system will run a specific driver program to test your Polynomial class.
The Polynomial class must be implemented using a linked list. Projects that do not use linked lists will be returned for resubmission with late penalty. Also, the copy constructor, assignment operator=, addition operator+, multiplication operator*, and input operator>> cannot be implemented using the explicit-value constructor. (I.e., you are not allowed to figure out what the coefficients and exponents are, put them in vectors and call the explicit-value constructor to create a Polynomial object to use as the result. However, you may, as usual, write (private) helper functions that may be useful for more than one operation.)
Note: for the friend overloaded operator functions, to access the Term definition you need to prefix it with Polynomial:: (the scope resolution operator). E.g.,
Polynomial::Term * ptr = aPoly.myFirst;
This is needed because the compiler needs to know where the definition of Term is.
You must submit a makefile named Makefile.project6 that creates executable named polydriver for your project. Submissions without working makefiles will be assessed up to a 3-point penalty as indicated in the syllabus. It should conform to the examples given in the handout Very Basic make and demonstrated in class.
REMINDER: Your project must compile for it to be graded. Submissions that do not compile will be returned for resubmission and assessed a late penalty. Submissions that do not substantially work also will be returned for resubmission and assessed a late penalty.
Follow the guidelines in the C++ Programming Style Guideline handout. As stated in the syllabus, part of the grade on a programming project depends on how well you adhere to the guidelines. The grader will look at your code listing and grade it according to the guidelines.
Electronically submit a tarfile containing Makefile.project6, polynomial.h, polynomial.cpp, and polydriver.cpp as explained in the handout Submission Instructions for CS 215. Turn in a hardcopy of Makefile.project6, polynomial.h, polynomial.cpp, and polydriver.cpp.
04/01/08