next up previous contents
Next: Subtraction: Up: Algebraic Operations: Previous: Algebraic Operations:

Addition:

    // ************************************ //
    // Overloaded Operator +                //
    // ************************************ //
FLOAT_POLY FLOAT_POLY::operator+(const FLOAT_POLY& P)
{
  unsigned int p;
  FLOAT_POLY *RESULT; 
  if(head==NULL && P.head==NULL){
    //both this and the incoming polynomial are degenerate
    //cout << "Both this and incoming polynomial are degenerate" << endl; 
    RESULT = new FLOAT_POLY();
    return(*RESULT);
    }
  if(head!=NULL && P.head==NULL){
    //we add nothing, so return *this
    return(*this);
    }
  if(head==NULL && P.head!=NULL){
    //*this is NULL, so return incoming P
    *RESULT = P;
    return(*RESULT);
    }
  if(head!=NULL && P.head!=NULL){
    RESULT = new FLOAT_POLY();
    *RESULT = *this;
    FLOAT_POLY_CELL* temp;
    for(FLOATPOLYIter next(P);next;++next){
      temp = next();
      RESULT->insert_nosort(temp->element,temp->power);
      }
    //insertions done, so now sort
    RESULT->mergesort();  
    return(*RESULT);
    }
}

    // ************************************ //
    // Overloaded Operator +=               //
    // ************************************ //
FLOAT_POLY& FLOAT_POLY::operator+=(const FLOAT_POLY& P)
{
  unsigned int p;
  if(head==NULL && P.head==NULL){
    //both this and the incoming polynomial are degenerate
    //cout << "Both this and incoming polynomial are degenerate" << endl;
    return(*this);
    }
  if(head!=NULL && P.head==NULL){
    //we add nothing, so return *this
    return(*this);
    }
  if(head==NULL && P.head!=NULL){
    //*this is NULL, so return incoming P
    *this = P;
    return(*this);
    }
  if(head!=NULL && P.head!=NULL){
    //both *this and P are non NULL
    FLOAT_POLY_CELL* temp;
    temp = P.head;
    insert_nosort(temp->element,temp->power);
    while(temp->next!=NULL){
      insert_nosort(temp->next->element,temp->next->power);
      temp = temp->next;
      }
    mergesort();
    return(*this);
    }
}



Jim Peterson
1999-04-22