Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2017
263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.25 KB | None | 0 0
  1. #ifndef COMPLEX_HPP_
  2. #define COMPLEX_HPP_
  3. #include <iostream>
  4. #include <cstdlib>
  5. #include <cmath>
  6.  
  7. using namespace std;
  8.  
  9. class Complex
  10. {
  11. private:
  12. double real;
  13. double imag;
  14. public:
  15. Complex(double=0,double=0);
  16. double getReal() const
  17. {
  18. return real;
  19. }
  20.  
  21. double getImag() const
  22. {
  23. return imag;
  24. } //zwraca cz. urojon¹
  25. double getModule() const
  26. {
  27. double R,I;
  28. R=real;
  29. I=imag;
  30. return sqrt(R*R+I*I);
  31. }
  32. double getPhase() const
  33. {
  34. double R, I, z, cos;
  35. R=real;
  36. I=imag;
  37. z=sqrt(R*R+I*I);
  38. cos=R/z;
  39. if(I>0)
  40. return (acos(cos)*180)/M_PI; //JESLI RADIANY, TO SAM ARCSIN
  41. else
  42. return (((acos(cos)*180)/M_PI)-360)*(-1);
  43. } //zwraca fazê
  44. friend ostream& operator << (ostream& out, const Complex& C)
  45. {
  46. if(C.real!=0)
  47. out<<C.real;
  48. else
  49. out<<"";
  50. if(C.imag==0 && C.real!=0)
  51. out<<"";
  52. else if(C.imag==0 && C.real==0)
  53. out<<0;
  54. else if(C.imag==1 && C.real!=0)
  55. out<<" + i";
  56. else if(C.imag==1 && C.real==0)
  57. out<<" i";
  58. else if(C.imag==-1)
  59. out<<" - i";
  60. else if(C.imag>0 && C.imag!=1 && C.real!=0)
  61. out<<" + "<<C.imag<<"i";
  62. else if(C.imag>0 && C.imag!=1 && C.real==0)
  63. out<<C.imag<<"i";
  64. else if(C.imag<0 && C.imag!=-1)
  65. out<<" - "<<(-1)*C.imag<<"i";
  66. out<<endl;
  67. return out;
  68. }
  69. bool operator == (const Complex& C) const
  70. {
  71. if(this->real==C.real && this->imag==C.imag)
  72. {
  73. return true;
  74. }
  75. else
  76. {
  77. return false;
  78. }
  79. }
  80. friend Complex operator + (const Complex& C1, const Complex& C2)
  81. {
  82. Complex tmp;
  83. tmp.real=C1.real+C2.real;
  84. tmp.imag=C1.imag+C2.imag;
  85. return tmp;
  86. }
  87. Complex& operator += (const Complex& C)
  88. {
  89. *this=*this+C;
  90. return *this;
  91. }
  92. friend Complex operator - (const Complex& C1, const Complex& C2)
  93. {
  94. Complex tmp;
  95. tmp.real=C1.real-C2.real;
  96. tmp.imag=C1.imag-C2.imag;
  97. return tmp;
  98. }
  99. Complex& operator -= (const Complex& C)
  100. {
  101. *this=*this-C;
  102. return *this;
  103. }
  104. friend Complex operator * (const Complex& C1, const Complex& C2)
  105. {
  106. Complex tmp;
  107. tmp.real=C1.real*C2.real-C1.imag*C2.imag;
  108. tmp.imag=C1.real*C2.imag+C1.imag*C2.real;
  109. return tmp;
  110. }
  111. Complex& operator *= (const Complex& C)
  112. {
  113. *this=*this*C;
  114. return *this;
  115. }
  116. friend Complex operator / (const Complex& C1, const Complex& C2)
  117. {
  118. Complex tmp, dzielna;
  119. double dzielnik;
  120. dzielna.real=C1.real*C2.real+C1.imag*C2.imag;
  121. dzielna.imag=C1.real*(-1)*C2.imag+C1.imag*C2.real;
  122. dzielnik=C2.real*C2.real+C2.imag*C2.imag;
  123. tmp.real=dzielna.real/dzielnik;
  124. tmp.imag=dzielna.imag/dzielnik;
  125. return tmp;
  126.  
  127. }
  128. Complex& operator /= (const Complex& C)
  129. {
  130. *this=*this/C;
  131. return *this;
  132. }
  133. };
  134.  
  135. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement