Advertisement
Savonarolla

Untitled

Dec 4th, 2020
859
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.52 KB | None | 0 0
  1. using System;
  2. namespace Overload
  3. {
  4.     public class Complex
  5.     {
  6.         private int Re;
  7.         private int Im;
  8.  
  9.         public int A
  10.         {
  11.             get
  12.             {
  13.                 return Re;
  14.             }
  15.             set
  16.             {
  17.                 Re = value;
  18.             }
  19.         }
  20.        
  21.         public int B
  22.         {
  23.             get
  24.             {
  25.                 return Im;
  26.             }
  27.             set
  28.             {
  29.                 Im = value;
  30.             }
  31.         }
  32.  
  33.         public Complex()
  34.         {
  35.             Re = 0;
  36.             Im = 0;
  37.         }
  38.  
  39.         public Complex(int num)
  40.         {
  41.             Re = num;
  42.             Im = num;
  43.         }
  44.        
  45.         public Complex(int real, int imaginary)
  46.         {
  47.             Re = real;
  48.             Im = imaginary;
  49.         }
  50.        
  51.         // 1) Вычетание (Complex - Complex)
  52.         // 2) Вычетание (Number  - Complex)
  53.         // 3) Умножение (Complex * Complex)
  54.         // 4) Умножение (Number  * Complex)
  55.         // 5) Деление   (Complex / Complex)
  56.  
  57.         // 1
  58.         public static Complex operator -(Complex x, Complex y)
  59.         {
  60.             int zx, zy;
  61.             zx = x.A - y.A;
  62.             zy = x.B - y.B;
  63.             Complex z = new Complex(zx,zy);
  64.             return z;
  65.         }
  66.         // 2
  67.         public static Complex operator -(int w, Complex x)
  68.         {
  69.             Complex y = new Complex(w);
  70.             int zx, zy;
  71.             zx = x.A - y.A;
  72.             zy = x.B - y.B;
  73.             Complex z = new Complex(zx,zy);
  74.             return z;
  75.         }
  76.         // 2.5
  77.         public static Complex operator -(Complex x, int w)
  78.         {
  79.             Complex y = new Complex(w);
  80.             int zx, zy;
  81.             zx = x.A - y.A;
  82.             zy = x.B - y.B;
  83.             Complex z = new Complex(zx,zy);
  84.             return z;
  85.         }
  86.         // 3
  87.         public static Complex operator *(Complex x, Complex y)
  88.         {
  89.             int zx, zy;
  90.             zx = x.A * y.A - x.B * y.B;
  91.             zy = x.A * y.B + x.B * y.A;
  92.             Complex z = new Complex(zx,zy);
  93.             return z;
  94.         }
  95.         // 4
  96.         public static Complex operator *(int w, Complex y)
  97.         {
  98.             Complex x = new Complex(w);
  99.             int zx, zy;
  100.             zx = x.A * y.A - x.B * y.B;
  101.             zy = x.A * y.B + x.B * y.A;
  102.             Complex z = new Complex(zx,zy);
  103.             return z;
  104.         }
  105.         // 5
  106.         public static Complex operator /(Complex x, Complex y)
  107.         {
  108.             int zx, zy;
  109.             zx = (x.A * y.A + x.B * y.B) / ((y.A * y.A) + (y.B * y.B));
  110.             zy = (y.A * x.B - x.A * y.B) / ((y.A * y.A) + (y.B * y.B));
  111.             Complex z = new Complex(zx,zy);
  112.             return z;
  113.         }
  114.        
  115.         public static bool operator ==(Complex x, Complex y)
  116.         {
  117.             if (x.A == y.A && x.B == y.B)
  118.             {
  119.                 return true;
  120.             }
  121.             else return false;
  122.         }
  123.         public static bool operator !=(Complex x, Complex y)
  124.         {
  125.             if (x.A != y.A || x.B != y.B)
  126.             {
  127.                 return true;
  128.             }
  129.             else return false;
  130.         }
  131.        
  132.         public override string ToString()
  133.         {
  134.             return string.Format(A + "+" + B + "i");
  135.         }
  136.        
  137.         public void ShowComplex()
  138.         {
  139.             Console.WriteLine(A + "+" + B + "i");
  140.         }
  141.        
  142.  
  143.     }
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement