Advertisement
Guest User

Untitled

a guest
Feb 20th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.31 KB | None | 0 0
  1. #include <fstream>
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. class complex {
  7.     float im;
  8.     float re;
  9. public:
  10.     complex() // по умолчанию
  11.     {
  12.         cout << "CREATED DEFAULT" << endl;
  13.         im = -1;
  14.         re = -1;
  15.     }
  16.     complex(const complex& o)
  17.     {
  18.         re = o.re;
  19.         im = o.im;
  20.         cout << "CREATED COPY" << endl;
  21.     }
  22.     complex(float re, float im)
  23.         : im(im), re(re)
  24.     {
  25.         cout << "CREATED HIPSTER" << endl;
  26.         this->im = im;
  27.         this->re = re;
  28.     }
  29.     ~complex()
  30.     {
  31.         cout << "DELETED" << endl;
  32.     }
  33.     float getIm()
  34.     {
  35.         return im;
  36.     }
  37.     float getRe()
  38.     {
  39.         return re;
  40.     }
  41.     void setIm(float im)
  42.     {
  43.         this->im = im;
  44.     }
  45.     void setRe(float re)
  46.     {
  47.         this->re = re;
  48.     }
  49.     complex operator+(float other)
  50.     {
  51.         complex c;
  52.         c.re = other + re;
  53.         c.im = im;
  54.         return c;
  55.     }
  56.     complex operator-(float other)
  57.     {
  58.         complex c;
  59.         c.re = re - other;
  60.         c.im = im;
  61.         return c;
  62.     }
  63.     complex operator*(float other);
  64.     complex operator/(float other);
  65.     complex operator+(complex& other)
  66.     {
  67.         complex c;
  68.         c.re = re + other.re;
  69.         c.im = im + other.im;
  70.         return c;
  71.     }
  72.     complex operator-(complex& other)
  73.     {
  74.         complex c;
  75.         c.re = re - other.re;
  76.         c.im = im - other.im;
  77.         return c;
  78.     }
  79.     complex operator*(complex& other)
  80.     {
  81.         complex c;
  82.         c.re = re*other.re - im*other.im;
  83.         c.im = re*other.im + im*other.re;
  84.         return c;
  85.     }
  86.     complex operator/(complex& other)
  87.     {
  88.         complex c;
  89.         c.re = (re*other.re + im*other.im) / (other.re*other.re + other.im*other.im);
  90.         c.im = (other.re*im - re*other.im) / (other.re*other.re + other.im*other.im);
  91.         return c;
  92.     }
  93.     void print();
  94.  
  95.     friend complex operator+(float other, complex& first);
  96.     friend complex operator-(float other, complex& first);
  97. };
  98.  
  99.  
  100. complex operator-(float other, complex& first)
  101. {
  102.     complex c;
  103.     c.re = first.re - other;
  104.     c.im = first.im;
  105.     return c;
  106. }
  107. complex operator*(float other, complex& first)
  108. {
  109.     complex c;
  110.     c.setRe(first.getRe()*other);
  111.     return c;
  112. }
  113. complex operator/(float other, complex& first)
  114. {
  115.     complex c;
  116.     c.setRe(first.getRe()/other);
  117.     return c;
  118. }
  119.  
  120.  
  121. complex operator+(float other, complex& first)
  122. {
  123.     complex c;
  124.     c.re = other + first.re;
  125.     c.im = first.im;
  126.     return c;
  127. }
  128.  
  129.  
  130. int main()
  131. {
  132.     complex p1(1, 2);
  133.     complex p2(2, 3);
  134.     complex c = p2/p1; //p1.operator+(p2);
  135.  
  136.     cout << c.getIm() << "*i + " << c.getRe() << endl;
  137.  
  138.     return 1;
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement