/**********************************************************************
 * I expect you to add appropriate documentation to this code.
 **********************************************************************/


#include <iostream.h>
#include "polynomial.h"
#include "list.h"


// Ensure that L's copy constructor is called.

Polynomial::Polynomial(const Polynomial& b)
: degree(b.degree), L(b.L)
{
}


const Polynomial& Polynomial::operator=(const Polynomial& b)
{
   L = b.L;
   degree = b.degree;
   return *this;
}



int Polynomial::SetCoefficient(int exp, double co)
{
   int i;
   double dummy;

   L.Delete(exp);

   if (exp == degree && co == 0.0)
   {
      for (i = degree - 1; i >= 0; --i)
         if (L.Retrieve(i, dummy))
         {
            degree = i;
            return 1;
         }

      degree = -1;
      return 1;
   }
   else if (co == 0.0)
      return 1;
   else
   {
      if (exp > degree)
         degree = exp;

      return L.Insert(exp, co);
   }
}


// Here's RetrieveCoefficient() so that you can see how the List
// member functions and L are used with the Polynomial class.

int Polynomial::RetrieveCoefficient(int exp, double& co) const
{
   return L.Retrieve(exp, co);
}


// Skeletal code for the addition operator, to get you started.

const Polynomial Polynomial::operator+(const Polynomial& b) const
{
   Polynomial Sum;
   int maxDegree = (Degree() > b.Degree()) ? Degree() : b.Degree();

   // Add code here.

   return Sum;
}
