
// Form1 sınıfı : using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Numerics; namespace C_Sharp_Kompleks_Sayilar_Aritmatik { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { ComplexNumber a = new ComplexNumber(-4,-2); ComplexNumber b = new ComplexNumber(2, 1); ComplexNumber c = new ComplexNumber(2, 1); listBox1.Items.Add(a.ToString()); listBox1.Items.Add(b.ToString()); listBox1.Items.Add((a + b).ToString()); listBox1.Items.Add((a * b).ToString()); listBox1.Items.Add((a * 2).ToString()); listBox1.Items.Add((-1 * b).ToString()); listBox1.Items.Add((a / b).ToString()); listBox1.Items.Add((a / 2).ToString()); listBox1.Items.Add((2 / b).ToString()); listBox1.Items.Add(b.Equals(c).ToString()); listBox1.Items.Add((a == b).ToString()); listBox1.Items.Add((b != c).ToString()); } } } // Komleks Sınıfı using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Numerics; namespace C_Sharp_Kompleks_Sayilar_Aritmatik { class ComplexNumber { private double realValue; private double imaginaryValue; public double RealValue { get { return realValue; } set { realValue = value; } } public double ımageValue { get { return imaginaryValue; } set { imaginaryValue = value; } } public ComplexNumber(double real, double image) { realValue = real; imaginaryValue = image; } public ComplexNumber() { realValue = 0; imaginaryValue = 0; } public ComplexNumber(double real) { realValue = real; imaginaryValue = 0; } public ComplexNumber(ComplexNumber complexNumber) { realValue = complexNumber.realValue; imaginaryValue = complexNumber.imaginaryValue; } ////// komplek sayı değerini (a+bi) formatında döner /// /// public override string ToString() { if (this.imaginaryValue < 0) { return this.realValue + "-" + -1 * this.imaginaryValue + "i"; } else { return this.realValue + "+" + this.imaginaryValue + "i"; } } ////// Karşılaştırılan sayılar birbirine eşitse true döner, değilse false. /// /// /// public override bool Equals(object obj) { ComplexNumber obrCompare = (ComplexNumber)obj; if (this.ımageValue == obrCompare.imaginaryValue && this.realValue == obrCompare.realValue) { return true; } else { return false; } } /// < summary > /// İki kompleks sayının toplamını hesaplayan metod /// /// < param name="sayi1" > /// < param name="sayi2" > /// < returns > public static ComplexNumber operator +(ComplexNumber sayi1, ComplexNumber sayi2) { double gercekToplam = sayi1.realValue + sayi2.realValue; double sanalToplam = sayi2.realValue + sayi2.realValue; return new ComplexNumber(gercekToplam, sanalToplam); } /// < summary > /// Bir kompleks sayı ile bir kompleks olmayan sayının toplamını hesaplayan metod /// /// < param name="sayi1" > /// < param name="sayi2" > /// < returns > public static ComplexNumber operator +(ComplexNumber sayi1, double sayi2) { double gercekToplam = sayi1.realValue + sayi2; return new ComplexNumber(gercekToplam, sayi1.imaginaryValue); } /// < summary > /// Bir kompleks sayı ile bir kompleks olmayan sayının toplamını hesaplayan metod /// /// < param name="sayi1" > /// < param name="sayi2" > /// < returns > public static ComplexNumber operator +(double sayi1, ComplexNumber sayi2) { double gercekToplam = sayi2.realValue + sayi1; return new ComplexNumber(gercekToplam, sayi2.imaginaryValue); } /// < summary > /// İki kompleks sayının çarpımını hesaplayan metod /// /// < param name="sayi1" > /// < param name="sayi2" > /// < returns > public static ComplexNumber operator*(ComplexNumber sayi1, ComplexNumber sayi2) { double gercekCarpma = 0; double sanalCarpma = 0; gercekCarpma = sayi1.realValue * sayi2.realValue - sayi1.imaginaryValue * sayi2.imaginaryValue; sanalCarpma = sayi1.realValue * sayi2.imaginaryValue + sayi1.imaginaryValue * sayi2.realValue; return new ComplexNumber(gercekCarpma, sanalCarpma); } /// < summary > /// Bir kompleks sayı iel bir kompleks olmayan sayının çarpımını hesaplayan metod /// /// < param name="sayi1" > /// < param name="sayi2" > /// < returns > public static ComplexNumber operator *(ComplexNumber sayi1, double sayi2) { return sayi1 * (new ComplexNumber(sayi2)); } /// < summary > /// Bir kompleks sayı iel bir kompleks olmayan sayının çarpımını hesaplayan metod /// /// < param name="sayi1" > /// < param name="sayi2" > /// < returns > public static ComplexNumber operator *(double sayi1, ComplexNumber sayi2) { return sayi2 * (new ComplexNumber(sayi1)); } /// < summary > /// İki kompleks sayının bölümünü hesaplayan metod /// /// < param name="sayi1" > /// < param name="sayi2" > /// < returns > public static ComplexNumber operator /(ComplexNumber sayi1, ComplexNumber sayi2) { ComplexNumber eslenik = new ComplexNumber(sayi2.realValue, -sayi2.imaginaryValue); ComplexNumber pay = sayi1 * eslenik; double payda = sayi2.realValue * sayi2.realValue + sayi2.imaginaryValue * sayi2.imaginaryValue; return new ComplexNumber(pay.realValue / payda, pay.imaginaryValue / payda); } /// < summary > /// bir kompleks sayının bir kompleks olmayan sayıya bölümünü hesaplayan metod /// /// < param name="sayi1" > /// < param name="sayi2" > /// < returns > public static ComplexNumber operator /(ComplexNumber sayi1, double sayi2) { return sayi1 / (new ComplexNumber(sayi2)) ; } /// < summary > /// Bir kompleks olmayan sayının bir kompleks sayıya bölümünü hesaplayan metod /// /// < param name="sayi1" > /// < param name="sayi2" > /// < returns > public static ComplexNumber operator /(double sayi1, ComplexNumber sayi2) { return (new ComplexNumber(sayi1)) / sayi2; } /// < summary > /// iki kompleks sayının birbirne eşit olup olmadığını hesaplayan metod /// /// < param name="sayi1" > /// < param name="sayi2" > /// < returns > public static bool operator ==(ComplexNumber sayi1, ComplexNumber sayi2) { if (sayi1.imaginaryValue == sayi2.imaginaryValue && sayi1.realValue == sayi2.realValue) { return true; } else { return false; } } /// < summary > /// iki kompleks sayının birbirne eşit olup olmadığını hesaplayan metod /// /// < param name="sayi1" > /// < param name="sayi2" > /// < returns > public static bool operator !=(ComplexNumber sayi1, ComplexNumber sayi2) { if (sayi1.imaginaryValue == sayi2.imaginaryValue && sayi1.realValue == sayi2.realValue) { return false; } else { return true; } } /// < summary > /// Biliçsiz biçimde yapılan double tipine dönüştürme işlemi /// /// < param name="complexnumber" > /// < returns > public static implicit operator double(ComplexNumber complexnumber) { return complexnumber.realValue; } /// < summary > /// Biliçsiz biçimde yapılan int tipine dönüştürme işlemi /// /// < param name="complexnumber" > /// < returns > public static implicit operator int(ComplexNumber complexnumber) { return (int)complexnumber.realValue; } } }
0 yorum