Advertisement
Guest User

Untitled

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