_DeNiS_On4Ik_

111111

Dec 30th, 2021
1,277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.64 KB | None | 0 0
  1. class Complex // класс комплексных чисел
  2. {
  3.    double re, im; // целая и мнимая части
  4.  
  5. // создаем конструкторы  
  6. public:
  7.  
  8.    Complex() {};
  9.  
  10.    Complex (double r) // конструктор по умолчанию
  11.    {
  12.       re = r;
  13.       im = 0;
  14.    }
  15.  
  16.    Complex (double r, double i) // конструктор по умолчанию
  17.    {
  18.       re = r;
  19.       im = i;
  20.    }
  21.  
  22.    Complex (Complex &c) // конструктор копирования
  23.    {
  24.       re = c.re;
  25.       im = c.im;
  26.    }
  27.  
  28.    ~Complex() {}
  29.  
  30.    float abs() // Модуль комплексного числа
  31.    {
  32.       return sqrt(re * re - im * im);
  33.    }    
  34.  
  35.    Complex & operator = (Complex &c) // перегрузка оператора присваивания
  36.    {
  37.       re = c.re;
  38.       im = c.im;
  39.  
  40.       return (*this);
  41.    }
  42.  
  43.    Complex Complex::operator + (Complex &c) // перегрузка оператора сложения
  44.    {
  45.       Complex temp;
  46.  
  47.       temp.re = re + c.re;
  48.       temp.im = im + c.re;
  49.  
  50.       return temp;
  51.    }
  52.  
  53.    Complex Complex::operator - (Complex &c) // перегрузка оператора вычитания
  54.    {
  55.       Complex temp;
  56.  
  57.       temp.re = re - c.re;
  58.       temp.im = im - c.re;
  59.  
  60.       return temp;
  61.    }
  62.  
  63.    Complex Complex::operator * (Complex &c) // перегрузка оператора умножения
  64.    {
  65.       Complex temp;
  66.  
  67.       temp.re = re*c.re;
  68.       temp.im = re*c.im;
  69.  
  70.       return temp;
  71.    }
  72.  
  73.    Complex Complex::operator / (Complex &c) // перегрузка оператора деления
  74.    {
  75.       Complex temp;
  76.  
  77.       double r = c.re * c.re + c.im * c.im;
  78.       temp.re = (re * c.re + im * c.im) / r;
  79.       temp.re = (im * c.re - re * c.im) / r;
  80.  
  81.       return temp;
  82.    }  
  83.  
  84.    friend ostream &operator<<(ostream &, Complex &); // перегрузка оператора <<
  85.    friend istream &operator>>(istream &, Complex &); // перегрузка оператора >>
  86.  
  87. };
  88.  
  89. ostream &operator<<(ostream &out, complex &c)
  90. {
  91.    out << "(" << c.re << ") + I (" << c.im << "\n";
  92.  
  93.    return out;
  94. }
  95.  
  96. istream &operator>>(istream &in, Complex &c)
  97. {
  98.    in >> c.re >> c.im;
  99.  
  100.    return in;
  101. }
  102.  
  103. int main()
  104. {
  105.     Complex value1(5,2);
  106.     Complex value2(3,-3);
  107.  
  108.     cout << value1 << " " << value2 << endl;
  109.  
  110.     cout << value1 + value2 << endl;
  111.  
  112.     cout << value1 - value2 << endl;
  113.  
  114.     cout << value1 * value2 << endl;
  115.  
  116.     cout << value1 / value2 << endl;    
  117.  
  118.     value1 = value2;
  119.  
  120.     cout << value1 << " = " << value2 << endl;
  121.  
  122.     return 0;
  123. }
Advertisement
Add Comment
Please, Sign In to add comment