Advertisement
Guest User

Untitled

a guest
Apr 25th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.74 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.     complex operator [] (int);
  18.     complex 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. };
  26.  
  27. bool complex::operator > (complex& com)
  28. {
  29.     if (this->re > com.re)
  30.         return 1;
  31.     else if (this->re == com.re && this->im > com.im)
  32.         return 1;
  33.     else
  34.         return 0;
  35. }
  36.  
  37. bool complex::operator < (complex& com)
  38. {
  39.     if (this->re < com.re)
  40.         return 1;
  41.     else if (this->re == com.re && this->im < com.im)
  42.         return 1;
  43.     else
  44.         return 0;
  45.  
  46. }
  47.  
  48. bool complex::operator != (complex& com)
  49. {
  50.     if (this->re != com.re || this->im != com.im)
  51.         return 1;
  52.     else
  53.         return 0;
  54. }
  55.  
  56. bool complex::operator==(complex& com)
  57. {
  58.     if (this->re == com.re && this->im == com.im)
  59.         return 1;
  60.     else
  61.         return 0;
  62. }
  63.  
  64.  
  65.  
  66. complex complex::operator*(complex &com)
  67. {
  68.     double i, j;
  69.     i = re * com.re - im * com.im;
  70.     j = re * com.im + com.re * im;
  71.     re = i;
  72.     im = j;
  73.     return *this;
  74. }
  75.  
  76.  
  77. complex& complex::operator=(complex com)
  78. {
  79.     this->re = com.re;
  80.     this->im = com.im;
  81.     return *this;
  82. }
  83.  
  84. complex complex::operator+(complex com)
  85. {
  86.     this->re = this->re + com.re;
  87.     this->im = this->im + com.im;
  88.     return *this;
  89. }
  90.  
  91. complex complex::operator-(complex com)
  92. {
  93.     this->re = this->re - com.re;
  94.     this->im = this->im - com.im;
  95.     return *this;
  96. }
  97.  
  98.  
  99. ostream& operator << (ostream& out, const complex& com)
  100. {
  101.     if (com.im < 0)
  102.         out << com.re << "+i(" << com.im << ")\n";
  103.     else
  104.         out << com.re << "+i" << com.im << "\n";
  105.     return out;
  106. }
  107.  
  108. istream& operator >> (istream& in, complex& com)
  109. {
  110.     std::cout << "Введите действительную часть комплексного числа ";
  111.     in >> com.re;
  112.     std::cout << "Введите мнимую часть комплексного числа ";
  113.     in >> com.im;
  114.     return in;
  115. }
  116.  
  117. float complex::operator[](int i) {
  118.     if (i == 0) {
  119.         return re;
  120.     }
  121.     else if (i == 1) {
  122.         return im;
  123.     }
  124.     else {
  125.         cout << "Оператор []";
  126.         abort();
  127.     }
  128. }
  129. float complex::operator()(char c) {
  130.     if (c == 're') {
  131.         return re;
  132.     }
  133.     else if (c == 'im') {
  134.         return im;
  135.     }
  136.     else {
  137.         cout << "Оператор ()";
  138.         abort();
  139.     }
  140. }
  141.  
  142. int main()
  143. {
  144.     setlocale(0, "rus");
  145.     complex com;
  146.     cin >> com;
  147.     cout << com << endl;
  148.     system("pause");
  149.     return 0;
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement