Advertisement
Tucancitto

Pb01. Numere complexe

Apr 15th, 2021
472
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.34 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. struct Complex
  4. {
  5.     float re, im;
  6.     void citire()
  7.     {
  8.         std::cin >> re >> im;
  9.     }
  10.  
  11.     void afisare()
  12.     {
  13.         if (im == 0)
  14.             std::cout << re;
  15.         else
  16.             if (im > 0)
  17.                 std::cout << re << " + " << fabs(im) << "*i";
  18.             else
  19.                 std::cout << re << " - " << fabs(im) << "*i";
  20.     }
  21.  
  22.     void conjugata()
  23.     {
  24.         im = -im;
  25.     }
  26.  
  27.     float modul()
  28.     {
  29.         return sqrt(re * re + im * im);
  30.     }
  31.  
  32.     Complex adunare(Complex C)
  33.     {
  34.         Complex suma = { 0,0 };
  35.         suma.re = re + C.re;
  36.         suma.im = im + C.im;
  37.         return suma;
  38.     }
  39.  
  40.     Complex scadere(Complex C)
  41.     {
  42.         Complex diferenta = { 0,0 };
  43.         diferenta.re = re - C.re;
  44.         diferenta.im = im - C.im;
  45.         return diferenta;
  46.     }
  47.  
  48.     Complex inmultire(Complex C)// (a + bi)(c + di) = (ac - bd) + (ad + bc)i
  49.     {
  50.         Complex produs = { 0,0 };
  51.         produs.re = re * C.re - im * C.im;
  52.         produs.im = re * C.im + im * C.re;
  53.         return produs;
  54.     }
  55.  
  56.     Complex impartire(Complex C)// (a + bi) / (c + di) = (a + bi)(c - di) / [(c + di)(c - di)] = [(ac + bd - (ad - bc)i] / (c*c + d*d)
  57.     {
  58.         Complex cat = { 0,0 };
  59.         cat.re = (re * C.re + im * C.im) / (C.re * C.re + C.im * C.im);
  60.         cat.im = (-re * C.im + im * C.re) / (C.re * C.re + C.im * C.im);
  61.         return cat;
  62.     }
  63. };
  64.  
  65. int main()
  66. {
  67.     Complex z = { 0,0 }, w = { 0,0 };
  68.     z.citire();
  69.     w.citire();
  70.     Complex cat;
  71.     cat = z.impartire(w);
  72.     cat.afisare();
  73.  
  74.     return 0;
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement