📄 poly.cpp
字号:
//////////////////////////////////////////////////////////////////////
// poly.cpp: implementation of the poly class.
//
// liam rainford-
// UL student id# 0109789
// 17-Dec-2001 to 6-Jan-2002
//
// dynamic array used to store the polynomial coefficients, including
// non zero ones, size of array set up using 'size' attribute.
// ***NOTE*** coefficients passed to the constructor in array
// are taken in natural form starting with highest coefficient first
//////////////////////////////////////////////////////////////////////
#include <iostream>
#include "poly.h"
#include <cmath>
using namespace std;
//////////////////////////////////////////////////////////////////////
// constructors/destructors
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
// default constructor
//
// creates polynomial of size inSize throws exception allocFail
// of type exception if 'new' fails
//
// parameter: inSize - size of polynomial, i.e.number of
// coefficients, default = 1
//////////////////////////////////////////////////////////////////////
poly::poly(int inSize)
{
try{
size = inSize; //size for dynamic array
data = new double[size]; //set up array, assign address
}
catch (...)
{
throw (allocFail);
}
}
//////////////////////////////////////////////////////////////////////
// constructor
//
// creates polynomial of size inSize and assigns coefficient values
// from array of values input throws exception allocFail
// of type exception if 'new' fails
//
// parameters: inSize, polynomial size and values[] array of
// coefficients ***CAUTION** coefficients passed
// in the array are taken in natural form starting
// with highest coefficeint first
//////////////////////////////////////////////////////////////////////
poly::poly(int inSize, double values[])
{
int i;
try
{
size = inSize; //set up size for array
data = new double[size]; //create dynamic array, assign address
} catch (...)
{
throw (allocFail);
}
if (data != NULL) //check for wild pointer, double check
{
for (i=0; i<size; i++)
data[i] = values[size-1-i]; //assign values allowing for
} //reversed index input
}
//////////////////////////////////////////////////////////////////////
// copy constructor
//
// deep copy needed due to use of pointer to dynamic memory throws
// exception allocFail of type exception if 'new' fails
//////////////////////////////////////////////////////////////////////
poly::poly(poly &orig)
{
int i;
try
{
size = orig.size; //duplicate size
data = new double[size]; //set up new dynamic array, assign address
} catch (...)
{
throw (allocFail);
}
for (i=0; i<size; i++)
data[i] = orig.data[i]; //copy values
}
//////////////////////////////////////////////////////////////////////
// destructor
//
// deallocate dynaic memory, avoid memory leaks
//////////////////////////////////////////////////////////////////////
poly::~poly()
{
delete[] data; // deallocate dynaic memory, avoid memory leaks
}
//////////////////////////////////////////////////////////////////////
// class methods
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
// 'getter' functions or accessor functions
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
// set coefficient
//
// explicitly sets a coefficient value.
//
// parameter: int, double -coefficient to be set,value to be asigned
// return type: none
//////////////////////////////////////////////////////////////////////
void poly::setCoef(int term, double value)
{
if (term >= 0 && term < size) //range check
data[term] = value; //assignment only if in range
} //terminate directly if out of range
//////////////////////////////////////////////////////////////////////
// get coefficient
//
// returns a coefficient value.
//
// parameter: int, coefficient to be returned
// return type: double, the value of requested coefficient
//////////////////////////////////////////////////////////////////////
double poly::getCoef(int term)
{
if (term >= 0 && term < size) //range check
return data[term]; //get value only if in range
else return 0; //out of range
}
//////////////////////////////////////////////////////////////////////
// getSize
//
// returns size, number of terms of poly
//
// parameter: none
// return type: int, the number of poly coefficients
//////////////////////////////////////////////////////////////////////
int poly::getSize()
{
return size; //return size
}
//////////////////////////////////////////////////////////////////////
// binary operators
//
// overloaded binary operators, object of class returned for chaining
// returns poly by value to avoid scope problems second argument is
// declared const to ensures it remains unchanged
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
// operator+
//
// addition operator for poly + poly, size of returned poly set to
// highest of two poly's, coefficients added iteratively up to smallest
// size poly then remaining coefficients from larger poly
//
// parameters: poly
// return type: poly
//////////////////////////////////////////////////////////////////////
poly poly::operator+(const poly &p)
{
int i;
poly ans(p.size); //ans poly same size as p
if (size == p.size) //check for same size
{
for (i=0; i<size; i++) //add coefficients up to size
ans.data[i] = data[i] + p.data[i];
}
else if (size > p.size) //check larger primary
{
ans = ans.proP((size)-(p.size)); //promote up to larger size
for (i=0; i<p.size; i++) //add up as far as size of smallest poly
ans.data[i] = data[i] + p.data[i];
for (i=p.size; i<size; i++) //place residue from larger poly into poly ans
ans.data[i] = data[i];
return ans;
}
else if (size < p.size) //check for larger secondary
{
for (i=0; i<size; i++) //add up to size of smallest poly
ans.data[i] = data[i] + p.data[i];
for (i=size; i<p.size; i++) //place residue from larger poly into poly ans
ans.data[i] = p.data[i];
}
return ans;
}
//////////////////////////////////////////////////////////////////////
// operator*
//
// multiplication operator for poly * poly,
// iteratively multiply one poly by coefficients of other (double)
// and promote by exponent value
//
// parameters: poly
// return type: poly
//////////////////////////////////////////////////////////////////////
poly poly::operator*(const poly &p)
{
double set1[]={0}; //must ensure zero
poly ans(1,set1); //
int i; //
for (i=0;i<p.size;i++) //iterate through all coeffs
{
ans = ans + poly::proP(i) * p.data[i];//promote by coef order
// and mult by coeff, sum iteratively
}
return ans;
}
//////////////////////////////////////////////////////////////////////
// operator/
//
// division operator for poly / poly,
// right hand poly is the divisor, must be smaller or same size
// and left hand poly otherwise just negative exponents, also
// remainder (negative exponents) ignored
//
// parameters: poly
// return type: poly
//////////////////////////////////////////////////////////////////////
poly poly::operator/(const poly &p)
{
poly ans;
ans.zero(); //default size = 1
int i;
if (size>=p.size) //do not divide if divisor larger
{
poly temp = *this; //temp copy to be changed
poly top = *this; //make top copy for calculations
poly bot(p.size); //make bottom (divisor) copy
for (i=0;i<bot.size;i++) //copying p to bot
bot.data[i] = p.data[i];
bot.clean(); //remove leading zero's
ans=ans.proP(top.size - bot.size); //promote to full size
ans.zero(); //zero temp answer
// begin division
for ( i=0 ;i < ans.size ; i++)
{
if (bot.checkZero()!=1) //check for non zero
{
ans.data[ans.size-1-i]=temp.data[temp.size-1-i]/bot.data[bot.size-1];
temp = top - ans * bot; //creates new top
}
}
}
return ans;
}
//////////////////////////////////////////////////////////////////////
// operator+
//
// addition operator for poly + double, adds double to coefficient
// zero
//
// parameters: double
// return type: poly
//////////////////////////////////////////////////////////////////////
poly poly::operator+(double value)
{
poly ans;
ans = *this;
ans.data[0]= data[0]+value;
return ans;
}
//////////////////////////////////////////////////////////////////////
// operator-
//
// subtraction operator for poly - double, subtracts double from
// coefficient zero
//
// parameters: double
// return type: poly
//////////////////////////////////////////////////////////////////////
poly poly::operator-(double value)
{
poly ans;
ans = *this;
ans.data[0]= data[0]-value;
return ans;
}
//////////////////////////////////////////////////////////////////////
// operator-
//
// subtraction operator for poly - poly, reuses addition operator and
// inverse function
//
// parameters: poly
// return type: poly
//////////////////////////////////////////////////////////////////////
poly poly::operator-(const poly &p)
{
poly ans;
ans = p;
- ans; //inverts
ans = ans + *this;
return ans;
}
//////////////////////////////////////////////////////////////////////
// operator*
//
// multiplication operator for poly * double,
// iteratively multiply poly coefficients by double
//
// parameters: double
// return type: poly
//////////////////////////////////////////////////////////////////////
poly poly::operator*(double value)
{
poly ans(size); // create return poly same size as orig
int i;
for (i=0; i<size; i++) //iterate
{
ans.data[i] = data[i] * value; //mult each coefficient
}
return ans; //return new poly
}
//////////////////////////////////////////////////////////////////////
// operator/
//
// division operator for poly / double,
// iteratively divide poly coefficients by double
//
// parameters: double
// return type: poly
//////////////////////////////////////////////////////////////////////
poly poly::operator/ (double value)
{
poly ans(size); // create return poly same size as orig
int i;
for (i=0; i<size; i++) //iterate
{
ans.data[i] = data[i] / value; //mult each coefficient
}
return ans; //return new poly
}
//////////////////////////////////////////////////////////////////////
// operator=
//
// assignemnt operator for class poly, includes check for self assignment
// 'original' object is assigned-to but reference also returned to
// allow chaining
//
// parameters: poly
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -