#include //for exit, EXIT_FAILURE #include //for sqrt and atan2 #include "point.h" using namespace std; const int point::subscript = ostream::xalloc(); ostream& operator<<(ostream& ost, const point& p) { switch (ost.iword(point::subscript)) { case 0: //Cartesian coordinates. return ost << "(" << p.x << ", " << p.y << ")"; case 1: //Polar coordinates. if (p.x == 0.0 && p.y == 0.0) { return ost << "(" << 0.0 << ", " << 0.0 << ")"; } else { return ost << "(" << sqrt(p.x * p.x + p.y * p.y) << ", " << atan2(p.y, p.x) << ")"; } default: cerr << "iword(" << point::subscript << ") == " << ost.iword(point::subscript) << " is neither 0 (Cartesian) nor 1 (polar).\n"; exit(EXIT_FAILURE); } }