home *** CD-ROM | disk | FTP | other *** search
/ Australian Personal Computer 1999 November / APC411-1.ISO / workshop / other / c / complex.cpp next >
Encoding:
C/C++ Source or Header  |  1999-08-30  |  2.3 KB  |  106 lines

  1. // **************************************************************
  2. // Complex.cpp
  3. // Example program for Simple C++
  4. //
  5. // (c) 1999 Emmenjay Consulting Pty Ltd                          
  6. //                                                               
  7. // History                                                       
  8. // 30/08/99 MJS  Initial Coding.                                 
  9. //                                                               
  10. // **************************************************************
  11.  
  12. #include <iostream>
  13.  
  14. class CComplex {
  15. private:
  16.   double r, i;
  17. public:
  18.   CComplex(void);
  19.   CComplex(double r0, double i0);
  20.   double Real(void);
  21.   double Imag(void);
  22.   void   Set(double r0, double i0);
  23.  
  24. };
  25.  
  26. CComplex operator +(CComplex &c0, CComplex &c1)
  27. {
  28.   CComplex c( c0.Real()+c1.Real(), c0.Imag()+c1.Imag() );
  29.   return c;
  30. }
  31.  
  32. CComplex operator +(CComplex &c0, double d)
  33. {
  34.   CComplex c( c0.Real()+d, c0.Imag() );
  35.   return c;
  36. }
  37.  
  38. CComplex operator -(CComplex &c0, CComplex &c1)
  39. {
  40.   CComplex c( c0.Real()-c1.Real(), c0.Imag()-c1.Imag() );
  41.   return c;
  42. }
  43.  
  44. CComplex operator *(CComplex &c0, CComplex &c1)
  45. {
  46.   // (a + bi) * (c + di) = (ac - bd) + (ad + bc)i
  47.   CComplex c( c0.Real()*c1.Real() - c0.Imag()*c1.Imag(),
  48.               c0.Real()*c1.Imag() + c0.Imag()*c1.Real() );
  49.   return c;
  50. }
  51.  
  52. CComplex operator /(CComplex &c0, CComplex &c1)
  53. {
  54.   // (a + bi) / (c + di) = (ac + bd)/(c**2 + d**2) + ((bc - ad)/(c**2 + d**2))i 
  55.   double denom, re, im;
  56.  
  57.   denom = c1.Real()*c1.Real() + c1.Imag()*c1.Imag();
  58.   re = (c0.Real()*c1.Real() + c0.Imag()*c1.Imag()) / denom;
  59.   im = (c0.Imag()*c1.Real() - c0.Real()*c1.Imag()) / denom;
  60.   CComplex c( re, im );
  61.   return c;
  62. }
  63.  
  64. ostream& operator <<(ostream &s, CComplex &c)
  65. {
  66.   s << "(" << c.Real() << "," << c.Imag() << ")";
  67.   return s;
  68. }
  69.  
  70. CComplex::CComplex(void)
  71. {
  72.   Set(0,0);
  73. }
  74.  
  75. CComplex::CComplex(double r0, double i0)
  76. {
  77.   Set(r0,i0);
  78. }
  79.  
  80. double CComplex::Real(void)
  81. {
  82.   return r;
  83. }
  84.  
  85. double CComplex::Imag(void)
  86. {
  87.   return i;
  88. }
  89.  
  90. void   CComplex::Set(double r0, double i0)
  91. {
  92.   r=r0;
  93.   i=i0;
  94. }
  95.  
  96. int main(void)
  97. {
  98.   CComplex c0(3.8,6.4);
  99.   CComplex c1(2.3,5.1);
  100.   CComplex c = c0-c1;
  101.   std::cout << "c = " << c << '\n';
  102.   c1 = c+3.0;
  103.   std::cout << "c1 = " << c1 << '\n';
  104.   return 0;
  105. }
  106.