Margoshinka

complex

Dec 1st, 2021 (edited)
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.61 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4.  
  5. namespace WinFormsApp1
  6. {
  7.     class Complex
  8.     {
  9.         double r;//целая часть
  10.         double i;//мнимая часть
  11.         public double R
  12.         {
  13.             get { return r; }
  14.             set
  15.             {
  16.                
  17.                  r = value;
  18.             }
  19.  
  20.         }
  21.         public double I
  22.         {
  23.             get { return i; }
  24.             set
  25.             {
  26.                
  27.                  i = value;
  28.             }
  29.  
  30.         }
  31.         public Complex(double r, double i)
  32.         {
  33.             R = r;
  34.             I = i;
  35.  
  36.  
  37.         }
  38.         public Complex()
  39.         {
  40.             r = 0.0;
  41.             i = 0.0;
  42.  
  43.  
  44.         }
  45.         public static Complex Sum(Complex a, Complex b)
  46.         {
  47.             Complex res = new Complex();
  48.             res.r = a.r + b.r;
  49.             res.i = a.i + b.i;
  50.             return res;
  51.         }
  52.  
  53.         public static Complex Multiplication(Complex a, Complex b)
  54.         {
  55.             Complex res = new Complex();
  56.             res.r = a.r * b.r - a.i * b.i;
  57.             res.i = a.i * b.r + a.r * b.i;
  58.             return res;
  59.         }
  60.  
  61.         public static Complex Subtract(Complex a, Complex b)
  62.         {
  63.             Complex res = new Complex();
  64.             res.r = a.r - b.r;
  65.             res.i = a.i - b.i;
  66.             return res;
  67.         }
  68.         public static Complex Division(Complex a, Complex b)
  69.         {
  70.             Complex res = new Complex();
  71.             double D = b.r * b.r + b.i * b.i;
  72.             res.r = (a.r * b.r + a.i * b.i) / D;
  73.             res.i = (a.i * b.r - a.r * b.i) / D;
  74.             return res;
  75.         }
  76.         public static double Modul(Complex a)
  77.         {
  78.             double res = 0;
  79.             res = Math.Sqrt(Math.Pow(a.r, 2) + Math.Pow(a.i, 2));
  80.             return res;
  81.         }
  82.         public static Complex operator +(Complex a, Complex b)
  83.         {
  84.             return Complex.Sum(a, b);
  85.         }
  86.  
  87.         public static Complex operator -(Complex a, Complex b)
  88.         {
  89.             return Complex.Subtract(a, b);
  90.         }
  91.  
  92.         public static Complex operator *(Complex a, Complex b)
  93.         {
  94.             return Complex.Multiplication(a, b);
  95.         }
  96.         public static Complex operator /(Complex a, Complex b)
  97.         {
  98.             return Complex.Division(a, b);
  99.         }
  100.         public override string ToString()
  101.         { if (i>0 )
  102.             return r.ToString() + " + " + i.ToString() + "i";
  103.         else
  104.                 return r.ToString() + i.ToString() + "i";
  105.         }
  106.     }
  107. }
Add Comment
Please, Sign In to add comment