Advertisement
Bars0_j

Untitled

May 27th, 2020
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.86 KB | None | 0 0
  1. #include <iostream>
  2. #include <math.h>
  3. #define M_PI 3.1415926535
  4. using namespace std;
  5.  
  6. class complex {
  7. public:
  8.     double Re;
  9.     double Im;
  10.  
  11.     friend  complex operator+(const complex& left, const complex& right) {
  12.         return complex(left.Re + right.Re, left.Im + right.Im);
  13.     }
  14.     friend const complex operator-(const complex& left, const complex& right) {
  15.         return complex(left.Re - right.Re, left.Im - right.Im);
  16.     }
  17.     friend const complex operator*(const complex& left, const complex& right) {
  18.         return complex(
  19.             (right.Re * left.Re) - (right.Im * left.Im),
  20.             (right.Im * left.Re) + (right.Re * left.Im)
  21.         );
  22.     }
  23.     friend const complex operator/(const complex& left, const complex& right) {
  24.         double mult = pow(right.Re, 2) + pow(right.Im, 2);
  25.         return complex(
  26.             ((right.Re * left.Re) + (right.Im * left.Im)) / mult,
  27.             ((right.Re * left.Im) - (right.Im * left.Re)) / mult
  28.         );
  29.     }
  30.  
  31.     complex& operator+=(const complex& right) {
  32.         Re += right.Re;
  33.         Im += right.Im;
  34.         return *this;
  35.     }
  36.     complex& operator-=(const complex& right) {
  37.         Re -= right.Re;
  38.         Im -= right.Im;
  39.         return *this;
  40.     }
  41.     complex& operator*=(const complex& right) {
  42.         Re = (right.Re * Re) - (right.Im * Im);
  43.         Im = (right.Im * Re) + (right.Re * Im);
  44.         return *this;
  45.     }
  46.     complex& operator/=(const complex& right) {
  47.         double mult = (Re * Re) + (Im * Im);
  48.         Re = ((right.Re * Re) + (right.Im * Im)) / mult;
  49.         Im = ((right.Re * Im) - (right.Im * Re)) / mult;
  50.         return *this;
  51.     }
  52.  
  53.     const double arg();
  54.     const double mod()
  55.     {
  56.         return sqrt((Re*Re) + (Im*Im));
  57.     }
  58.  
  59.     complex(double _real = 0, double _noreal = 0):
  60.         Re(_real),
  61.         Im(_noreal) {}
  62. };
  63.  
  64. const double complex::arg() {
  65.     double result = 0;
  66.     double temp = atan2(Im, Re);
  67.  
  68.     if (Re == 0)
  69.         return Im > 0 ? M_PI / 2 : 3 * M_PI / 2;
  70.     if (Re > 0)
  71.         result = Im < 0 ? temp : (2 * M_PI) - abs(temp);
  72.     else
  73.         result = Im < 0 ? M_PI + abs(temp) :M_PI - abs(temp);
  74.    
  75.     while (result > 2 * M_PI)
  76.         result -= 2 * M_PI;
  77.  
  78.     return result;
  79. }
  80.  
  81. ostream& operator<< (ostream& out, const complex& num)
  82. {
  83.     out << num.Re << " ";
  84.     if (num.Im > 0) {
  85.         out << '+';
  86.     }
  87.     else {
  88.         out << '-';
  89.     }
  90.     out << " " << num.Im << 'i';
  91.     return out;
  92. }
  93.  
  94. int main()
  95. {
  96.     complex test1(4, 2);
  97.     complex test2(5, 1);
  98.     cout << "First: " << test1 << " Second: " << test2 << endl;
  99.     cout << "Binary + " << (test1 + test2) << endl;
  100.     cout << "Binary - " << (test1 - test2) << endl;
  101.     cout << "Binary * " << (test1 * test2) << endl;
  102.     cout << "Binary / " << (test1 / test2) << endl;
  103.     cout << "Binary first + with double 2: " << (test1 + 2.1) << endl;
  104.     cout << "Binary first - with double 2: " << (test1 - 2.2) << endl;
  105.     cout << "Binary first * with double 2: " << (test1 * 2.3) << endl;
  106.     cout << "Binary first / with double 2: " << (test1 / 2.5) << endl;
  107.     cout << "First mod = " << test1.mod() << endl;
  108.     auto x = (test1 + test2) = 3;
  109.  
  110.  
  111.         auto b = 2 + test1;
  112.  
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement