Advertisement
AmirVagapov

Untitled

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