next up previous contents
Next: The Overloaded Input and Up: The Definition File mycomplex.c: Previous: The nth Roots of

The Usual Private Print and Grab Agents:

Note that the print agent is a bit messy because we are trying to print out the complex number in a pretty format.

//====================================================//
//                                                    //
//                grab agent                          //
//                                                    //
//====================================================//

istream& MYCOMPLEX::grab(istream& in)
{
  in >> real;
  in >> imaginary;
  return(in);
}
//====================================================//
//                                                    //
//                print agent                         //
//                                                    //
//====================================================//
ostream& MYCOMPLEX::print(ostream& output) const
{
  if(fabs(real)> 1.0e-14)
    output << real;
  else
    output << 0.0;
  if(fabs(imaginary)> 1.0e-14){
    if(imaginary > 1.0e-14)
      output << " +" << imaginary << "I ";
    if(imaginary< -1.0e-14)
      output << imaginary << "I ";
    }
  else
    output << "+" << 0.0 << "I ";
  return output;
}



Jim Peterson
1999-04-22