Advertisement
Deerenaros

Complex numbers in C++ by Andrew_Lvov.

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