Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.95 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace _04
  8. {
  9.     class Zespolona
  10.     {
  11.         decimal Rzeczywista, Urojona;
  12.  
  13.         public Zespolona(decimal one, decimal two)
  14.         {
  15.             Rzeczywista = one;
  16.             Urojona = two;
  17.         }
  18.  
  19.             public static Zespolona operator +(Zespolona z1, Zespolona z2)
  20.             {
  21.                 return new Zespolona(z1.Rzeczywista + z2.Rzeczywista, z1.Urojona + z2.Urojona);
  22.             }
  23.  
  24.         public static Zespolona operator -(Zespolona z1, Zespolona z2)
  25.         {
  26.             return new Zespolona(z1.Rzeczywista - z2.Rzeczywista, z1.Urojona - z2.Urojona);
  27.         }
  28.  
  29.             public static Zespolona operator *(Zespolona z1, Zespolona z2)
  30.             {
  31.                 return new Zespolona((z1.Rzeczywista*z2.Rzeczywista-z1.Urojona*z2.Urojona),(z1.Urojona*z2.Rzeczywista+z1.Rzeczywista*z2.Urojona));
  32.             }
  33.  
  34.         public static Zespolona operator /(Zespolona z1, Zespolona z2)
  35.         {
  36.             if(z2.Rzeczywista==0&&z2.Urojona==0)
  37.             {
  38.                 throw new System.DivideByZeroException("Nie można dzielić przez 0.");
  39.             }
  40.             else
  41.             return new Zespolona(z2.Rzeczywista*z2.Rzeczywista + z2.Urojona * z2.Urojona,(z1.Rzeczywista * z2.Rzeczywista - z1.Urojona * z2.Urojona) +(z1.Urojona * z2.Rzeczywista - z1.Rzeczywista * z2.Urojona));
  42.         }
  43.  
  44.             public static bool operator !=(Zespolona z1, Zespolona z2)
  45.             {
  46.                 bool stat = false;
  47.                 if(z1.Rzeczywista!=z2.Rzeczywista||z1.Urojona!=z2.Urojona)
  48.                 {
  49.                     stat = true;  
  50.                 }
  51.                 return stat;
  52.             }
  53.  
  54.         public static bool operator ==(Zespolona z1, Zespolona z2)
  55.         {
  56.             bool stat = false;
  57.             if (z1.Rzeczywista == z2.Rzeczywista || z1.Urojona == z2.Urojona)
  58.             {
  59.                 stat = true;
  60.             }
  61.             return stat;
  62.         }
  63.  
  64.             public static bool operator >(Zespolona z1, Zespolona z2)
  65.             {
  66.                 bool stat = false;
  67.                 if (z1.Rzeczywista > z2.Rzeczywista || z1.Urojona > z2.Urojona)
  68.                 {
  69.                     stat = true;
  70.                 }
  71.                 return stat;
  72.             }
  73.  
  74.         public static bool operator <(Zespolona z1, Zespolona z2)
  75.         {
  76.             bool stat = false;
  77.             if (z1.Rzeczywista < z2.Rzeczywista || z1.Urojona< z2.Urojona)
  78.             {
  79.                 stat = true;
  80.             }
  81.             return stat;
  82.         }
  83.  
  84.                             public String getUro
  85.                             {
  86.                                 get { return Convert.ToString(Urojona); }
  87.                             }
  88.  
  89.                             public String getRze
  90.                             {
  91.                                 get { return Convert.ToString(Rzeczywista); }
  92.                             }
  93.         public static explicit operator decimal(Zespolona a)
  94.         {
  95.             return a.Rzeczywista;
  96.         }
  97.         public static implicit operator Zespolona(decimal a)
  98.         {
  99.             return new Zespolona(a,a);
  100.         }
  101.  
  102.         public decimal this[int nr]
  103.         {
  104.             get {
  105.                 if (nr==0)
  106.                 {
  107.                     return Rzeczywista;
  108.                 }
  109.                 else if (nr==1)
  110.                 {
  111.                     return Urojona;
  112.                 }
  113.                 return 0;
  114.             }
  115.         }
  116.  
  117.         public decimal this[string a]
  118.         {
  119.             get
  120.             {
  121.                 if(a.Equals("R"))
  122.                 {
  123.                     return Rzeczywista;
  124.                 }
  125.                 else if(a.Equals("I"))
  126.                 {
  127.                     return Urojona;
  128.                 }
  129.                 return 0;
  130.             }
  131.         }
  132.     }
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement