In: Computer Science
Hi, I have this code so far and need to modify it so that the output does not print something like 2x^0 but instead will just print 2. Currently it prints 2x^0. I also am having a problem with the output of Polynomial 1 - Polynomial 2. If both coefficient values for the polynomials are equal instead of Polynomial 1 - Polynomial 2 = 0 it outputs nothing. Just Polynomial 1 - Polynomial 2 =
For example if I input (assuming there is only one coefficient for each polynomial)
Coefficient 1 for polynomial 1: 1 1
Coefficient 1 for polynomial 2: 1 1
You get no output instead of 0.
Could you please edit my code so that it shows this, or teach me in detail of how to change this. Thank you.
#include <cmath>
#include <iostream>
using namespace std;
const int DEFAULTPOLY = 10; //Default size of our dynamic coefficient array
//DO NOT modify the class header
class Poly
{
private:
//Data members
int arraySize; //size of array
int *coeff; //dynamic array
public:
Poly();
Poly(int size);
Poly(const Poly& aPoly);
~Poly();
const Poly& operator=(const Poly& aPolt);
void grow(int newSize);
int degree() const;
void setCoeff(int value, int i);
int getCoeff(int i) const;
void negate();
friend Poly operator+(const Poly& aPoly, const Poly& bPoly);
friend Poly operator-(const Poly& aPoly, const Poly& bPoly);
friend bool operator==(const Poly& aPoly, const Poly& bPoly);
friend ostream& operator<<(ostream& out, const Poly &aPoly);
};
//default constructor
Poly::Poly()
{
arraySize = DEFAULTPOLY;
coeff = new int[DEFAULTPOLY];
for(int i=0; i<DEFAULTPOLY; i++)
coeff[i] = 0;
}
//constructor
Poly::Poly(int size)
{
arraySize = size;
coeff = new int[size];
for(int i=0; i<size; i++)
coeff[i] = 0;
}
//copy constructor
Poly::Poly(const Poly& aPoly)
{
arraySize = aPoly.arraySize;
if(coeff){
delete [] coeff;}
coeff = new int[arraySize];
for(int i=0; i<arraySize; i++)
coeff[i] = aPoly.coeff[i];
}
//destructor
Poly::~Poly()
{
delete []coeff;
}
// = operator overloading
const Poly& Poly::operator=(const Poly& aPoly)
{
if(this == &aPoly){
return *this;}
if(coeff){
delete [] coeff;}
arraySize = aPoly.arraySize;
coeff = new int[arraySize];
for(int i=0; i<arraySize; ++i)
{
coeff[i] = aPoly.getCoeff(i);
}
return *this;
}
// function to grow the polynomial
void Poly::grow(int newSize)
{
if(arraySize>=newSize) return;
Poly temp(newSize);
for(int i=arraySize; i<newSize; i++)
temp.coeff[i] = 0;
for(int i=0; i<arraySize; i++)
temp.coeff[i] = coeff[i];
delete []coeff;
coeff = temp.coeff;
}
// function to return the degree of the polynomial
int Poly::degree() const
{
int i;
for(i=DEFAULTPOLY-1; coeff[i]==0; i--);
return i;
}
// function to set the coefficient of the polynomial
void Poly::setCoeff(int value, int i)
{
if(i>arraySize) grow(i);
coeff[i] = value;
}
// function to return coefficient of the polynomial
int Poly::getCoeff(int i) const
{
if(i>=arraySize) return 0;
return coeff[i];
}
// function to negate the polynomial
void Poly::negate()
{
for(int i=0; i<arraySize; i++)
coeff[i] = -coeff[i];
}
// + operator overloading
Poly operator+(const Poly& aPoly, const Poly& bPoly)
{
Poly temp;
int i;
for(i=0; i<DEFAULTPOLY-1; i++)
temp.coeff[i] = aPoly.coeff[i] + bPoly.coeff[i];
return temp;
}
// - operator overloading
Poly operator-(const Poly& aPoly, const Poly& bPoly)
{
Poly temp;
int i;
for(i=0; i<DEFAULTPOLY-1; i++)
temp.coeff[i] = aPoly.coeff[i] - bPoly.coeff[i];
return temp;
}
// == operator overloading
bool operator==(const Poly& aPoly, const Poly& bPoly)
{
int i;
for(i=0; i<DEFAULTPOLY; i++)
if(aPoly.coeff[i]!=bPoly.coeff[i]) return false;
return true;
}
// << operator overloading
ostream& operator<<(ostream& out, const Poly
&aPoly)
{
for(int i=aPoly.degree(); i>=0; i--)
{
if(aPoly.coeff[i]==0) continue;
if(i==aPoly.degree() || aPoly.coeff[i]<0)
out<<aPoly.coeff[i]<<"X^"<<i<<" ";
else
out<<"+ "<<aPoly.coeff[i]<<"X^"<<i<<"
";
}
out<<endl;
return out;
}
/////////////////////////////////////////////////////////////////////////////////////////
int main() {
int numCoeff, coeffValue, coeffDegree, x;
Poly poly1, poly2;
//prompt user for number of coefficients
cout << "How many coefficients for polynomial 1?" << endl;
cin >> numCoeff;
for(int i=0; i<numCoeff; ++i)
{
cout << "Coefficient " << i+1 << " for polynomial 1:";
cin >> coeffValue >> coeffDegree;
poly1.setCoeff(coeffValue, coeffDegree);
}
cout << endl << "How many coefficients for polynomial
2?" << endl;
cin >> numCoeff;
for(int i=0; i<numCoeff; ++i)
{
cout << "Coefficient " << i+1 << " for polynomial 2:";
cin >> coeffValue >> coeffDegree;
poly2.setCoeff(coeffValue, coeffDegree);
}
//sample test cases for degree and operator <<
cout << endl << "Polynomial 1 = " << poly1 << endl;
cout << "Polynomial 1 has degree " << poly1.degree() << endl;
cout << "Polynomial 2 = " << poly2 << endl;
cout << "Polynomial 2 has degree " << poly2.degree() << endl;
//Sample test cases for operator+ and operator-
cout << endl << "Polynomial 1 + Polynomial 2 = " << poly1 + poly2 << endl;
cout << "Polynomial 1 - Polynomial 2 = " << poly1 - poly2 << endl << endl;
//Sample test cases for operator ==
if(poly1 == poly2)
{
cout << "Two polynomials are the same." << endl;
}
else
{
cout << "Two polynomials are different." << endl;
}
return 0;
}
// << operator overloading
ostream& operator<<(ostream& out, const Poly
&aPoly)
{
bool check=false;
for(int i=aPoly.degree(); i>=1; i--)
{
if(aPoly.coeff[i]==0) continue;
if(i==aPoly.degree() || aPoly.coeff[i]<0)
out<<aPoly.coeff[i]<<"X^"<<i<<" ";
else
out<<"+ "<<aPoly.coeff[i]<<"X^"<<i<<"
";
check=true;
}
if(check==false||aPoly.coeff[0]!=0)
{
if(check==true)
out<<"+";
out<<aPoly.coeff[0];
}
out<<endl;
return out;
}
As you were printing the x^0 with its coefficient within the loop,instead i took it separately.
And i used a check flag to see if the degree of polynomial1 - polynomial2 was zero or non zero.