السلام عليكم ورحمة الله وبركاته
أود في هذا الطرح المشاركة بموضوع جمع وطرح الأعداد التركيبية

الاعداد التركيبية أو المعروفه باسم complex numbers هي اعداد تتركب من جزئين: جزء حقيقي وآخر خيالي
والصورة العامة لهذه الاعداد هي a + bi
حيث a هو عدد حقيقي و b هو العدد الخيالي . أما i فقيمتها الخيالية تكون الجذر التربيعي للسالب واحد
أي أن مربع i يساوي -1
قمت بتطبيق هذه الفكرة في برنامج واستخدمت طريقة object based programming
حيث عرفت class باسم Complex بهذا الشكل
public class Complex
{
private double realPart;
private double imaginaryPart;
public Complex(double real , double imaginary)
{
SetComplexNumber(real , imaginary);
}
// non-argument constructor
public Complex()
{
SetComplexNumber(0.0,0.0);
}
/// <summary>
/// Method SetComplexNumber to
/// initialize the values of realPart and imaginaryPart
/// </summary>
///
public void SetComplexNumber( double real , double imaginary )
{
RealPart = real;
ImaginaryPart = imaginary;
}
/// <summary>
/// Property RealPart
/// </summary>
public double RealPart
{
get
{
return realPart;
}
set
{
realPart = value;
}
}
/// <summary>
/// Property ImaginaryPart
/// </summary>
public double ImaginaryPart
{
get
{
return imaginaryPart;
}
set
{
imaginaryPart = value;
}
}
. . .
}ثم قمت بكتابة دوال لحساب نواتج الجمع والطرح
بافتراض ان العدد التركيبي الاول هو a + bi
والعددد التركيبي الثاني هو c + di
فعملية الجمع تكون كالآتي
(a+c) + ( b + d ) i
أي جمع الاعداد الحقيقية ثم جمع الاعداد الخيالية
أما عملية الطرح تكون كالتالي
(a – c ) + ( b – d ) i
أي بطرح الاعداد الحقيقة ثم طرح الاعداد الخيالية مع بعضها
وكانت كذلك
/// <summary>
/// Method Addition to add two complex numbers
/// </summary>
public void Addition(Complex a , Complex b)
{
double r , i;
r = a.RealPart + b.RealPart;
i = a.ImaginaryPart + b.ImaginaryPart;
SetComplexNumber(r,i);
}
/// <summary>
/// Method Subtraction to subtract two complex numbers
/// </summary>
/// <param name="a">the first complex number</param>
/// <param name="b">the second complex number</param>
///
public void Subtraction( Complex a , Complex b)
{
double r , i;
r = a.RealPart - b.RealPart;
i = a.ImaginaryPart - b.ImaginaryPart;
SetComplexNumber(r,i);
}ولكن لطباعة الاعداد على الصورة a + b i
قمت بكتابة دالة لطباعتها على هذه الصورة بحيث لو كان العدد التخيلي موجب ، يطبع العدد التركيبي بهذه الصورة
a + b i
أما إذا كان العدد التخيلي سالبا فيطبع العدد التركيبي بهذه الصورة a – b i
وإذا كان العدد التخيلي يساوي الصفر فيطبع فقط العدد الحقيقي
/// <summary>
/// Method Print to print Complex number in
/// the format a + bi
/// </summary>
/// <param name="real"></param>
/// <param name="imaginary"></param>
public string Print( double real , double imaginary)
{
if ( imaginary > 0.0 ) // positive number
return String.Format("{0} + {1} i " , real ,imaginary );
else
return String.Format("{0} - {1} i " , real , Math.Abs(imaginary));
if ( imaginary == 0.0 ) // print real number only
return String.Format("{0} " , real);
}وبهذا انتهى تعريف العدد التركيبي
ولاختبار هذا التعريف ، نقوم بعمل برنامج نستخدم فيه ثلاثة كائنات من نوع Complex
هم و complex 3 complex1 , complex 2
complex1 = new Complex(); complex2 = new Complex(); complex3 = new Complex();
ثم بتشغيل حدث لل Addition button
يتم تعيين قيمة العدد التركيبي الحقيقي الاول والعدد التركيبي التخيلي الاول للكائن complex1
ونفس الشي للكائن complex2
complex1.RealPart = Double.Parse(realTextBox1.Text); complex2.RealPart = Double.Parse(realTextBox2.Text); complex1.ImaginaryPart = Double.Parse(imaginaryTextBox1.Text); complex2.ImaginaryPart = Double.Parse(imaginaryTextBox2.Text);
وثم للعدد التركيبي الثالث complex3 يتم به تخزين ناتج الجمع أو الطرح
complex3.Addition(complex1,complex2);
ولطباعة الناتج في مربع النص الخاص بالنتيجة تكون التعليمة كالتالي
resultTextBox.Text = "[Addition] " + complex3.Print(complex3.RealPart ,complex3.ImaginaryPart);
وهذا كل شيء
إن أحسنت فأعينوني وإن أخطأت فقوموني
وعلى الخير والمحبة نلتقي
