home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / CTECHAPP.ZIP / CLASSES.ZIP / CP_OPS.CPP < prev    next >
C/C++ Source or Header  |  1990-01-28  |  1KB  |  77 lines

  1. //  Module:     Cp_ops
  2. //  Version:    2.20
  3. //
  4. //  Language:   C++ 2.0
  5. //  Environ:    Any
  6. //
  7. //  Purpose:    Basic operators for Complex objects
  8. //
  9. //  Written by: Scott Robert Ladd
  10.  
  11. #include "Complex.hpp"
  12.  
  13. extern "C"
  14.     {
  15.     #include "math.h"
  16.     }
  17.  
  18. // unary minus method
  19. Complex Complex::operator - ()
  20.     {
  21.     Complex result;
  22.  
  23.     result.Real = -Real;
  24.     result.Imag = -Imag;
  25.  
  26.     return result;
  27.     }
  28.  
  29. // calculation methods
  30. Complex operator + (const Complex & c1, const Complex & c2)
  31.     {
  32.     Complex result;
  33.  
  34.     result.Real = c1.Real + c2.Real;
  35.     result.Imag = c1.Imag + c2.Imag;
  36.  
  37.     return result;
  38.     }
  39.  
  40. Complex operator - (const Complex & c1, const Complex & c2)
  41.     {
  42.     Complex result;
  43.  
  44.     result.Real = c1.Real - c2.Real;
  45.     result.Imag = c1.Imag - c2.Imag;
  46.  
  47.     return result;
  48.     }
  49.  
  50. Complex operator * (const Complex & c1, const Complex & c2)
  51.     {
  52.     Complex result;
  53.  
  54.     result.Real = (c1.Real * c2.Real) - (c1.Imag * c2.Imag);
  55.     result.Imag = (c1.Real * c2.Imag) + (c1.Imag * c2.Real);
  56.  
  57.     return result;
  58.     }
  59.  
  60. Complex operator / (const Complex & c1, const Complex & c2)
  61.     {
  62.     Complex result;
  63.     double den;
  64.  
  65.     den = norm(c2);
  66.  
  67.     if (den != 0.0)
  68.         {
  69.         result.Real = (c1.Real * c2.Real + c1.Imag * c2.Imag) / den;
  70.         result.Imag = (c1.Imag * c2.Real - c1.Real * c2.Imag) / den;
  71.         }
  72.     else
  73.         Complex::ErrorHandler();
  74.  
  75.     return result;
  76.     }
  77.