next up previous contents
Next: The Destructor: Up: The Implementation: Previous: The Copy Constructor and

Comparison Operators:

     // *********************************************** //
     // overloading == operator for FLOAT_POLY class    //
     // *********************************************** //
int FLOAT_POLY::operator==(const FLOAT_POLY& B)
{
  int i;
  if (&B != this){
    FLOAT_POLY_CELL *temp,*again;
    if(head==NULL) return(0);
    if(!(B.head==NULL)){
      //use the previous *this until we run out of cells
      if(head->power!=B.head->power) return 0;
      if( (head->element-B.head->element)!=0) return 0;
      temp = head;
      again = B.head->next;
      while(!(again==NULL)){
        if(temp->next==NULL) return 0;
          //B has a cell that this doesn't
        else{
          //both have a cell
          if(again->power!=temp->next->power) return 0;
          if( (again->element-temp->next->element)!=0 ) return 0;
          }
        again = again->next;
        temp = temp->next;
        }//again != NULL loop
      //we have finished checking to see if the values of B
      //match this.  Check to see if the left side object of the A==B
      //still contains more elements.  If so, the objects do not match.
      if(temp!=NULL) return 0;
      else return(1);
      }//B.head != NULL test
    else{//B.head = NULL
      if(this->head==NULL) return 1;
      else return(0);
      }
    }//B != this test
  else{
    return(1);
    }
}

     // *************************************************** //
     // overloading not equal operator for FLOAT_POLY class //
     // *************************************************** //
int FLOAT_POLY::operator!=(const FLOAT_POLY& B)
{
  int i;
  if (&B != this)
    {
    FLOAT_POLY_CELL *temp,*again;
    if(!(B.head==NULL)){
      //we don't care if the name fields match
      //use the previous *this until we run out of cells
      if(head->power!=B.head->power) return 1;
      if( (head->element-B.head->element)==0 ) return 1;
      temp = head;
      again = B.head->next;
      while(!(again==NULL)){
        if(temp->next==NULL) return 1;
          //B has a cell that this doesn't
        else{
          //both have a cell
          if(again->power!=temp->next->power) return 1;
          if( (again->element-temp->next->element)==0 ) return 1;
          }
        again = again->next;
        temp = temp->next;
        }
      //we have finished checking to see if the values of B
      //match this.  Check to see if the left side object of the A==B
      //still contains more elements.  If so, the objects do not match.
      if(temp!=NULL) return 1;
      }
    }
}



Jim Peterson
1999-04-22