Advertisement
Vladislav_Bezruk

Some task

Oct 5th, 2021
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.78 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3.  
  4. #define ESP 1e-6
  5.  
  6. using namespace std;
  7.  
  8. class comp {
  9.     float Re, Im;
  10.    
  11.     public:
  12.         comp() {}
  13.  
  14.         comp& operator = (const comp &x) {
  15.             Re = x.Re;
  16.             Im = x.Im;
  17.             return *this;
  18.         }
  19.         comp& operator -= (const comp &x) {
  20.             Re -= x.Re;
  21.             Im -= x.Im;
  22.             return *this;
  23.         }
  24.        
  25.         friend comp operator + (const comp &a, const comp &b);
  26.  
  27.         bool operator == (const comp &x) {
  28.             return(this -> Re == x.Re && this -> Im == x.Im);
  29.         }
  30.        
  31.         void set(char s) {
  32.             cout<<"Enter comp numb "<<s<<":"<<endl;
  33.             cout<<"\tRe: ";
  34.             cin>>Re;
  35.             cout<<"\tIm: ";
  36.             cin>>Im;
  37.             return;
  38.         }
  39.        
  40.         void get(char s);
  41. };
  42. int main () {
  43.     comp a, b, c;
  44.     a.set('a');
  45.     b.set('b');
  46.  
  47.     cout<<"operator -="<<endl;
  48.     cout<<"before"<<endl;
  49.     a.get('a');
  50.     b.get('b');
  51.     cout<<"after"<<endl;
  52.     a -= b;
  53.     a.get('a');
  54.     b.get('b');
  55.  
  56.     cout<<"operator +"<<endl;
  57.  
  58.     a.get('a');
  59.     b.get('b');
  60.  
  61.     c = a + b;
  62.  
  63.     cout<<"c = a + b :"<<endl;
  64.     c.get('c');
  65.  
  66.     cout<<"operator == "<<endl;
  67.     a.get('a');
  68.     b.get('b');
  69.  
  70.     cout<<"a == b"<<endl;
  71.     if(a == b) {
  72.         cout<<"true";
  73.     } else {
  74.         cout<<"false";
  75.     }
  76.  
  77.     a = b;
  78.  
  79.     cout<<"  a assigned b :"<<endl;
  80.  
  81.     cout<<"operator =="<<endl;
  82.     a.get('a');
  83.     b.get('b');
  84.  
  85.     cout<<"a == b"<<endl;
  86.     if(a == b) {
  87.         cout<<"true";
  88.     } else {
  89.         cout<<"false";
  90.     }
  91.     return 0;
  92. }
  93. comp operator + (const comp &a, const comp &b) {
  94.     comp c;
  95.     c.Re = a.Re + b.Re;
  96.     c.Im = a.Im + b.Im;
  97.     return c;
  98. }
  99.  
  100. bool zero(double x) {
  101.     return fabs(x) <= ESP; //було так return fabs(x) > ESP;
  102. }
  103. void comp :: get(char s) {
  104.     cout <<"comp numb "<<s<<": ";
  105.     if(!zero(Re)) {
  106.         cout<<Re;
  107.     }
  108.     if(!zero(Im)) {
  109.         if(!zero(Im) && Im > 0) {
  110.             cout<<" + ";
  111.         }
  112.         if(Im < 0) {
  113.             cout<<" - ";
  114.         }
  115.         cout << abs(Im)<<"i";
  116.     }
  117.     cout <<endl;
  118.     return;
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement