Advertisement
Guest User

Untitled

a guest
May 22nd, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.60 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. class complex
  6. {
  7. private:
  8. double re, im;
  9. public:
  10. complex() { re  = 0; im  = 0; }
  11. complex(double r, double i) { re  = r; im  = i; }
  12. complex(const complex &ob) { re  = ob.re; im  = ob.im; };
  13. complex& operator  = (complex);
  14. complex operator  + (complex);
  15. complex operator  - (complex);
  16. complex operator * (complex&);
  17. float operator [](int);
  18. float operator () (char);
  19. friend istream& operator  >> (istream&, complex&);
  20. friend ostream& operator  << (ostream&, const complex&);
  21. bool operator  == (complex& com);
  22. bool operator  != (complex& com);
  23. bool operator > (complex& com);
  24. bool operator < (complex& com);
  25. complex operator  + (int);
  26. complex operator * (int);
  27. };
  28.  
  29. bool complex::operator >(complex& com)
  30. {
  31. if (this->re > com.re)
  32. return 1;
  33. else if (this->re  == com.re && this->im > com.im)
  34. return 1;
  35. else
  36. return 0;
  37. }
  38.  
  39. bool complex::operator < (complex& com)
  40. {
  41. if (this->re < com.re)
  42. return 1;
  43. else if (this->re  == com.re && this->im < com.im)
  44. return 1;
  45. else
  46. return 0;
  47.  
  48. }
  49.  
  50. bool complex::operator  != (complex& com)
  51. {
  52. if (this->re  != com.re  || this->im  != com.im)
  53. return 1;
  54. else
  55. return 0;
  56. }
  57.  
  58. bool complex::operator==(complex& com)
  59. {
  60. if (this->re  == com.re && this->im  == com.im)
  61. return 1;
  62. else
  63. return 0;
  64. }
  65.  
  66. complex& complex::operator=(complex com)
  67. {
  68. this->re  = com.re;
  69. this->im  = com.im;
  70. return *this;
  71. }
  72.  
  73. complex complex::operator*(complex &com)
  74. {
  75. double i, j;
  76. i  = re * com.re  - im * com.im;
  77. j  = re * com.im  + com.re * im;
  78. re  = i;
  79. im  = j;
  80. return *this;
  81. }
  82.  
  83. complex complex::operator+(complex com)
  84. {
  85. this->re  = this->re  + com.re;
  86. this->im  = this->im  + com.im;
  87. return *this;
  88. }
  89.  
  90. complex complex::operator-(complex com)
  91. {
  92. this->re  = this->re  - com.re;
  93. this->im  = this->im  - com.im;
  94. return *this;
  95. }
  96.  
  97.  
  98. ostream& operator  << (ostream& out, const complex& com)
  99. {
  100. if (com.im < 0)
  101. out  << com.re  << "+i(" << com.im  << ")\n";
  102. else
  103. out  << com.re  << "+i" << com.im  << "\n";
  104. return out;
  105. }
  106.  
  107. istream& operator  >> (istream& in, complex& com)
  108. {
  109. std::cout  << "Введите действительную часть комплексного числа ";
  110. in  >> com.re;
  111. std::cout  << "Введите мнимую часть комплексного числа ";
  112. in  >> com.im;
  113. return in;
  114. }
  115.  
  116. float complex::operator[](int i) {
  117. if (i  == 0) {
  118. return re;
  119. }
  120. else if (i  == 1) {
  121. return im;
  122. }
  123. else {
  124. cout  << "Оператор []";
  125. abort();
  126. }
  127. }
  128. float complex::operator()(char c) {
  129. if (c  == 're') {
  130. return re;
  131. }
  132. else if (c  == 'im') {
  133. return im;
  134. }
  135. else {
  136. cout  << "Оператор ()";
  137. abort();
  138. }
  139. }
  140. complex complex::operator  + (int i) {
  141. complex com(*this);
  142. com.re  += i;
  143. return com;
  144. }
  145. complex complex::operator * (int i) {
  146. complex com(*this);
  147. com.re  *= i;
  148. return com;
  149. }
  150. int main()
  151. {
  152. setlocale(0, "rus");
  153. complex com, com1(2, 1);
  154. cin  >> com;
  155. cout  << com  << endl;
  156. com  = com  + 3;
  157. cout  << com  << endl;
  158. com  = com * com1;
  159. cout  << com  << endl;
  160. com  = com  - com1;
  161. cout  << com  << endl;
  162. cout  << com[1] << endl;
  163. if (com  == com1) {
  164. cout  << "True" << endl;
  165.  
  166. }
  167. else {
  168. cout  << "False" << endl;
  169. }
  170. if (com  != com1) {
  171. cout  << "True" << endl;
  172.  
  173. }
  174. else {
  175. cout  << "False" << endl;
  176. }
  177. system("pause");
  178. return 0;
  179. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement