Advertisement
AmirVagapov

Untitled

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