DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH PRINT BOOK
 
Complex arithmetic in C++

input and output

Simple input and output can be done using the operators >> (get from) and << (put to). They are declared like this using the facility for overloading function operators:

   ostream& operator<<(ostream&, complex);
   istream& operator>>(istream&, complex&);
When zz is a complex variable cin>>zz reads a pair of numbers from the standard input stream cin into zz. The first number of the pair is interpreted as the real part of the Cartesian representation of a complex number and the second as the imaginary part. The expression cout<<zz writes zz to the standard output stream cout. For example:
   void copy(istream& from, ostream& to)
   {
           complex zz;
           while (from>>zz) to<<zz;
   }
reads a stream of complex numbers like (3.400000,5.000000) and writes them like (3.4,5). The parentheses and comma are mandatory delimiters for input, while white space is optional. A single real number, for example 10e-7 or (123), will be interpreted as a complex with 0 as the imaginary part by operator >>.

A user who does not like the standard implementation of << and >> can provide alternate versions.


Next topic: Cartesian and polar coordinates
Previous topic: Complex variables and data initialization

© 2004 The SCO Group, Inc. All rights reserved.
UnixWare 7 Release 7.1.4 - 27 April 2004