next up previous contents
Next: Multiplication: Up: Algebraic Operations: Previous: Addition:

Subtraction:

    // ************************************ //
    //        Overloaded Operator -         //
    // ************************************ //
FLOAT_POLY FLOAT_POLY::operator-(const FLOAT_POLY& P)
{
  unsigned int p;
  FLOAT_POLY *RESULT;
  FLOAT s;
   
  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 subtract nothing, so return *this
    return(*this);
    }
  if(head==NULL && P.head!=NULL){
    //*this is NULL, so return -P
    RESULT = new FLOAT_POLY();
    *RESULT = P;
    FLOAT_POLY_CELL* temp;
    temp = RESULT->head;
    temp->element *= -1.0;
    while(temp->next!=NULL){
      temp->next->element *= -1.0;
      temp = temp->next;
      }
    return(*RESULT);
    }
  if(head!=NULL && P.head!=NULL){
    //both *this and P are non NULL
    RESULT = new FLOAT_POLY();
    *RESULT = *this;
    FLOAT_POLY_CELL* temp;
    temp = P.head;
    s = -1.0*(temp->element);
    RESULT->insert_nosort(s,temp->power);
    while(temp->next!=NULL){
      s = -1.0*(temp->next->element);
      RESULT->insert_nosort(s,temp->next->power);
      temp = temp->next;
      }
    return(*RESULT);
    }
}

    // ************************************ //
    // 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
    RESULT = new FLOAT_POLY();
    return(*RESULT);
    }
  if(head!=NULL && P.head==NULL){
    //we subtract nothing, so return *this
    return(*this);
    }
  if(head==NULL && P.head!=NULL){
    //*this is NULL, so return -P
    *RESULT = P;
    FLOAT_POLY_CELL* temp;
    temp = RESULT->head;
    temp->element *= -1.0;
    while(temp->next!=NULL){
      temp->next->element *= -1.0;
      temp = temp->next;
      }
    return(*RESULT);
    }
  if(head!=NULL && P.head!=NULL){
    //both *this and P are non NULL
    FLOAT_POLY_CELL* temp;
    temp = P.head;
    insert(-1.0*temp->element,temp->power);
    while(temp->next!=NULL){
      insert(-1.0*temp->next->element,temp->next->power);
      temp = temp->next;
      }
    return(*this);
    }
}



Jim Peterson
1999-04-22