// Matt Kaliszewski
// Polynomial.cpp
#include "Polynomial.h"
#include <math.h>
//////////////////////////////////////////////////////////////////////
polynomial::polynomial()
{
degree=-1;
}
//////////////////////////////////////////////////////////////////////
// Function to take a string polynomial as a paramater, divide it up
// into tokens and finally divide the tokens up and put the coefficients
// in their correct place into the coefficients vector based on their degree
polynomial::polynomial(string p)
{
int size, position, count;
int start, end, n, i;
vector<string> tokens(20);
// Initialize degree to 0
degree=0;
// Get rid of any spaces in the string
while((position=p.find(' '))!=4294967295)
p.replace(position, 1, "");
// Get the size of the string
size=p.size();
coefficients.resize(1);
// Parse the string into tokens
start=0;
count=0;
position=1;
do
{ // Loop through every character of the string, one at a time.
// If the position contains a token ending character, copy the
// values from start to end into a token and increment the token count
if(p[position]=='-' || p[position]=='+' || p[position]=='\0')
{
end=position;
n=end-start;
tokens[count].insert(0, p, start, n);
if(end<size-1)
{
start=end+1;
position=end;
}
count++;
}
position++;
}while(position<size+1);
// Parse the tokens into the polynomial
for(i=0;i<count;i++)
{
position=tokens[i].find('x');
// If the token contains no 'x'
if(position==-1)
{
coefficients[0]=atoi(tokens[i].c_str());
if(0>=degree)
degree=0;
}
else
{
position=tokens[i].find('^');
// If the token contains an 'x, not raised to any power
if(position==-1)
{
if(1>degree)
{
degree=1;
coefficients.resize(degree+1);
}
tokens[i].replace(tokens[i].find('x'), 1, "");
coefficients[1]=atoi(tokens[i].c_str());
}
else // If the token contains an 'x' raised to a certain power
{
if(tokens[i].at(position+1)>degree)
{
degree=atoi(&tokens[i].at(position+1));
coefficients.resize(degree+1);
}
tokens[i].erase(tokens[i].find('x'), position+1);
coefficients[degree]=atoi(tokens[i].c_str());
}
}
}
}
//////////////////////////////////////////////////////////////////////
int polynomial::getDegree()
{
return degree;
}
//////////////////////////////////////////////////////////////////////
void polynomial::setDegree(int d)
{
degree = d;
coefficients.resize(degree+1);
}
//////////////////////////////////////////////////////////////////////
void polynomial::setCoefficient(int value, int power)
{
if(degree<power)
{
degree=power;
coefficients.resize(degree+1);
}
if(coefficients[power]==NULL)
coefficients[power]=value;
else
coefficients[power]+=value;
}
//////////////////////////////////////////////////////////////////////
int polynomial::evaluateForX(int x)
{
int value;
for(int i=0;i<=degree;i++)
{
if(i==0)
value=coefficients[i];
else if(i==1)
value+=(coefficients[i]*x);
else
value+=coefficients[i]*pow(x,i);
}
return value;
}
//////////////////////////////////////////////////////////////////////
polynomial operator*(polynomial poly1, polynomial poly2)
{
polynomial answer;
int i,j;
int mul;
answer.setDegree(poly1.degree+poly2.degree);
// Make sure the polynomials are not empty
if(poly1.degree==-1 || poly2.degree==-1)
{
answer.setDegree(0);
answer.setCoefficient(0,0);
return answer;
}
// Multiply the two polynomials
for(i=0;i<poly1.degree+1;i++)
{
if(poly1.coefficients[i]!=NULL)
for(j=0;j<poly2.degree+1;j++)
{
if(poly2.coefficients[j]!=NULL)
answer.setCoefficient(poly1.coefficients[i]*poly2.coefficients[j], i+j);
}
}
return answer;
}
//////////////////////////////////////////////////////////////////////
ostream &operator<<(ostream &out, polynomial &p)
{
int count=0;
for(int i=0;i<=p.degree;i++)
{
if(p.coefficients[i]!=NULL)
{
if(i<1)
{
out << p.coefficients[i];
count++;
}
else if(i<2)
{
if(p.coefficients[i]>0 && count>0)
out << "+";
out << p.coefficients[i] << "x";
count++;
}
else
{
if(p.coefficients[i]>0 && count>0)
out << "+";
out << p.coefficients[i] << "x^" << i;
count++;
}
}
}
return out;
}
//////////////////////////////////////////////////////////////////////