Advertisement
Guest User

Untitled

a guest
Jun 28th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.23 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class Cplx
  5. {
  6.     friend ostream& operator<<(ostream &os, Cplx z);
  7.     friend Cplx operator+(Cplx &a, Cplx &b);
  8.     friend Cplx operator-(Cplx &a, Cplx &b);
  9.     friend Cplx operator*(Cplx &a, Cplx &b);
  10. private:
  11.     float re, im;
  12. public:
  13.  
  14.     Cplx(float x = 0, float y = 0) { re = x; im = y; }
  15.     void   Setri(float x, float y) { re = x; im = y; }
  16.     float  Getr() { return re; }
  17.     void   Seti(float x) { im = x; }
  18. };
  19.  
  20.  
  21. Cplx operator+(Cplx &a, Cplx &b)
  22. {
  23.     return Cplx(a.re + b.re, a.im + b.im);
  24. }
  25. Cplx operator-(Cplx &a, Cplx &b)
  26. {
  27.     return Cplx(a.re - b.re, a.im - b.im);
  28. }
  29. Cplx operator*(Cplx &a, Cplx &b)
  30. {
  31.     return Cplx(a.re*b.re - a.im*b.im, a.re*b.im + a.im*b.re);
  32. }
  33. Cplx operator/(Cplx &a, Cplx &b)
  34. {
  35.     return Cplx((a.re*b.re + a.im*b.im)/b.re*b.re+b.im*b.im, (a.im*b.re-a.re*b.im)/b.re*b.re+b.im*b.im);
  36. }
  37. ostream& operator<<(ostream &os, Cplx z)
  38. {
  39.     os << z.re;
  40.     if(z.im >= 0) os << " + ";
  41.     return os << z.im << "i";
  42. }
  43.  
  44. int main()
  45. {
  46.     Cplx a(5, 6), b(8, 3), c; // deklaracija tri kompleksna broja
  47.     c = a + b;         
  48.     cout << c;
  49.  
  50.     c = a - b;         
  51.     cout << c;
  52.  
  53.     c = a * b;         
  54.     cout << c; 
  55.  
  56.     c = a / b;         
  57.     cout << c; 
  58.     return 0;
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement