The goal of assignment 4 is to reinforce the dynamic classes in C++. Specificall
ID: 3665869 • Letter: T
Question
The goal of assignment 4 is to reinforce the dynamic classes in C++. Specifically, the assignment is to implement the polynomial class from chapter 4.6 on page 212. But the additional requirement is to implement the class using a dynamic array to store the coefficients. The class needs to have a destructor, copy constructor, and an overloaded assignment operator. The following files are provided for your reference. You should start by modifying the header file first.
here is the header file:
// FILE: poly0.h
// CLASS PROVIDED:
// class polynomial (in the namespace main_savitch_3)
// A polynomial has one variable x, real number coefficients, and
// non-negative integer exponents. Such a polynomial can be viewed
// as having the form:
// A[n]*x^n + A[n-1]*x^(n-1) + ... A[2]*x^2 + A[1]*x + A[0]
// where the A[n] are the real number coefficients and x^i represents
// the variable x raised to the i power. The coefficient A[0] is
// called the "constant" or "zeroth" term of the polynomial.
//
// NOTES TO STUDENT:
// 1. This version works by storing the coefficients in
// a fixed array. The coefficient for the x^k term is stored
// in location [k] of the fixed-size array. Later we will modify
// the implementation to use a dynamic array.
// 2. Note that two functions have been implemented as inline functions
// in this file (the degree and operator() functions).
// 3. An implementation of the make_gif function is available for
// students to use at www.cs.colorado.edu/~main/projects/polygif.cxx.
//
// MEMBER CONSTANTS
// const static size_t CAPACITY
// The size of the fixed array to store the coefficients.
// const static size_t MAX_EX = CAPACITY - 1;
// The maximum exponent permitted.
//
// CONSTRUCTOR for the polynomial class
// polynomial(double c = 0.0, unsigned int exponent = 0)
// PRECONDITION: exponent <= MAX_EX.
// POSTCONDITION: This polynomial has been create with all zero
// coefficients, except for coefficient c for the specified exponent.
// When used as a default constructor (using default values for
// both arguments), the result is a polynomial with all zero
// coefficients.
//
// MODIFICATION MEMBER FUNCTIONS for the polynomial class
// void add_to_coef(double amount, unsigned int exponent)
// PRECONDITION: exponent <= MAX_EX.
// POSTCONDITION: Adds the given amount to the coefficient of the
// specified exponent.
//
// void assign_coef(double coefficient, unsigned int exponent)
// PRECONDITION: exponent <= MAX_EX.
// POSTCONDITION: Sets the coefficient for the specified exponent.
//
// void clear( )
// POSTCONDITION: All coefficients of this polynomial are set to zero.
//
// CONSTANT MEMBER FUNCTIONS for the polynomial class
// double coefficient(unsigned int exponent) const
// POSTCONDITION: Returns coefficient at specified exponent of this
// polynomial.
// NOTE: for exponents > MAX_EX, the return value is always zero.
//
// unsigned int degree( ) const
// POSTCONDITION: The function returns the value of the largest exponent
// with a non-zero coefficient.
// If all coefficients are zero, then the function returns zero.
//
// polynomial derivative( ) const
// POSTCONDITION: The return value is the first derivative of this
// polynomial.
//
// double eval(double x) const
// POSTCONDITION: The return value is the value of this polynomial with the
// given value for the variable x.
//
// unsigned int next_term(unsigned int e) const
// POSTCONDITION: The return value is the next exponent n which is LARGER
// than e such that coefficient(n) != 0.
// If there is no such term, then the return value is zero.
//
// unsigned int previous_term(unsigned int e) const
// POSTCONDITION: The return value is the next exponent n which is SMALLER
// than e such that coefficient(n) != 0.
// If there is no such term, then the return value is UINT_MAX
// from <climits>.
//
// CONSTANT OPERATORS for the polynomial class
// double operator( ) (double x) const
// Same as the eval member function.
//
// NON-MEMBER BINARY OPERATORS for the polynomial Class
// polynomial operator -(const polynomial& p1, const polynomial& p2)
// POSTCONDITION: return-value is a polynomial with each coefficient
// equal to the difference of the coefficients of p1 & p2 for any given
// exponent.
//
// polynomial operator +(const polynomial& p1, const polynomial& p2)
// POSTCONDITION: return-value is a polynomial with each coefficient
// equal to the sum of the coefficients of p1 & p2 for any given
// exponent.
//
// polynomial operator *(const polynomial& p1, const polynomial& p2)
// PRECONDITION: p1.degree( ) + p2.degree( ) <= polynomial::MAX_EX.
// POSTCONDITION: Each term of p1 has been multiplied by each term of p2,
// and the answer is the sum of all these term-by-term products.
// For example, if p1 is 2x^2 + 3x + 4 and p2 is 5x^2 - 1x + 7, then the
// return value is 10x^4 + 13x^3 + 31x^2 + 17x + 28.
//
// NON-MEMBER OUTPUT FUNCTIONS for the polynomial Class
// ostream& operator << (ostream& out, const polynomial& p)
// POSTCONDITION: The polynomial has been printed to ostream out, which,
// in turn, has been returned to the calling function.
//
// void make_gif(
// const polynomial& p,
// const char filename[ ],
// double low_x,
// double high_x,
// double low_y,
// double high_y
// )
// PRECONDITION: filename is a legal filename for a gif file.
// Also (low_x < high_x) && (low_y < high_y).
// POSTCONDITION: A gif file has been written to the specified filename
// with a graphical representation of the polynomial in the specified
// ranges (low_x...high_x and low_y...high_y).
#ifndef POLY0_H
#define POLY0_H
#include <iostream> // Provides ostream
class polynomial
{
public:
// CONSTANTS
typedef int value_type;
typedef std::size_t size_type;
static const size_type CAPACITY = 30;
polynomial();
~polynomial( );
// CONSTRUCTOR
polynomial(double c = 0.0, unsigned int exponent = 0);
// MODIFICATION MEMBER FUNCTIONS
void add_to_coef(double amount, unsigned int exponent);
void assign_coef(double coefficient, unsigned int exponent);
void clear( );
// CONSTANT MEMBER FUNCTIONS
double coefficient(unsigned int exponent) const;
unsigned int degree( ) const { return current_degree; }
//polynomial derivative( ) const;
double eval(double x) const;
unsigned int next_term(unsigned int e) const;
unsigned int previous_term(unsigned int e) const;
// CONSTANT OPERATORS
double operator( ) (double x) const { return eval(x); }
private:
double coef[CAPACITY];
unsigned int current_degree;
void compute_degree();
};
// NON-MEMBER BINARY OPERATORS
polynomial operator +(const polynomial& p1, const polynomial& p2);
polynomial operator -(const polynomial& p1, const polynomial& p2);
//polynomial operator *(const polynomial& p1, const polynomial& p2);
// NON-MEMBER OUTPUT FUNCTIONS
std::ostream& operator << (std::ostream& out, const polynomial& p);
#endif
Explanation / Answer
main.cpp
#include <cctype> // Provides toupper
#include <iostream> // Provides cout and cin
#include <cstdlib> // Provides EXIT_SUCCESS
#include "poly0.h" // Provides the polynomial class
using namespace std;
using namespace main_namexxx;
const unsigned int MANY = 3; // Number of polynomials allowed in this test program.
// PROTOTYPES for functions used by this test program:
void print_menu();
// Postcondition: The menu has been written to cout.
size_t set_current( );
// Postcondition: Return value is index for a new current polynomial.
char get_command();
// Postcondition: The user has been prompted for a character.
// The entered charatcer will be returned, translated to upper case.
void view(const polynomial& test);
//Postcondition: The polynomial passed has been sent to cout.
void view_all(const polynomial a[]);
//Postcondition: All polynomials has been written to cout.
void test_add(polynomial& test);
// Postcondition: The user has been prompted for a coefficent and degree of
// the term added. The resulting polynomial has been written to cout.
void test_assign(polynomial& test);
// Postcondition: The user has been prompted for the degree and the coeffinient
// to be set. The resulting polynomial has been written to cout.
void test_clear(polynomial& test);
// Postcondition: test.clear( ) has been activated.
// to be set. The resulting polynomial has been written to cout.
void test_eval(const polynomial& test);
// Post conditon: The user has been prompted for the x value. The evaluation
// of the polynomial is written to cout.
void test_np(const polynomial& test);
// Post conditon: The user has been prompted for the e value. The
// value of test.next_term(e) and test.previous_term(e) are written to cout.
int main()
{
polynomial p[MANY];
size_t current_index = 0;
char command;
size_t i;
cout << "Polynomials ";
for (i = 0; i < MANY; ++i)
cout << char('A' + i) << ' ';
cout << "have all been initialized." << endl;
do
{
print_menu();
command = toupper(get_command());
switch(command)
{
case 'S': current_index = set_current( );
break;
case '1': test_assign(p[current_index]);
break;
case '2': test_add(p[current_index]);
break;
case 'C': test_clear(p[current_index]);
break;
case 'V':
cout << char(current_index + 'A') << ": ";
view(p[current_index]);
break;
case 'A': view_all(p);
break;
case 'D':
cout << char(current_index + 'A') << ".derivative: ";
view(p[current_index].derivative( ));
break;
case 'E': test_eval(p[current_index]);
break;
case 'N': test_np(p[current_index]);
break;
case '+':
cout << "A + B: ";
view(p[0] + p[1]);
break;
case '-':
cout << "A - B: ";
view(p[0] - p[1]);
break;
case '*':
cout << "A * B: ";
view(p[0] * p[1]);
break;
case 'Q': // Do nothing..
break;
default: cout << "Invalid command." << endl;
break;
}
}
while(command != 'Q');
return (EXIT_SUCCESS);
}
void print_menu()
{
cout << "----------------- The Commands -----------------" << endl;
cout << "S - set the current Polynomial to work on" << endl;
cout << " - - - - - - - - - - - -" << endl;
cout << "1 - use the assign_coef function" << endl;
cout << "2 - use the add_to_coef function" << endl;
cout << "C - use the clear function" << endl;
cout << "V - view the current polynomial by using <<" << endl;
cout << "A - view all polynomials by using <<" << endl;
cout << "D - view derivative of current polynomial" << endl;
cout << "E - evaluate current polynomial by using () op" << endl;
cout << "N - use the next_term and previous_term functions" << endl;
cout << "+ - view A + B" << endl;
cout << "- - view A - B" << endl;
cout << "* - view A * B" << endl;
cout << " - - - - - - - - - - - -" << endl;
cout << "Q - quit this interactive test program" << endl;
cout << "-------------------------------------------------" << endl;
}
char get_command()
{
char command;
cout << ">";
cin >> command;
return(toupper(command));
}
void view(const polynomial& test)
{
cout << test
<< " (degree is " << test.degree( ) << ")" << endl;
}
size_t set_current( )
{
size_t i;
char command;
do
{
cout << "Polynomials ";
for (i = 0; i < MANY; ++i)
cout << char('A' + i) << ' ';
cout << "." << endl;
cout << "Enter the polynomial you want to work on: ";
command = toupper(get_command());
}
while ((command < 'A') || (command >= char('A' + MANY)));
return command - 'A';
}
void test_add(polynomial& test)
{
double coefficient;
unsigned int exponent;
cout << "Enter exponent: ";
cin >> exponent;
cout << "Enter coefficient: ";
cin >> coefficient;
test.add_to_coef(coefficient, exponent);
cout << "After adding: ";
view(test);
}
void test_assign(polynomial& test)
{
double coefficient;
unsigned int exponent;
cout << "Enter exponent: ";
cin >> exponent;
cout << "Enter coefficient: ";
cin >> coefficient;
test.assign_coef(coefficient, exponent);
cout << "After assigning: ";
view(test);
}
void test_eval(const polynomial& test)
{
double x_value;
cout << "Enter the x value: ";
cin >> x_value;
cout << "For the poly: ";
view(test);
cout << "The evaluation returned is " << test(x_value) << endl;
}
void view_all(const polynomial p[])
{
size_t i;
cout << endl;
for (i = 0; i < MANY; ++i)
{
cout << char(i + 'A') << ": ";
view(p[i]);
}
}
void test_clear(polynomial& test)
{
test.clear( );
cout << "After clearing: ";
view(test);
}
void test_np(const polynomial& test)
{
unsigned int exponent;
cout << "Enter exponent: ";
cin >> exponent;
cout << "For polynomial: ";
view(test);
cout << "next_term(" << exponent << ") = "
<< test.next_term(exponent) << endl;
cout << "previous_term(" << exponent << ") = "
<< test.previous_term(exponent) << endl;
}
poly0.h
// FILE: poly0.h
// CLASS PROVIDED:
// class polynomial (in the namespace main_namexxx)
// A polynomial has one variable x, real number coefficients, and
// non-negative integer exponents. Such a polynomial can be viewed
// as having the form:
// A[n]*x^n + A[n-1]*x^(n-1) + ... A[2]*x^2 + A[1]*x + A[0]
// where the A[n] are the real number coefficients and x^i represents
// the variable x raised to the i power. The coefficient A[0] is
// called the "constant" or "zeroth" term of the polynomial.
//
// NOTES TO STUDENT:
// 1. This version works by storing the coefficients in
// a fixed array. The coefficient for the x^k term is stored
// in location [k] of the fixed-size array. Later we will modify
// the implementation to use a dynamic array.
// 2. Note that two functions have been implemented as inline functions
// in this file (the degree and operator() functions).
//
// MEMBER CONSTANTS
// const static size_t CAPACITY
// The size of the fixed array to store the coefficients.
// const static size_t MAX_EX = CAPACITY - 1;
// The maximum exponent permitted.
//
// CONSTRUCTOR for the polynomial class
// polynomial(double c = 0.0, unsigned int exponent = 0)
// PRECONDITION: exponent <= MAX_EX.
// POSTCONDITION: This polynomial has been create with all zero
// coefficients, except for coefficient c for the specified exponent.
// When used as a default constructor (using default values for
// both arguments), the result is a polynomial with all zero
// coefficients.
//
// MODIFICATION MEMBER FUNCTIONS for the polynomial class
// void add_to_coef(double amount, unsigned int exponent)
// PRECONDITION: exponent <= MAX_EX.
// POSTCONDITION: Adds the given amount to the coefficient of the
// specified exponent.
//
// void assign_coef(double coefficient, unsigned int exponent)
// PRECONDITION: exponent <= MAX_EX.
// POSTCONDITION: Sets the coefficient for the specified exponent.
//
// void clear( )
// POSTCONDITION: All coefficients of this polynomial are set to zero.
//
// CONSTANT MEMBER FUNCTIONS for the polynomial class
// double coefficient(unsigned int exponent) const
// POSTCONDITION: Returns coefficient at specified exponent of this
// polynomial.
// NOTE: for exponents > MAX_EX, the return value is always zero.
//
// unsigned int degree( ) const
// POSTCONDITION: The function returns the value of the largest exponent
// with a non-zero coefficient.
// If all coefficients are zero, then the function returns zero.
//
// polynomial derivative( ) const
// POSTCONDITION: The return value is the first derivative of this
// polynomial.
//
// double eval(double x) const
// POSTCONDITION: The return value is the value of this polynomial with the
// given value for the variable x.
//
// unsigned int next_term(unsigned int e) const
// POSTCONDITION: The return value is the next exponent n which is LARGER
// than e such that coefficient(n) != 0.
// If there is no such term, then the return value is zero.
//
// unsigned int previous_term(unsigned int e) const
// POSTCONDITION: The return value is the next exponent n which is SMALLER
// than e such that coefficient(n) != 0.
// If there is no such term, then the return value is UINT_MAX
// from <climits>.
//
// CONSTANT OPERATORS for the polynomial class
// double operator( ) (double x) const
// Same as the eval member function.
//
// NON-MEMBER BINARY OPERATORS for the polynomial Class
// polynomial operator -(const polynomial& p1, const polynomial& p2)
// POSTCONDITION: return-value is a polynomial with each coefficient
// equal to the difference of the coefficients of p1 & p2 for any given
// exponent.
//
// polynomial operator +(const polynomial& p1, const polynomial& p2)
// POSTCONDITION: return-value is a polynomial with each coefficient
// equal to the sum of the coefficients of p1 & p2 for any given
// exponent.
//
// polynomial operator *(const polynomial& p1, const polynomial& p2)
// PRECONDITION: p1.degree( ) + p2.degree( ) <= polynomial::MAX_EX.
// POSTCONDITION: Each term of p1 has been multiplied by each term of p2,
// and the answer is the sum of all these term-by-term products.
// For example, if p1 is 2x^2 + 3x + 4 and p2 is 5x^2 - 1x + 7, then the
// return value is 10x^4 + 13x^3 + 31x^2 + 17x + 28.
//
// NON-MEMBER OUTPUT FUNCTION for the polynomial Class
// ostream& operator << (ostream& out, const polynomial& p)
// POSTCONDITION: The polynomial has been printed to ostream out, which,
// in turn, has been returned to the calling function.
#ifndef POLY0_H
#define POLY0_H
#include <iostream> // Provides ostream
namespace main_namexxx
{
class polynomial
{
public:
// CONSTANTS
static const unsigned int CAPACITY = 30;
static const unsigned int MAX_EX = CAPACITY - 1;
// CONSTRUCTOR
polynomial(double c = 0.0, unsigned int exponent = 0);
// MODIFICATION MEMBER FUNCTIONS
void add_to_coef(double amount, unsigned int exponent);
void assign_coef(double coefficient, unsigned int exponent);
void clear( );
// CONSTANT MEMBER FUNCTIONS
double coefficient(unsigned int exponent) const;
unsigned int degree( ) const { return current_degree; }
polynomial derivative( ) const;
double eval(double x) const;
unsigned int next_term(unsigned int e) const;
unsigned int previous_term(unsigned int e) const;
// CONSTANT OPERATORS
double operator( ) (double x) const { return eval(x); }
private:
double coef[CAPACITY];
unsigned int current_degree;
};
// NON-MEMBER BINARY OPERATORS
polynomial operator +(const polynomial& p1, const polynomial& p2);
polynomial operator -(const polynomial& p1, const polynomial& p2);
polynomial operator *(const polynomial& p1, const polynomial& p2);
// NON-MEMBER OUTPUT FUNCTION
std::ostream& operator << (std::ostream& out, const polynomial& p);
}
#endif
poly0.cpp
#include <climits> // Provides UINT_MAX
#include <cmath> // Provides fabs
#include <cstdlib> // Provides rand
#include <cstring> // Provides memcpy
#include <iostream> // Provides cout
#include <cassert> // provides assert
#include <algorithm> //provides fill_n
#include <string> // provides string
#include <sstream>
#include "poly0.h" // defines what must be implemented
namespace main_namexxx
{
// NEEDED GLOBALS
const unsigned int polynomial::CAPACITY;
const unsigned int polynomial::MAX_EX;
// CONSTRUCTOR for the polynomial class
// polynomial(double c = 0.0, unsigned int exponent = 0)
// PRECONDITION: exponent <= MAX_EX.
// POSTCONDITION: This polynomial has been create with all zero
// coefficients, except for coefficient c for the specified exponent.
// When used as a default constructor (using default values for
// both arguments), the result is a polynomial with all zero
// coefficients.
//THIS WORKS
polynomial::polynomial (double c, unsigned int exponent){
assert(exponent <= MAX_EX);
clear (); //THE ORDER COUNT FOR CLEAR
current_degree = 0;
assign_coef(c,exponent);
}
//
// MEMBER CONSTANTS
// const static size_t CAPACITY
// The size of the fixed array to store the coefficients.
// const static size_t MAX_EX = CAPACITY - 1;
// The maximum exponent permitted.
// MEMBER CONSTANTS ALREADY CALLED
// MODIFICATION MEMBER FUNCTIONS for the polynomial class
// void add_to_coef(double amount, unsigned int exponent)
// PRECONDITION: exponent <= MAX_EX.
// POSTCONDITION: Adds the given amount to the coefficient of the
// specified exponent.
// THIS WORKS
void polynomial::add_to_coef (double amount, unsigned int exponent){
assert(exponent <= MAX_EX);
assign_coef((amount + coefficient(exponent)), exponent);
}
// void assign_coef(double coefficient, unsigned int exponent)
// PRECONDITION: exponent <= MAX_EX.
// POSTCONDITION: Sets the coefficient for the specified exponent.
// SETS CURRENT_DEGREE AND ALSO SETS COEFFICIENT
//THIS WORKS
void polynomial::assign_coef(double coefficient, unsigned int exponent){
assert(exponent <= MAX_EX);
coef[exponent] = coefficient;
//SETS THE CURRENT_DEGREE
if (exponent > current_degree && coefficient != 0)
current_degree = exponent;
if(current_degree == exponent && coefficient == 0 && current_degree != 0){
if(previous_term(current_degree) == UINT_MAX){
current_degree = 0;
}
else{
current_degree = previous_term(current_degree);
}
}
}
// void clear( )
// POSTCONDITION: All coefficients of this polynomial are set to zero.
//PROBABLY CORRECT.(PROBLEM???CORRECT??? ONLY CLEARS CURRENT POLYNOMIAL)
//THIS WORKS ACCORDING TO TEST
void polynomial::clear(){
std::fill_n(coef,CAPACITY,0.0); //FILL ARRAY COEF WITH 0.0 AKA CLEARING IT
current_degree = 0;
}
// CONSTANT MEMBER FUNCTIONS for the polynomial class
// double coefficient(unsigned int exponent) const
// POSTCONDITION: Returns coefficient at specified exponent of this
// polynomial.
// NOTE: for exponents > MAX_EX, the return value is always zero.
// PROBABLY CORRECT. CALLS THE COEFFICIENT UNLESS THE IF ACTIVATED
double polynomial::coefficient(unsigned int exponent) const{
if (exponent > MAX_EX){
return 0.0;
}
else{
return coef[exponent];
}
}
// unsigned int degree( ) const
// POSTCONDITION: The function returns the value of the largest exponent
// with a non-zero coefficient.
// If all coefficients are zero, then the function returns zero.
// THIS DEGREE IS SET IN THE HEADER FILE
// polynomial derivative( ) const
// POSTCONDITION: The return value is the first derivative of this
// polynomial.
//USED POWER RULE. NEEDS A FOR LOOP.
// THIS WORKS.
polynomial polynomial::derivative() const{
polynomial p(0.0,0);
int n = 0;
while ((n = this->next_term(n)) != 0){
double c = coefficient(n);
p.assign_coef(c * (n), n - 1); //POWER RULE. COEFICCIENT TIMES N, AND EXPONENT MINUS 1.
}
return p;
}
// double eval(double x) const
// POSTCONDITION: The return value is the value of this polynomial with the
// given value for the variable x.
//THIS WORKS
double polynomial::eval(double x) const{
double value = 0.0;
int n = 0;
value += coefficient(n); //adds up all the coefficients
while ((n = next_term(n)) != 0){
value += coefficient(n) * pow(x, n); //returns value is the value of the polynomial in c * x ^n form.
}
return value;
}
// unsigned int next_term(unsigned int e) const
// POSTCONDITION: The return value is the next exponent n which is LARGER
// than e such that coefficient(n) != 0.
// If there is no such term, then the return value is zero.
//WORKING NOW
unsigned int polynomial::next_term(unsigned int e) const{
unsigned int n;
if(e < degree()){
for(n = e+1; n <= degree(); n++) // Goes forward until it hits the exponent
if(coefficient(n) != 0.0) // POSTCONDITION
return n;
}
return 0; //if coefficient (n) == 0; it has found the next term
}
// unsigned int previous_term(unsigned int e) const
// POSTCONDITION: The return value is the next exponent n which is SMALLER
// than e such that coefficient(n) != 0.
// If there is no such term, then the return value is UINT_MAX
// from <climits>.
//WORKING NOW
unsigned int polynomial::previous_term (unsigned int e) const{
unsigned int n = e;
if (e == 0){
return UINT_MAX; //UINTMAX IS MAX UNSIGNED INT
}
do{
if( n > degree()){
return degree();
}
n--; // goes backwards 1 term
if(coefficient(n) != 0.0){
return n;
}
}while(n>0);
return UINT_MAX;
}
// CONSTANT OPERATORS for the polynomial class
// double operator( ) (double x) const
// Same as the eval member function.
// NON-MEMBER BINARY OPERATORS for the polynomial Class
// polynomial operator -(const polynomial& p1, const polynomial& p2)
// POSTCONDITION: return-value is a polynomial with each coefficient
// equal to the difference of the coefficients of p1 & p2 for any given
// exponent.
//WORKING
polynomial operator -(const polynomial& p1, const polynomial& p2){
polynomial p(0.0,0);
int n1 = 0,
n2 = 0,
n = 0;
do{ //DOES THE OPERATOR AND FINDS THE DIFFERENCE AND PUTS IT INTO NEW POLYNOMIAL P
double q1 = p1.coefficient(n);
double q2 = p2.coefficient(n);
if (q1 - q2 != 0){
p.assign_coef(q1 - q2, n);
}
n1 = p1.next_term(n);
n = p2.next_term(n);
//Assigns for exponent. Finds the exponent because p1 & p2 passes by reference
if (n1 == 0){
n = n2;
}
else if (n2 == 0){
n = n1;
}
else{
n = n1;
if (n > n2){
n = n2;
}
}
}while (n != 0); //if both the coefficients aren't 0, q-q2 !=0 is another check.
return p;
}
// polynomial operator +(const polynomial& p1, const polynomial& p2)
// POSTCONDITION: return-value is a polynomial with each coefficient
// equal to the sum of the coefficients of p1 & p2 for any given
// exponent.
//WORKING
polynomial operator +(const polynomial& p1, const polynomial& p2){
polynomial p(0.0,0);
int n1 = 0,
n2 = 0,
n = 0;
do{
double q1 = p1.coefficient(n);
double q2 = p2.coefficient(n);
if (q1 + q2 != 0){
p.assign_coef(q1 + q2, n); //SUMS OPERATOR AND SETS AS NEW P.
}
n1 = p1.next_term(n);
n2 = p2.next_term(n);
// LOOKS FOR THE COEFFICIENT
if (n1 == 0){
n = n2;
}
else if (n2 == 0){
n = n1;
}
else{
n = n1;
if (n > n2){
n = n2;
}
}
}while (n != 0);
return p;
}
// polynomial operator *(const polynomial& p1, const polynomial& p2)
// PRECONDITION: p1.degree( ) + p2.degree( ) <= polynomial::MAX_EX.
// POSTCONDITION: Each term of p1 has been multiplied by each term of p2,
// and the answer is the sum of all these term-by-term products.
// For example, if p1 is 2x^2 + 3x + 4 and p2 is 5x^2 - 1x + 7, then the
// return value is 10x^4 + 13x^3 + 31x^2 + 17x + 28.
//WORKING
polynomial operator *(const polynomial& p1, const polynomial&p2){
assert(p1.degree() + p2.degree() <= polynomial::MAX_EX);
polynomial p(0.0, 0);
int n1 = 0;
do{
double q1 = p1.coefficient(n1); //GRABS P1 COEFFICIENT
int n2 = 0;
do{
double q2 = p2.coefficient(n2); // GRABS P2 COEFFICIENT
double result = q1 * q2; // MULTIPLIES COEFFICIENTS
p.add_to_coef(result, n1 + n2); // COEFFICIENT SET
n2 = p2.next_term(n2); //GOES TO NEXT TERM
}while(n2 != 0);
n1 = p1.next_term(n1); //GOES TO NEXT TERM
}while(n1 != 0);
return p;
}
// NON-MEMBER OUTPUT FUNCTION for the polynomial Class
// ostream& operator << (ostream& out, const polynomial& p)
// POSTCONDITION: The polynomial has been printed to ostream out, which,
// in turn, has been returned to the calling function
//WORKING
std::ostream& operator << (std::ostream& out, const polynomial& p){
unsigned int i = p.degree( );
double number;
// Each iteration of this loop prints one term of the polynomial:
do
{
// Get the coefficient:
number = p.coefficient(i);
// Print a sign
/* ...there are three possibilities:
"-" in front of the first term if it is negative
" - " in front of other negative terms
" + " in front of positive terms (except if it is first term)
*/
if(i == p.degree() && number<0)
out << "-";
else if(number < 0)
out << " - ";
else if(number > 0 && i != p.degree())
out << " + ";
// Get rid of any negative sign in the number:
number = fabs(number);
// Print the number, variable x, and exponent
/* ...print the number only when it is not 1.0 or it is the
constant term
...print the letter x only when the exponent is above zero
...print the exponent only when it is above one
*/
if(number != 1.0 || i == 0){
out << number;
}
if(i > 1){
out << "x^" << i;
}
if(i == 1){
out << "x";
}
// Move to the next lowest term:
i = p.previous_term(i);
} while (i != UINT_MAX);
return out << ' '; //return the output stream
}
}
sample output
Polynomials A B C have all been initialized.
----------------- The Commands -----------------
S - set the current Polynomial to work on
- - - - - - - - - - - -
1 - use the assign_coef function
2 - use the add_to_coef function
C - use the clear function
V - view the current polynomial by using <<
A - view all polynomials by using <<
D - view derivative of current polynomial
E - evaluate current polynomial by using () op
N - use the next_term and previous_term functions
+ - view A + B
- - view A - B
* - view A * B
- - - - - - - - - - - -
Q - quit this interactive test program
-------------------------------------------------
>1
Enter exponent: 2
Enter coefficient: 2
After assigning: 2x^2
(degree is 2)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.