I wrote the following: change it to having a header, main and cpp file.. THERE ARE ERRORS
PLEASE FIX.. Picture of output is given below
The internal representation of a Polynomial is an array of terms. Each term contains a coefficient
and an exponent, e.g., the term
2x4
has the coefficient 2 and the exponent 4.
Develop a complete class containing proper constructor and destructor functions as well as set,
get, and print functions. The class should also provide the following overloaded operator
capabilities:
a) Overload the addition operator (+) to add two Polynomials.
b) Overload the subtraction operator (-) to subtract two Polynomials.
c) Overload the assignment operator to assign one Polynomial to another.
d) Overload the multiplication operator (*) to multiply two Polynomials.
e) Overload the addition assignment operator (+=), subtraction assignment operator (-=), and
multiplication assignment operator (*=).
Write an application that tests all the functionality provided by class Polynomial:
• create three Polynomials
• add two Polynomials, using + and += operators
• subtract two Polynomials, using – and -= operators
• assign one Polynomial to another Polynomial
• multiply two Polynomials, using * and *= operators.
---------------------------------------------------------------------------------------------
#include
#include
using namespace std;
using std::setiosflags;
using std::resetiosflags;
class Polynomial
{
public:
Polynomial();
Polynomial operator+( const Polynomial& ) const;
Polynomial operator-( const Polynomial& ) const;
Polynomial operator*( const Polynomial& );
const Polynomial operator=( const Polynomial&);
Polynomial& operator+=( const Polynomial& );
Polynomial& operator-=( const Polynomial& );
void enterTerms( void );
void printPolynomial( void ) const;
private:
int exponents[ 100 ];
int coefficients[ 100 ];
};
Polynomial::Polynomial()
{
for ( int t = 0; t < 100; ++t ) {
coefficients[ t ] = 0;
exponents[ t ] = 0;
}
}
void Polynomial::printPolynomial( void ) const
{
int start;
bool zero = false;
if ( coefficients[ 0 ] ) { // output constants
cout << coefficients[ 0 ];
start = 1;
zero = true; // at least one term exists
}
else {
if ( coefficients[ 1 ] ) {
cout << coefficients[ 1 ] << \'x\'; // constant does not exist
// so output first term
// without a sign
if ( ( exponents[ 1 ] != 0 ) && ( exponents[ 1 ] != 1 ) )
cout << \'^\' << exponents[ 1 ];
zero = true; // at least one term exists
}
start = 2;
}
// output remaining polynomial terms
for ( int x = start; x < 100; ++x ) {
if ( coefficients[ x ] != 0 ) {
cout << setiosflags( ios::showpos ) << coefficients[ x ]
<< resetiosflags( ios::showpos ) << \'x\';
if ( ( exponents[ x ] != 0 ) && ( exponents[ x ] != 1 ) )
cout << \'^\' << exponents[ x ];
zero = true; // at least one term exists
}
}
if ( !zero ) // no terms exist in the polynomial
cout << \'0\';
cout << endl;
}
const Polynomial Polynomial::operator=( const Polynomial& r )
{
exponents[ 0 ] = r.exponents[ 0 ];
coefficients[.
I wrote the following change it to having a header, main and cpp fi.pdf
1. I wrote the following: change it to having a header, main and cpp file.. THERE ARE ERRORS
PLEASE FIX.. Picture of output is given below
The internal representation of a Polynomial is an array of terms. Each term contains a coefficient
and an exponent, e.g., the term
2x4
has the coefficient 2 and the exponent 4.
Develop a complete class containing proper constructor and destructor functions as well as set,
get, and print functions. The class should also provide the following overloaded operator
capabilities:
a) Overload the addition operator (+) to add two Polynomials.
b) Overload the subtraction operator (-) to subtract two Polynomials.
c) Overload the assignment operator to assign one Polynomial to another.
d) Overload the multiplication operator (*) to multiply two Polynomials.
e) Overload the addition assignment operator (+=), subtraction assignment operator (-=), and
multiplication assignment operator (*=).
Write an application that tests all the functionality provided by class Polynomial:
• create three Polynomials
• add two Polynomials, using + and += operators
• subtract two Polynomials, using – and -= operators
• assign one Polynomial to another Polynomial
• multiply two Polynomials, using * and *= operators.
---------------------------------------------------------------------------------------------
#include
#include
using namespace std;
using std::setiosflags;
using std::resetiosflags;
class Polynomial
{
public:
Polynomial();
Polynomial operator+( const Polynomial& ) const;
Polynomial operator-( const Polynomial& ) const;
Polynomial operator*( const Polynomial& );
2. const Polynomial operator=( const Polynomial&);
Polynomial& operator+=( const Polynomial& );
Polynomial& operator-=( const Polynomial& );
void enterTerms( void );
void printPolynomial( void ) const;
private:
int exponents[ 100 ];
int coefficients[ 100 ];
};
Polynomial::Polynomial()
{
for ( int t = 0; t < 100; ++t ) {
coefficients[ t ] = 0;
exponents[ t ] = 0;
}
}
void Polynomial::printPolynomial( void ) const
{
int start;
bool zero = false;
if ( coefficients[ 0 ] ) { // output constants
cout << coefficients[ 0 ];
start = 1;
zero = true; // at least one term exists
}
else {
if ( coefficients[ 1 ] ) {
cout << coefficients[ 1 ] << 'x'; // constant does not exist
// so output first term
// without a sign
if ( ( exponents[ 1 ] != 0 ) && ( exponents[ 1 ] != 1 ) )
cout << '^' << exponents[ 1 ];
zero = true; // at least one term exists
}
start = 2;
}
3. // output remaining polynomial terms
for ( int x = start; x < 100; ++x ) {
if ( coefficients[ x ] != 0 ) {
cout << setiosflags( ios::showpos ) << coefficients[ x ]
<< resetiosflags( ios::showpos ) << 'x';
if ( ( exponents[ x ] != 0 ) && ( exponents[ x ] != 1 ) )
cout << '^' << exponents[ x ];
zero = true; // at least one term exists
}
}
if ( !zero ) // no terms exist in the polynomial
cout << '0';
cout << endl;
}
const Polynomial Polynomial::operator=( const Polynomial& r )
{
exponents[ 0 ] = r.exponents[ 0 ];
coefficients[ 0 ] = r.coefficients[ 0 ];
for ( int s = 1; ( s < 100 ); ++s ) {
if ( r.exponents[ s ] != 0 ) {
exponents[ s ] = r.exponents[ s ];
coefficients[ s ] = r.coefficients[ s ];
}
else {
if ( exponents[ s ] == 0 )
break;
exponents[ s ] = 0;
coefficients[ s ] = 0;
}
}
return *this;
}
Polynomial Polynomial::operator+( const Polynomial& r ) const
{
Polynomial temp;
bool exponentExists;
4. // process element with a zero exponent
temp.coefficients[ 0 ] = coefficients[ 0 ] + r.coefficients[ 0 ];
for ( int s = 1; ( s < 100 ) && ( r.exponents[ s ] != 0 ); ++s ) {
temp.coefficients[ s ] = r.coefficients[ s ];
temp.exponents[ s ] = r.exponents[ s ];
}
for ( int x = 1; x < 100; ++x ) {
exponentExists = false; // assume exponent will not be found
for ( int t = 1; ( t < 100 ) && ( !exponentExists ); ++t )
if ( exponents[ x ] == temp.exponents[ t ] ) {
temp.coefficients[ t ] += coefficients[ x ];
exponentExists = true; // exponent found
}
// exponent was not found, insert into temp
if ( !exponentExists ) {
temp.exponents[ s ] = exponents[ x ];
temp.coefficients[ s ] += coefficients[ x ];
++s;
}
}
return temp;
}
Polynomial &Polynomial::operator+=( const Polynomial &r )
{
*this = *this + r;
return *this;
}
Polynomial Polynomial::operator-( const Polynomial& r ) const
{
Polynomial temp;
bool exponentExists;
// process element with a zero exponent
temp.coefficients[ 0 ] = coefficients[ 0 ] - r.coefficients[ 0 ];
// copy left arrays into temp object s will be used to keep
// track of first open coefficient element
for ( int s = 1; ( s < 100 ) && ( exponents[ s ] != 0 ); ++s ) {
5. temp.coefficients[ s ] = coefficients[ s ];
temp.exponents[ s ] = exponents[ s ];
}
for ( int x = 1; x < 100; ++x) {
exponentExists = false; // assume exponent will not be found
for ( int t = 1; ( t < 100 ) && ( !exponentExists ); ++t )
if ( r.exponents[ x ] == temp.exponents[ t ] ) {
temp.coefficients[ t ] -= r.coefficients[ x ];
exponentExists = true; // exponent found
}
// exponent was not found, insert into temp
if ( !exponentExists ) {
temp.exponents[ s ] = r.exponents[ x ];
temp.coefficients[ s ] -= r.coefficients[ x ];
++s;
}
}
return temp;
}
Polynomial &Polynomial::operator-=( const Polynomial& r )
{
*this = *this - r;
return *this;
}
Polynomial Polynomial::operator*( const Polynomial& r )
{
Polynomial temp;
int s = 1; // subscript location for temp coefficients and exponents
for ( int x = 0; ( x < 100 ) && ( x == 0 || coefficients[ x ] != 0 ); ++x )
for ( int y = 0; ( y < 100 ) && ( y == 0 || r.coefficients[ y ] != 0 ); ++y )
if ( coefficients[ x ] * r.coefficients[ y ] )
if ( ( exponents[ x ] == 0 ) && ( r.exponents[ y ] == 0 ) )
temp.coefficients[ 0 ] += coefficients[ x ] * r.coefficients[ y ];
else {
temp.coefficients[ s ] = coefficients[ x ] * r.coefficients[ y ];
temp.exponents[ s ] = exponents[ x ] + r.exponents[ y ];
6. ++s;
}
polynomialCombine( temp ); // combine common terms
return temp;
}
void Polynomial::polynomialCombine( Polynomial& w )
{
Polynomial temp = w;
int exp;
// zero out elements of w
for ( int x = 0; x < 100; ++x ) {
w.coefficients[ x ] = 0;
w.exponents[ x ] = 0;
}
for ( x = 1; x < 100; ++x ) {
exp = temp.exponents[ x ];
for ( int y = x + 1; y < 100; ++y )
if ( exp == temp.exponents[ y ] ) {
temp.coefficients[ x ] += temp.coefficients[ y ];
temp.exponents[ y ] = 0;
temp.coefficients[ y ] = 0;
}
}
w = temp;
}
Polynomial &Polynomial::operator*=( const Polynomial& r )
{
*this = *this * r;
return *this;
}
void Polynomial::enterTerms( void )
{
bool found = false;
int numberOfTerms, c, e;
cout << " Enter number of polynomial terms: ";
cin >> numberOfTerms;
7. for ( int n = 1; n <= numberOfTerms; ++n ) {
cout << " Enter coefficient: ";
cin >> c;
cout << "Enter exponent: ";
cin >> e;
if ( c != 0 ) {
// exponents of zero are forced into first element
if ( e == 0 ) {
coefficients[ 0 ] += c;
continue;
}
for ( int term = 1; ( term < 100 ) &&
( coefficients[ term ] != 0 ); ++term )
if ( e == exponents[ term ] ) {
coefficients[ term ] += c;
exponents[ term ] = e;
found = true; // existing exponent updated
}
if ( !found ) { // add term
coefficients[ term ] += c;
exponents[ term ] = e;
}
}
}
}
int main()
{
Polynomial a, b, c, t;
a.enterTerms();
b.enterTerms();
t = a; //save the value of a
cout << "First polynomial is: ";
a.printPolynomial();
cout << "Second polynomial is: ";
b.printPolynomial();
cout << " Adding the polynomials yields: ";
8. c = a + b;
c.printPolynomial();
cout << " += the polynomials yields: ";
a += b;
a.printPolynomial();
cout << " Subtracting the polynomials yields: ";
a = t; // reset a to original value
c = a - b;
c.printPolynomial();
cout << " -= the polynomials yields: ";
a -= b;
a.printPolynomial();
cout << " Multiplying the polynomials yields: ";
a = t; // reset a to original value
c = a * b;
c.printPolynomial();
cout << " *= the polynomials yields: ";
a *= b;
a.printPolynomial();
cout << endl;
system("pause");
return 0;
}
Solution
Here is the exact code and let me know if any errors occurs
#include
#include
#include
struct Term
{
int coeff;
unsigned exp;
Term(int c, unsigned e) : coeff(c), exp(e) {};
};
9. class Polynomial
{
public:
Polynomial() = default;
Polynomial(const Polynomial&) = default;
Polynomial(const std::vector& c) : coeff(c) {};
Polynomial(const Term t);
Polynomial(const std::vector& t);
std::string print() const;
Polynomial& operator=(const Polynomial& other) = default;
Polynomial& operator-=(const Polynomial& other);
Polynomial& operator+=(const Polynomial& other);
private:
std::vector coeff;
};
Polynomial::Polynomial(const Term t) : coeff(t.exp + 1)
{
coeff[t.exp] = t.coeff;
}
Polynomial::Polynomial(const std::vector& t)
{
for(auto term: t)
*this += term;
}
std::string Polynomial::print() const
{
std::string temp;
for(signed long long i = coeff.size() - 1; i >= 0; --i) {
if (coeff[i] == 0)
continue;
if (temp != "")
temp += " + ";
if (coeff[i] != 1 || i == 0)
temp += std::to_string(coeff[i]);
if (i != 0)
temp += "x^" + std::to_string(i);