Advertisement
AmirVagapov

Untitled

Mar 29th, 2022
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.48 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. class Complex {
  6.    
  7.     double re, im;
  8.    
  9.     public:
  10.         // Complex(double, double);
  11.         Complex(double Re = 0.0, double Im = 0.0) {
  12.             re = Re;
  13.             im = Im;
  14.         }
  15.        
  16.         double getRe() {
  17.             return re;
  18.         }
  19.         double getIm() {
  20.             return im;
  21.         }
  22.    
  23.         void setRe(double a) {
  24.             re = a;
  25.         }
  26.         void setIm(double a) {
  27.             im = a;
  28.         }
  29.        
  30.         bool operator == (const Complex &);
  31.         bool operator != (const Complex &);
  32.        
  33.         Complex operator + (const Complex &);
  34.         Complex operator - (const Complex &);
  35.         Complex operator * (const Complex &);
  36.         Complex operator / (const Complex &);
  37.  
  38.         friend ostream & operator << (ostream & os,const Complex & a);
  39.         friend istream & operator >> (istream & is, Complex & a);
  40.        
  41. };
  42.  
  43.  
  44.  
  45.  
  46. bool Complex::operator ==(const Complex & x) {
  47.     return (re == x.re && im == x.im);
  48. }
  49.  
  50. bool Complex::operator !=(const Complex & x) {
  51.     return !(re == x.re && im == x.im);
  52. }
  53.  
  54. Complex Complex::operator + (const Complex & x) {
  55.     Complex res;
  56.     res.re = re + x.re;
  57.     res.im = im + x.im;
  58.     return res;
  59. }
  60. Complex Complex::operator - (const Complex & x) {
  61.     Complex res;
  62.     res.re = re - x.re;
  63.     res.im = im - x.im;
  64.     return res;
  65. }
  66.    
  67. Complex Complex::operator * (const Complex & x) {
  68.     Complex res;
  69.     res.re = re * x.re - im * x.im;
  70.     res.im = re * x.im + x.re * im;
  71.     return res;
  72. }
  73. Complex Complex::operator / (const Complex & x) {
  74.     Complex res;
  75.     res.re = (re * x.re + im * x.im) / (im * im + x.im * x.im);
  76.     res.im = (im * x.re - re * x.im) / (im * im + x.im * x.im);
  77.     return res;
  78. }
  79.  
  80. ostream & operator << (ostream & os, const Complex & a) {
  81.     os << a.re;
  82.     if (a.im)
  83.         os << showpos << a.im << noshowpos << "i";
  84.     os << endl;
  85.     return os;
  86. }
  87. istream & operator >> (istream & is, Complex & a) {
  88.     is >> a.re >> a.im;
  89.     return is;
  90. }
  91.  
  92. int main() {
  93.     Complex a, b;
  94.     cin >> a;
  95.     cin >> b;
  96.     cout << a;
  97.     a = a + b;
  98.     cout << a;
  99.     a = a / b;
  100.     cout << a;
  101.     a.setIm(5);
  102.     a.setRe(2);
  103.     b.setIm(5);
  104.     b.setRe(2);
  105.     if (a!=b) cout << "NO";
  106.     else cout <<"YES";
  107.     b = a * b;
  108.     cout << b;
  109.     b = b / a;
  110.     if (a==b) cout << "YES";
  111.     else cout <<"NO";
  112.     cout << endl << a.getRe() << " " << a.getIm() << endl;
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement