Advertisement
Vladislav_Bezruk

OOP 8 lab 1 task

Sep 30th, 2021 (edited)
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.05 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3.  
  4. #define EPS 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; im = x.im;
  16.             return *this;
  17.         }
  18.  
  19.         Comp &operator -= (const Comp &x) {
  20.             re -= x.re; im -= x.im;
  21.             return *this;
  22.         }
  23.        
  24.         friend Comp operator + (const Comp &a, const Comp &b);
  25.        
  26.         bool operator == (const Comp &x) {
  27.             return (this -> re == x.re && this -> im == x.im);
  28.         }
  29.        
  30.         void set(char c);
  31.        
  32.         void get(char c);
  33. };
  34.  
  35. int main() {
  36.    
  37.     Comp a, b, c;
  38.    
  39.     a.set('a');
  40.     b.set('b');
  41.    
  42.     cout << endl << "Operator -=" << endl;
  43.    
  44.     cout << "before:" << endl;
  45.    
  46.     a.get('a');
  47.     b.get('b');
  48.    
  49.     cout << endl << "after a -= b:" << endl;
  50.    
  51.     a -= b;
  52.    
  53.     a.get('a');
  54.     b.get('b');
  55.    
  56.    
  57.     cout << endl << "Operator +" << endl;
  58.    
  59.     a.get('a');
  60.     b.get('b');
  61.    
  62.     c = a + b;
  63.    
  64.     cout << endl << "after c = a + b:" << endl;
  65.    
  66.     c.get('c');
  67.    
  68.        
  69.     cout << endl << "Operator ==" << endl;
  70.    
  71.     a.get('a');
  72.     b.get('b');
  73.    
  74.     cout << endl << "after a == b:" << endl;
  75.    
  76.     if (a == b) {
  77.         cout << "true";
  78.     } else {
  79.         cout << "false";
  80.     }
  81.    
  82.     a = b;
  83.    
  84.     cout << endl << endl << "In a assigned b:" << endl;
  85.            
  86.     cout << endl << "Operator ==" << endl;
  87.    
  88.     a.get('a');
  89.     b.get('b');
  90.    
  91.     cout << endl << "after a == b:" << endl;
  92.    
  93.     if (a == b) {
  94.         cout << "true";
  95.     } else {
  96.         cout << "false";
  97.     }
  98.    
  99.     return 0;  
  100. }
  101.  
  102. Comp operator + (const Comp &a, const Comp &b) {
  103.     Comp c;
  104.    
  105.     c.re = a.re + b.re; c.im = a.im + b.im;
  106.     return c;  
  107. }
  108.  
  109. bool isZero(double x) { return fabs(x) > EPS; }
  110.  
  111. void Comp :: set(char c) {
  112.     cout << "Enter comp number " << c << ":" << endl;
  113.     cout << "\tre: ";
  114.     cin >> re;
  115.     cout << "\tim: ";
  116.     cin >> im;
  117.            
  118.     return;
  119. }
  120.        
  121. void Comp :: get(char c) {
  122.     cout << "Comp number " << c << ": ";
  123.     if (!isZero(re)) {
  124.         cout << re;
  125.     }
  126.            
  127.     if (!isZero(im)) {
  128.         if (!isZero(re) && im > 0) {
  129.                
  130.             cout << " + ";
  131.         }
  132.         if (im < 0) {
  133.             cout << " - ";
  134.         }
  135.         cout << abs(im) << "i";
  136.     }
  137.            
  138.     cout << endl;
  139.            
  140.     return;
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement