Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ Program having Polynomial.h, Polynomial.cpp and Main.cpp Implementation A po

ID: 3883228 • Letter: C

Question

C++ Program having Polynomial.h, Polynomial.cpp and Main.cpp

Implementation
A polynomial can be represented by a collection of coefficient/exponent pairs. For example, the above
polynomial can be represented by the following collection of number pairs:
(4,5) (7,3) (-1,2) (9,0)

One way to do this would be to use an array of structures. You may assume a limit of 10 terms in the
equation. Also, all exponents will be positive and integers. All coefficients will be integers.

Methods
You will need to implement the following methods:
void addTerm(int coefficient, int exponent) – add the specified term to the
polynomial. Used repeatedly to construct the polynomial.
int degree() - return the degree of the polynomial – that is, the highest exponent.
int coefficient(int power) – return the coefficient of the term with the given power, or 0
if there is no such term.
int numberOfTerms() - return the number of terms in the polynomial.
int value(int x) – return the value of the polynomial for the given value of x.


Main Program
• Your main program should read pairs of numbers from a file and create a Polynomial by
repeatedly calling the addTerm() method
• You will run your program 2 times, using 2 different input files:

For each execution, output the following:

Degree: d
# terms: t
coeff(0): c
coeff(1): c
:
:
coeff(7): c
value(1): v

Explanation / Answer

Poly.h

#ifndef POLY_H
#define POLY_H

#include <iostream>
using std::ostream;
using std::istream;


class Poly
{

public:
Poly();
Poly( int degrees, const double* llist );
Poly( const Poly& );
~Poly();

int setDeg(int degrees);
int getDeg() const;   

const Poly& operators=( const Poly& llist);
bool operators==( const Poly& llist) const;


void dispPoly( int degrees, const double* llist );


double operators[]( int indices ) const;
double& operators[]( int indices );

private:
int deg;
double *coeff;


};

ostream& operators<<( ostream& , const Poly& );
istream& operators>>( istream& , Poly& );

#endif

Poly.cpp

#include <iostream>
using std::istream;
using std::ostream;

using std::cerr;
using std::endl;

#include <cstdlib>
using std::exit;

#include "Poly.h"


Poly::Poly()
{
deg = 0;
coeff = 0;

}


Poly::Poly(int degrees, const double* llist)
{
getDeg();

}

int Poly::getDeg() const
{
return deg;
}

Poly:: ~Poly()
{
setDeg(0);
}

int Poly::setDeg(int degrees)
{

if( degrees <0 ) {
cerr << "Error: attempted to set a negative deg" << endl;
exit(1);
} else {
if( coeff!=0 ) {
delete [] coeff;
coeff = 0;
deg = 0;
}
if( degrees !=0 ) {
coeff = new double [degrees];
if( coeff==0 ) {
deg =0;
cerr << "Warning: unable to allocate enough space for list" << endl;
exit(2);
} else {
deg=degrees;
}
}
}
return degrees;


}

Poly::Poly(const Poly& llist) : deg(0), coeff(0)
{
if( llist.getDeg()<=0 ) {
setDeg(0);
} else {
setDeg(llist.getDeg());
for (int uu=0; uu< deg ; uu++) {
coeff[uu]=llist.coeff[uu];
}
}
}


const Poly& Poly :: operators=(const Poly& llist)
{
if ( &llist==this )
{
cerr << "Warning: attempt to copy Poly onto self" << endl;
}
else {
if( llist.getDeg()<=0 ) {
setDeg(0);
} else {
setDeg(llist.getDeg());
for (int uu=0; uu< deg ; uu++) {
coeff[uu] = llist.coeff[uu];
}
}
}
return *this;
}

bool Poly::operators==(const Poly& llist) const
{
bool res=true;
if( deg!=llist.getDeg() ) {
res=false;
} else {
for(int uu=0; uu< deg && res == true; uu++) {
res = coeff[uu]==llist.coeff[uu];
}
}
return res;
}


double Poly::operators[]( int indices ) const
{
if( indices<0 || indices>= deg ) {
cerr << "Attempt to access element outside indices bounds"
<< endl;
exit(3);
} else {
return coeff[indices];
}
}


double& Poly::operators[]( int indices )
{
if( indices < 0 || indices>= deg ) {
cerr << "Attempt to access element outside indices bounds"
<< endl;
exit(4);
} else {
return coeff[indices];
}
}

ostream& operators<<( ostream& lt, const Poly& rt)
{
if( rt.getDeg() > 0 ) {
lt << endl;
for (int uu=0; uu< rt.getDeg(); uu++ ) {
for (int qq = 0; qq >= 0 ; qq -- ){


lt << rt[uu] << "X^(" << qq << ") " ;
if (qq > 0)
{   
lt << " + ";
}

}
}
}
else {
cerr << "Warning: Attempt to display empty List" << endl;
}


return lt;
}


istream& operators>>( istream& lt, Poly& rt)
{
int tempo;
lt >> tempo;
if( tempo<=0 ) {
cerr << "First value expected to be list size, > 0." << endl;
} else {
if( tempo!=rt.getDeg() ) {
rt.setDeg(tempo);
}
for (int uu=0; uu<rt.getDeg(); uu++) {
lt >> rt[uu];
}
}
return lt;
}

Main.cpp

#include <iostream>
using std::cout;
using std::endl;
using std::cin;

#include "Poly.h"


int main()
{
Poly ARR;
cout << "(1) Testing `cout << ARR': " << ARR << endl;

cout << "(2) Testing `cin >> ARR': ";
cout << "Enter the polynomial (integer order then double coeff): ";
cin >> ARR;
cout << endl;
cout << "(3) Second look at ARR: " << ARR << endl;

Poly B(ARR);
cout << "(4) Testing `Poly B(ARR)': " << B << endl;

double llist[]={8, 4.5, 1};
Poly C(2, llist);
cout << "(5) Testing `Poly C(2, llist)': " << C << endl;

Poly D=C;
cout << "(6) Testing D = C): " << D << endl;

cout << "(7) Testing ARR == B : " << (ARR==B ? "TRUE" : "FALSE") << endl;
cout << "(8) Testing ARR == D : " << (ARR==D ? "TRUE" : "FALSE")<< endl;

return 0;
}

Rate an upvote......Thankyou

Hope this helps....

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote