Srxon05

komplex.cpp

Nov 15th, 2017
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.98 KB | None | 0 0
  1. #include "komplex.hpp"
  2.  
  3. Complex::Complex()
  4. {
  5.     imag=0;
  6.     real=0;
  7. }
  8. Complex::Complex(double r, double i)
  9. {
  10.     imag=i;
  11.     real=r;
  12. }
  13. Complex::Complex(const Complex &Z)
  14. {
  15.     imag=Z.imag;
  16.     real=Z.real;
  17. }
  18.  
  19. double Complex::getReal()const
  20. {
  21.     return real;
  22. }
  23.  
  24. double Complex::getImag()const
  25. {
  26.     return imag;
  27. }
  28.  
  29. void Complex::setReal(double Real)
  30. {
  31.     Complex::real=Real;
  32. }
  33.  
  34. void Complex::setImag(double Imag)
  35. {
  36.     Complex::imag=Imag;
  37. }
  38.  
  39. Complex& Complex::operator=(const Complex &c1)
  40. {
  41.  
  42.     real=c1.real;
  43.     imag=c1.imag;
  44.     return *this;
  45. }
  46.  
  47. Complex& Complex::operator+=(const Complex &c1)
  48. {
  49.  
  50.     real=real + c1.real;
  51.     imag=imag + c1.imag;
  52.     return *this;// vraca referencu na objekat na levu stranu znaka jednakosti
  53. }
  54.  
  55. Complex& Complex::operator-=(const Complex &c1)
  56. {
  57.  
  58.     real=real - c1.real;
  59.     imag=imag - c1.imag;
  60.     return *this;
  61. }
  62.  
  63. Complex& Complex::operator*=(const Complex &c1)
  64. {
  65.  
  66.     real=real * c1.real - c1.real * imag;
  67.     imag=real * c1.imag + imag * c1.real;
  68.     return *this;
  69. }
  70.  
  71. Complex& Complex::operator/=(const Complex &c1)
  72. {
  73.  
  74.     real=(real * c1.real + c1.imag * imag)/(c1.real*c1.real + c1.imag*c1.imag);
  75.     imag=(imag * c1.real - c1.imag * real)/(c1.real*c1.real + c1.imag*c1.imag);
  76.     return *this;
  77. }
  78.  
  79. const Complex& Complex::operator++() //prefiksni operator odmah uvecava i vraca vrednost
  80. {
  81.     real++;
  82.     imag++;
  83.     return *this;//vraca objekat
  84. }
  85.  
  86. const Complex Complex::operator++(int i) //postfiksna vraca lokalnu kopiju w, ne vraca uvecani objekat
  87. {
  88.     Complex w(real, imag);
  89.     real++;
  90.     imag++;
  91.     return w;
  92. }
  93.  
  94. Complex operator+(const Complex &z1, const Complex &z2)
  95. {
  96.     Complex w(z1.real+z2.real, z1.imag+z2.imag);
  97.     return w;
  98. }
  99.  
  100. Complex operator-(const Complex &z1, const Complex &z2)
  101. {
  102.     Complex w(z1.real-z2.real, z1.imag-z2.imag);
  103.     return w;
  104. }
  105.  
  106. Complex operator*(const Complex &z1, const Complex &z2)
  107. {
  108.     Complex w(z1.real*z2.real-z1.imag*z2.real, z1.real*z2.imag+z1.imag*z2.real);
  109.     return w;
  110. }
  111.  
  112. Complex operator/(const Complex &z1, const Complex &z2)
  113. {
  114.     Complex w((z1.real*z2.real+z1.imag*z2.imag)/(z2.real*z2.real+z2.imag*z2.imag), (z1.imag*z2.real-z1.real*z2.imag)/(z2.real*z2.real+z2.imag*z2.imag));
  115.     return w;
  116. }
  117.  
  118. bool operator== (const Complex &z1, const Complex &z2)
  119. {
  120.     return (z1.real==z2.real)&&(z1.imag==z2.imag);
  121. }
  122. bool operator!= (const Complex &z1, const Complex &z2)
  123. {
  124.     bool p=(z1.real==z2.real)&&(z1.imag==z2.imag);
  125.     return !p;
  126. }
  127. ostream& operator<<(ostream &out, const Complex &z1)
  128. {
  129.     if(z1.imag==0 && z1.real!=0)
  130.     {
  131.         out<<z1.real;
  132.     }
  133.     if(z1.real==0 && z1.imag!=0)
  134.     {
  135.         out<<z1.imag<<"i";
  136.     }
  137.     if(z1.real!=0 && z1.imag>0)
  138.     {
  139.         out<<z1.real<<"+"<<z1.imag;
  140.     }
  141.     if(z1.real!=0 && z1.imag<0)
  142.     {
  143.         out<<z1.real<<z1.imag;
  144.     }
  145.     return out;
  146. }
  147.  
  148. istream& operator>>(istream &in, Complex &z)
  149. {
  150.     in>>z.real;
  151.     in>>z.imag;
  152.     return in;
  153. }
Advertisement
Add Comment
Please, Sign In to add comment