Advertisement
Ctyderv

complex

Apr 21st, 2021
639
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.02 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. struct complex {
  4.     public:
  5.         complex(float r, float i): re(r), im(i) {}
  6.         complex() {}
  7.  
  8.         float getRe() const { return re; };
  9.         float getIm() const { return im; };
  10.  
  11.         complex operator+(const complex& z) {
  12.             return complex( re + z.getRe(), im + z.getIm());
  13.         }
  14.  
  15.         friend std::ostream& operator<<(std::ostream& os, const complex& z) {
  16.             os << z.getRe() << " + i*" << z.getIm();
  17.             return os;
  18.         }
  19.     private:
  20.         float re, im;
  21. };
  22.  
  23. complex operator-(const complex& a, const complex& b) {
  24.     return complex( a.getRe() - b.getRe(), a.getIm() - b.getIm());
  25. }
  26.  
  27. std::istream& operator>>(std::istream& is, complex& z){
  28.     // aici am realizat ca designul meu nu prea permite citirea directa
  29.     float x, y; is >> x >> y;
  30.     z = complex( x, y);
  31.  
  32.     return is;
  33. }
  34.  
  35. int main() {
  36.     complex a, b;
  37.  
  38.     std::cin >> a >> b;
  39.     std::cout << ( a + b) << "\n"
  40.               << ( a - b) << "\n";
  41.  
  42.     return 0;
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement