Advertisement
wowonline

Untitled

Apr 7th, 2022
1,123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.42 KB | None | 0 0
  1. #include <string>
  2. #include <iostream>
  3. #include <cmath>
  4.  
  5. namespace numbers {
  6.  
  7. class complex {
  8.  
  9. public:
  10.     double imag, real;
  11.  
  12.     complex() {
  13.         real = 0;
  14.         imag = 0;
  15.     }
  16.  
  17.     complex(double r, double i = 0.0) {
  18.         real = r;
  19.         imag = i;
  20.     }
  21.  
  22.     explicit complex(const std::string str) {
  23.         sscanf(str.c_str(), "(%lf,%lf)", &real, &imag);
  24.     }
  25.  
  26.     double re() const
  27.     {
  28.         return real;
  29.     }
  30.  
  31.     double im() const
  32.     {
  33.         return imag;
  34.     }
  35.  
  36.     double abs2() const
  37.     {
  38.         return real * real + imag * imag;
  39.     }
  40.  
  41.     double abs() const
  42.     {
  43.         return sqrt(abs2());
  44.     }
  45.  
  46.     std::string to_string() const
  47.     {
  48.         char buf[100];
  49.         snprintf(buf, 100, "(%.10g,%.10g)", real, imag);
  50.         return (std::string) buf;
  51.     }
  52.  
  53.     complex operator+=(complex a, complex b);
  54.     complex operator-=(complex a, complex b);
  55.     complex operator*=(complex a, complex b);
  56.     complex operator/=(complex a, complex b);
  57.     complex operator+(complex a, complex b) ;
  58.     complex operator-(complex a, complex b) ;
  59.     complex operator*(complex a, complex b) ;
  60.     complex operator/(complex a, complex b) ;
  61.     complex operator~(complex a) ;
  62.     complex operator-(complex a) ;
  63.  
  64. };
  65.  
  66.     complex operator+=(complex a, complex b)
  67.     {
  68.         a.real += b.re();
  69.         a.imag += b.im();
  70.         numbers::complex tmp(a.real, a.imag);
  71.         return tmp;
  72.     }
  73.  
  74.     complex operator-=(complex a, complex b)
  75.     {
  76.         a.real -= b.re();
  77.         a.imag -= b.im();
  78.         numbers::complex tmp(a.real, a.imag);
  79.         return tmp;
  80.     }
  81.  
  82.     complex operator*=(complex a, complex b)
  83.     {
  84.         //(a+bi)(c+di) = (ac-bd)+(ad+bc)i
  85.         double tmp_real = a.real * b.re() - a.imag * b.im();
  86.         double tmp_imag = a.real * b.im() + a.imag * b.re();
  87.         a.real = tmp_real;
  88.         a.imag = tmp_imag;
  89.         numbers::complex tmp(a.real, a.imag);
  90.         return tmp;
  91.     }
  92.  
  93.     complex operator/=(complex a, complex b)
  94.     {
  95.         /*
  96.             a - real
  97.             b - imag
  98.             c - b.re()
  99.             d - b.im()
  100.         */
  101.         //(a+bi)(c+di) = ((ac+bd) / (c^2 + d^2)) + ((bc - ad) / (c^2 + d^2))i
  102.         double tmp_real = (a.real * b.re() + a.imag * b.im()) / (b.re() * b.re() + b.im() * b.im());
  103.         double tmp_imag = (a.imag * b.re() - a.real * b.im()) / (b.re() * b.re() + b.im() * b.im());
  104.         a.real = tmp_real;
  105.         a.imag = tmp_imag;
  106.         numbers::complex tmp(a.real, a.imag);
  107.         return tmp;
  108.     }
  109.  
  110.     complex operator+(complex a, complex b)
  111.     {
  112.         numbers::complex c(a.real, a.imag);
  113.         return c+=b;
  114.     }
  115.  
  116.     complex operator-(complex a, complex b)
  117.     {
  118.         numbers::complex c(a.real, a.imag);
  119.         return c-=b;
  120.     }
  121.  
  122.     complex operator*(complex a, complex b)
  123.     {
  124.         numbers::complex c(a.real, a.imag);
  125.         return c*=b;
  126.     }
  127.  
  128.     complex operator/(complex a, complex b)
  129.     {
  130.         numbers::complex c(a.real, a.imag);
  131.         return c/=b;
  132.     }
  133.  
  134.     complex operator~(complex a)
  135.     {
  136.         numbers::complex c(a.real, -a.imag);
  137.         return c;
  138.     }
  139.  
  140.     complex operator-(complex a)
  141.     {
  142.         numbers::complex c(-a.real, -a.imag);
  143.         return c;
  144.     }
  145.  
  146. }
  147.  
  148. // int main()
  149. // {
  150. //     numbers::complex c1("(1.5656, 2.213)"), c2("(56, 3)");
  151.    
  152. //     std::cout << (c1 + c2).im() <<" "<<c1.im();
  153. // }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement