RTXI  2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
complex.cpp
Go to the documentation of this file.
1 //
2 // file = complex.cpp
3 //
4 
5 #include <iostream>
6 #include <math.h>
7 
8 #include "complex.h"
9 
10 std::ostream&
11 operator<<(std::ostream& s, const complex& z)
12 {
13  s << "(" << real(z) << "," << imag(z) << ")";
14  return s;
15 }
16 
17 std::istream&
18 operator>>(std::istream& s, complex& z)
19 {
20  char lpar, rpar, comma;
21  double _r, _i;
22  for (;;) {
23  if ((!(s >> lpar >> _r >> comma >> _i >> rpar)) || (lpar != '(') ||
24  (rpar != ')') || (comma != ',')) {
25  // bad stream input
26  s.clear();
27  std::cout << "format for complex value is '( double, double)'\n"
28  << " -- try again" << std::endl;
29  continue;
30  }
31  break;
32  }
33  z = complex(_r, _i);
34  return s;
35 }
36 
37 complex
38 operator/(const complex value1, const complex value2)
39 {
40  double divisor = value2.re * value2.re + value2.im * value2.im;
41  return complex((value2.re * value1.re + value2.im * value1.im) / divisor,
42  (value1.im * value2.re - value1.re * value2.im) / divisor);
43 }
complex operator/(const complex value1, const complex value2)
Definition: complex.cpp:38
std::istream & operator>>(std::istream &s, complex &z)
Definition: complex.cpp:18
double re
Definition: complex.h:47
std::ostream & operator<<(std::ostream &s, const complex &z)
Definition: complex.cpp:11
double im
Definition: complex.h:47
double imag(const complex &_z)
Definition: complex.h:103
double real(const complex &_z)
Definition: complex.h:98