Assignment 4 Solutions
Solution polynomial.cc code:
/*******************************************************************
* Name: Brenda Student
* Date: 3/16/14
* Course: CSCI 132 Section 01
* Assignment: Assignment 4
* Instructor: Royden
* Program: polynomial.cc
* Purpose: Implementation of polynomial class
***************************************************************************/
#include <iostream>
#include "polynomial.h"
using namespace std;
void Polynomial :: print( ) const
/* Post: The Polynomial is printed to cout. */
{
Node *print_node = front;
bool first_term = true;
while (print_node != NULL) {
Term &print_term = print_node->entry;
if (first_term) { // In this case, suppress printing an initial +.
first_term = false;
if (print_term.coefficient < 0) cout << "- ";
}
else if (print_term.coefficient < 0) cout << " - ";
else cout << " + ";
double r = (print_term.coefficient >= 0)
? print_term.coefficient : -(print_term.coefficient);
if (r != 1) cout << r;
if (print_term.degree > 1) cout << " X^" << print_term.degree;
if (print_term.degree == 1) cout << " X";
if (r == 1 && print_term.degree == 0) cout << " 1";
print_node = print_node->next;
}
if (first_term)
cout << "0"; // Print 0 for an empty Polynomial.
cout << endl;
}
void Polynomial :: read( )
/* Post: The Polynomial is read from cin. */
{
clear( );
double coefficient;
int last_exponent, exponent;
bool first_term = true;
cout << "Enter the coefficients and exponents for the polynomial, "
<< "one pair per line. Exponents must be in descending order." << endl
<< "Enter a coefficient of 0 or an exponent of 0 to terminate." << endl;
do {
cout << "coefficient? " << flush;
cin >> coefficient;
if (coefficient != 0.0) {
cout << "exponent? " << flush;
cin >> exponent;
if ((!first_term && exponent >= last_exponent) || exponent < 0) {
exponent = 0;
cout << "Bad exponent: Polynomial terminates without its last term."
<< endl;
}
else {
Term new_term(exponent, coefficient);
append(new_term);
first_term = false;
}
last_exponent = exponent;
}
} while (coefficient != 0.0 && exponent != 0);
}
void Polynomial :: equals_sum(Polynomial p, Polynomial q)
/* Post: The Polynomial object is reset as the sum of the two parameters. */
{
clear( );
while (!p.empty( ) || !q.empty( )) {
Term p_term, q_term;
if (p.degree( ) > q.degree( )) {
p.serve_and_retrieve(p_term);
append(p_term);
}
else if (q.degree( ) > p.degree( )) {
q.serve_and_retrieve(q_term);
append(q_term);
}
else {
p.serve_and_retrieve(p_term);
q.serve_and_retrieve(q_term);
if (p_term.coefficient + q_term.coefficient != 0) {
Term answer_term(p_term.degree,
p_term.coefficient + q_term.coefficient);
append(answer_term);
}
}
}
}
void Polynomial :: equals_difference(Polynomial p, Polynomial q)
/* Post: The Polynomial object is reset as the difference of the two parameters. */
{
clear( );
while (!p.empty( ) || !q.empty( )) {
Term p_term, q_term;
if (p.degree( ) > q.degree( )) {
p.serve_and_retrieve(p_term);
append(p_term);
}
else if (q.degree( ) > p.degree( )) {
q.serve_and_retrieve(q_term);
q_term.coefficient = -q_term.coefficient;
append(q_term);
}
else {
p.serve_and_retrieve(p_term);
q.serve_and_retrieve(q_term);
if (p_term.coefficient - q_term.coefficient != 0) {
Term answer_term(p_term.degree,
p_term.coefficient - q_term.coefficient);
append(answer_term);
} //end if
} //end if--else
} //end while
} //end equals_difference()
void Polynomial :: equals_product(Polynomial p, Polynomial q)
/* Post: The Polynomial object is reset as the product of the two parameters. */
{
clear();
Polynomial r, s; //Temporary polynomials for storing intermediate results
while(!q.empty()) { //Sum results of multiplying p by each term of q
Term oneTerm;
q.serve_and_retrieve(oneTerm);
r.mult_term(p, oneTerm);
s.equals_sum(s, r);
} //end while
while(!s.empty()) { //transfer polynomial s into calling object
Term theTerm;
s.serve_and_retrieve(theTerm);
append(theTerm);
} //end while
} //end equals_product
Error_code Polynomial :: equals_quotient(Polynomial p, Polynomial q)
{
return success;
}
void Polynomial :: mult_term(Polynomial p, Term t)
/* Post: The Polynomial object is reset as the product of the two parameters (one
polynomial and one term. */
{
clear();
while(!p.empty()) {
Term theTerm;
p.serve_and_retrieve(theTerm);
theTerm.degree += t.degree;
theTerm.coefficient *= t.coefficient;
append(theTerm);
} //end while
} //end mult_term
double Polynomial :: evaluate (double number)
/* Post: Evaluates the Polynomial object at number. */
{
Polynomial p = *this; //uses the object "receiving the message", but
//does not change the object data.
double answer;
Term pterm;
while(p.serve_and_retrieve(pterm) == success) {
double addon = 1.0;
for (int i = 0; i < pterm.degree; i++)
addon = addon * number;
addon = addon * pterm.coefficient;
answer = answer + addon;
} //end while
return answer;
} //end evaluate
int Polynomial :: degree( ) const
/* Post: If the Polynomial is identically 0, a result of -1 is returned.
Otherwise the degree of the Polynomial is returned. */
{
if (empty( )) return -1;
Term lead;
retrieve(lead);
return lead.degree;
}
Home | | Syllabus | | Assignments | | Lecture Notes