In: Computer Science
Separate code into .cpp and .h files:
// C++ program to create and implement Poly class representing
Polynomials
#include <iostream>
#include <cmath>
using namespace std;
class Poly
{
private:
int degree;
int *coefficients;
public:
Poly();
Poly(int coeff, int degree);
Poly(int coeff);
Poly(const Poly& p);
~Poly();
void resize(int new_degree);
void setCoefficient(int exp, int coeff);
int getCoefficient(int exp);
void display();
};
// default constructor to create a polynomial with constant
0
Poly::Poly()
{
// set degree to 0
degree = 0;
// create an array of size 1
coefficients = new int[degree+1];
coefficients[0] = 0;
}
// parameterized constructor to create a polynomial with degree
degree whose coefficient is coeff
Poly::Poly(int coeff, int degree)
{
this->degree = degree;
// create an array of size degree+1
coefficients = new int[degree+1];
// loop to set all the entries to 0
for(int i=0;i<degree;i++)
coefficients[i] = 0;
// set coefficient of degree to coeff
coefficients[degree] = coeff;
}
// parameterized constructor to create a polynomial with
constant coeff
Poly::Poly(int coeff)
{
// set degree to 0
degree = 0;
// create an array of size 1
coefficients = new int[degree+1];
// set coefficients of constant to coeff
coefficients[0] = coeff;
}
// copy constructor to create a Polynomial same as p
Poly::Poly(const Poly& p)
{
// set degree
degree = p.degree;
// create a new array of size degree+1
coefficients = new int[degree+1];
// loop to copy the coefficients
for(int i=0;i<=degree;i++)
coefficients[i] = p.coefficients[i];
}
// destructor to release the memory allocated
Poly::~Poly()
{
delete[] coefficients;
}
// function to resize the polynomial to new_degree if new_degree
> degree
void Poly:: resize(int new_degree)
{
if(new_degree > degree) // validate new_degree > degree
{
// create a new temporary array of size new_degree+1
int *temp = new int[new_degree+1];
// loop to copy coefficients to temp
for(int i=0;i<=degree;i++)
temp[i] = coefficients[i];
// loop to set the coefficients entries to 0
for(int i=degree+1; i<=new_degree; i++)
temp[i] = 0;
// release memory of existing array
delete[] coefficients;
// update degree
degree = new_degree;
// set coefficients to point to temp
coefficients = temp;
}
}
// function to set coefficient of exp to coeff
void Poly:: setCoefficient(int exp, int coeff)
{
// validate exp to be between [0,degree]
if(exp >= 0 && exp <= degree)
{
coefficients[exp] = coeff;
}
}
// function to return the coefficient of exp
int Poly::getCoefficient(int exp)
{
// validate exp to be between [0,degree]
if(exp >= 0 && exp <= degree)
return coefficients[exp];
return 0; // invalid exp, return 0
}
// function to display the polynomial
void Poly:: display()
{
bool firstTermDisplayed = false;
// loop from degree to 0
for(int i=degree; i>=0 ; i--)
{
if(coefficients[i] != 0) // ith coefficients is non-zero
{
if(firstTermDisplayed) // first term displayed
{
if(coefficients[i] > 0) // display sign between terms
cout<<" + ";
else
cout<<" - ";
cout<<abs(coefficients[i]); // display absolute value of
coefficient
}
else // first term being displayed
{
cout<<coefficients[i];
firstTermDisplayed = true; // set firstTermDisplayed to true
}
// display the power of x
if(i > 0)
{
if(i == 1)
cout<<"x";
else
cout<<"x^"<<i;
}
}
else if(i == 0 && !firstTermDisplayed) // if polynomial is
0, display the constant 0
cout<<coefficients[i];
}
}
int main()
{
// test the Poly class
Poly A(5, 7), B(2), X;
Poly C(A);
cout<<"A: ";
A.display();
cout<<endl<<"B: ";
B.display();
cout<<endl<<"X: ";
X.display();
cout<<endl<<"C: ";
C.display();
A.setCoefficient(0, -2);
A.setCoefficient(1, 10);
A.setCoefficient(3, -4);
cout<<endl<<"A: ";
A.display();
B.resize(5);
B.setCoefficient(2, 10);
cout<<endl<<"B: ";
B.display();
return 0;
}
//end of program
Separate code into .cpp and .h files:
// C++ program to create and implement Poly class representing Polynomials
Header file code
#include <iostream>
#include <cmath>
using namespace std;
class Poly
{
private:
int degree;
int *coefficients;
public:
Poly();
Poly(int coeff, int degree);
Poly(int coeff);
Poly(const Poly& p);
~Poly();
void resize(int new_degree);
void setCoefficient(int exp, int coeff);
int getCoefficient(int exp);
void display();
};
Cpp files
Separate code into .cpp
/ default constructor to create a polynomial with constant
0
Poly::Poly()
{
// set degree to 0
degree = 0;
// create an array of size 1
coefficients = new int[degree+1];
coefficients[0] = 0;
}
// parameterized constructor to create a polynomial with degree
degree whose coefficient is coeff
Poly::Poly(int coeff, int degree)
{
this->degree = degree;
// create an array of size degree+1
coefficients = new int[degree+1];
// loop to set all the entries to 0
for(int i=0;i<degree;i++)
coefficients[i] = 0;
// set coefficient of degree to coeff
coefficients[degree] = coeff;
}
// parameterized constructor to create a polynomial with
constant coeff
Poly::Poly(int coeff)
{
// set degree to 0
degree = 0;
// create an array of size 1
coefficients = new int[degree+1];
// set coefficients of constant to coeff
coefficients[0] = coeff;
}
// copy constructor to create a Polynomial same as p
Poly::Poly(const Poly& p)
{
// set degree
degree = p.degree;
// create a new array of size degree+1
coefficients = new int[degree+1];
// loop to copy the coefficients
for(int i=0;i<=degree;i++)
coefficients[i] = p.coefficients[i];
}
// destructor to release the memory allocated
Poly::~Poly()
{
delete[] coefficients;
}
// function to resize the polynomial to new_degree if new_degree
> degree
void Poly:: resize(int new_degree)
{
if(new_degree > degree) // validate new_degree > degree
{
// create a new temporary array of size new_degree+1
int *temp = new int[new_degree+1];
// loop to copy coefficients to temp
for(int i=0;i<=degree;i++)
temp[i] = coefficients[i];
// loop to set the coefficients entries to 0
for(int i=degree+1; i<=new_degree; i++)
temp[i] = 0;
// release memory of existing array
delete[] coefficients;
// update degree
degree = new_degree;
// set coefficients to point to temp
coefficients = temp;
}
}
// function to set coefficient of exp to coeff
void Poly:: setCoefficient(int exp, int coeff)
{
// validate exp to be between [0,degree]
if(exp >= 0 && exp <= degree)
{
coefficients[exp] = coeff;
}
}
// function to return the coefficient of exp
int Poly::getCoefficient(int exp)
{
// validate exp to be between [0,degree]
if(exp >= 0 && exp <= degree)
return coefficients[exp];
return 0; // invalid exp, return 0
}
// function to display the polynomial
void Poly:: display()
{
bool firstTermDisplayed = false;
// loop from degree to 0
for(int i=degree; i>=0 ; i--)
{
if(coefficients[i] != 0) // ith coefficients is non-zero
{
if(firstTermDisplayed) // first term displayed
{
if(coefficients[i] > 0) // display sign between terms
cout<<" + ";
else
cout<<" - ";
cout<<abs(coefficients[i]); // display absolute value of
coefficient
}
else // first term being displayed
{
cout<<coefficients[i];
firstTermDisplayed = true; // set firstTermDisplayed to true
}
// display the power of x
if(i > 0)
{
if(i == 1)
cout<<"x";
else
cout<<"x^"<<i;
}
}
else if(i == 0 && !firstTermDisplayed) // if polynomial is
0, display the constant 0
cout<<coefficients[i];
}
}
int main()
{
// test the Poly class
Poly A(5, 7), B(2), X;
Poly C(A);
cout<<"A: ";
A.display();
cout<<endl<<"B: ";
B.display();
cout<<endl<<"X: ";
X.display();
cout<<endl<<"C: ";
C.display();
A.setCoefficient(0, -2);
A.setCoefficient(1, 10);
A.setCoefficient(3, -4);
cout<<endl<<"A: ";
A.display();
B.resize(5);
B.setCoefficient(2, 10);
cout<<endl<<"B: ";
B.display();
return 0;
}
//end of program
So header files and c++ are separated