📄 poly.cpp
字号:
// return type: poly
///////////////////////////////////////////////////////////////////////
poly &poly::operator=(const poly &p) {
int i;
if (&p != this) //self assignment check
{ //only assign if different
if (size != p.size)
{ //size check
try
{
delete[] data; //delete old array
size = p.size; //new size
data = new double[size];//new array
} catch (...)
{
throw (allocFail);
}
}
if (data != NULL) //safety check for wild pointer
{
for (i=0; i<size; i++) //copy coefficients
data[i] = p.data[i];//
}
}
return *this; //original object reference is returned
}
/////////////////////////////////////////////////////////////////////
// friend functions
// allowing built-in types to be left-hand parameters for operators
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
// operator* (double, poly)
//
// provides symmetrical operator* function for double * poly
//
// parameter: double, poly
// return type: poly
/////////////////////////////////////////////////////////////////////
poly operator*(double value, const poly &p)
{
poly ans(p.size);
int i;
for (i=0; i<p.size; i++)
ans.data[i] = p.data[i] * value;//mult coefficients by double
return ans;
}
/////////////////////////////////////////////////////////////////////
// operator+ (double, poly)
//
// provides symmetrical operator+ function for double + poly
//
// parameter: double, poly
// return type: poly
/////////////////////////////////////////////////////////////////////
poly operator+(double value, const poly &p)
{
poly ans(p.size);
p.data[0]= p.data[0] + value;//add to lowest coefficient
ans = p;
return ans;
}
/////////////////////////////////////////////////////////////////////
// operator- (double, poly)
//
// provides symmetrical operator- function for double - poly
//
// parameter: double, poly
// return type: poly
/////////////////////////////////////////////////////////////////////
poly operator-(double value, const poly &p)
{
poly ans(p.size);
p.data[0]= p.data[0] - value;//sub from lowest coefficient
ans = p;
return ans;
}
/////////////////////////////////////////////////////////////////////
// NOTE: Dividing a double by a poly would give negative exponents
// therefore not supported
/////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
// UNARY OPERATORS
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
// operator-
//
// inverse, reuses the multiplication operator i.e. * -1.0
// changes original and returns changed poly
//
// parameters: none
// return type: poly
///////////////////////////////////////////////////////////////////////
poly poly::operator-()
{
*this = *this * -1.0;
return (*this ); //
}
//////////////////////////////////////////////////////////////////////
// operator+
//
// identity, returns poly unchanged
//
// parameters: none
// return type: poly
///////////////////////////////////////////////////////////////////////
poly poly::operator+()
{
return *this; //
}
//////////////////////////////////////////////////////////////////////
// operator[]
// subscript operator, gives read access as p[i] = ith coefficient
//
// parameter: int - coefficient required
// return type: double, value of coefficient
//////////////////////////////////////////////////////////////////////
double &poly::operator[](int term)
{
if (term < 0) return data[0]; //returns first if low
else if (term > size) return data[size];//returns last if high
else return data[term]; //return correct term
}
//////////////////////////////////////////////////////////////////////
// utility functions
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
// proP - promote polynomial
//
// returns new polynomial of higher degree of poly (promotion)
// original poly unchanged - used for aritmatic and calculus functions
//
// parameters: int, value to increase degree by
// return type: poly
//////////////////////////////////////////////////////////////////////
poly poly::proP(int value)
{
int i;
poly ans(size+value); // ans array poly
for (i=0;i<size;i++) // copy over coefficients
ans.data[i+value]=data[i];
for (i=0;i<value;i++) // zero lower values
ans.data[i]=0;
return ans;
}
//////////////////////////////////////////////////////////////////////
// demP - demote polynomial
//
// returns new polynomial of lower degree of poly (demotion)
// original poly unchanged - used for aritmatic and calculus functions
//
// parameters: int, value to decrease degree by
// return type: poly
//////////////////////////////////////////////////////////////////////
poly poly::demP(int value)
{
int i;
poly ans(size-value); // ans arary poly
for (i=0;i<size-value;i++) // copy over coefficients
ans.data[i]=data[i+value]; //
return ans;
}
//////////////////////////////////////////////////////////////////////
// clean
//
// removes leading zero's, reduces size of poly if highest coefs zero,
// if all zeros then set size will remain = 1 and coeff of x^0 is zero
//
// parameters: none
// return type: none
///////////////////////////////////////////////////////////////////////
void poly::clean()
{
int i;
int j = int(size);
for (i=0;i<j-1;i++)
{
if (data[size-1]==0 ) // check if highest coeff is zero
size = size -1; // reduce size
}
}
//////////////////////////////////////////////////////////////////////
// zero - zero all coefficients
//
// parameters: none
// return type: none
//////////////////////////////////////////////////////////////////////
void poly::zero()
{
int i;
for (i=0;i<size;i++)
data[i]=0;
}
//////////////////////////////////////////////////////////////////////
// checkZero - check if zero
//
// checks for a single coef value of zero, when clean function
// preceeds zero check then entire poly is checked for zero
//
// parameters: none
// return type: none
//////////////////////////////////////////////////////////////////////
int poly::checkZero()
{
if ((size==1)&&(data[0]==0))
return 1;
return 0;
}
//////////////////////////////////////////////////////////////////////
// normalise
//
// highest coefficient forced to 1, divide all coeffs by highest coeff
//
// parameters: none
// return type: none
//////////////////////////////////////////////////////////////////////
poly poly::normalise()
{
int i;
poly::clean(); //remove multiple leading zero's
for (i=0;i<size;i++)
{
if (poly::checkZero()!=1)//check for zero
{
data[i]=data[i]/data[size-1];//divide by highest coeff
}
}
return *this;
}
//////////////////////////////////////////////////////////
// differenciate
//
// differenciates polynomial by firstly demoting by one degree,
// and dividing by its old index x^2 => 2x^1 => 2x^1
// iterates through terms
//
// parameters: none
// return type: none
//////////////////////////////////////////////////////////
poly poly::differenciate()
{
int i;
poly::clean();
if (size==1) // if only x^0 term
poly::zero(); //return zero
else
{
*this=poly::demP(1); //demote each term
for (i=0;i<size;i++)
{
data[i]=data[i]*(i+1); //divide each term by old index
}
}
return *this;
}
//////////////////////////////////////////////////////////
// integrate
//
// integrates polynomial by firstly promoting by one degree,
// and dividing by its index 2x^1 => 2x^2 => x^2
// iterates through terms
//
// parameters: none
// return type: none
//////////////////////////////////////////////////////////
poly poly::integrate()
{
int i;
*this=poly::proP(1); //promote poly
for (i=1;i<size;i++)
{
data[i]=data[i]/(i);//divide promoted coefficients
} //by exponent
return *this;
}
//////////////////////////////////////////////////////
// evaluate
//
// returns evaluated polynimial value for given x value
//
// parameters: float - value for 'x'
// return type: float - 'y' value
//////////////////////////////////////////////////////
float poly::evaluate(float xValue)
{
float retValue=0;
int i;
for (i=0;i<size;i++)
{
retValue = retValue + pow(xValue,i);//adds all evaluated terms
}
return retValue;
}
/////////////////////////////////////////////////////////////////////
// print
//
// prints coefficients in 'normal' format, highest first with conext
// sensitive signs
//
// parameters: none
// return type: none
/////////////////////////////////////////////////////////////////////
void poly::print()
{
int i;
poly::clean(); //remove leading zeros
if (poly::checkZero()==1) // if poly is all zero just print zero
cout<<0;
if (data[size-1]!=0) //check not zero and
cout<<data[size-1]; //print highest without leading '+'
if (size>1) //if size>1 print x^
cout<<"x^"<<size-1<<" ";//print highest without lead '+'
for (i=size-2; i>=0; i--) //loop through the remainder
{
if (data[i]!=0) //only print non-zero
{
if (data[i]>0) //only print '+' for positives
cout<<"+";
cout<<data[i];
if (i!=0) //do not print 'x^' for x^0 term
cout<<"x^"<<i<<" ";
}
}
// cout<<endl;
}
/////////////////////////////////////////////////////////////////////
// fullPrint
//
// prints all coefficients for testing purposes
//
// parameters: none
// return type: none
/////////////////////////////////////////////////////////////////////
void poly::fullPrint()
{
int i;
for (i=0; i<size-1; i++)
{
cout << data[i] <<"x^"<<i<<" ";
if (data[i+1]>=0)
cout<<"+";
}
cout << data[size-1]<<"x^"<<i;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -