[rwadaltechnia] الدرس الثامن

  • From: mohamed bashir <bashir633@xxxxxxxxx>
  • To: rwadaltechnia <rwadaltechnia@xxxxxxxxxxxxx>
  • Date: Mon, 24 Dec 2018 12:22:38 +0900

الأخوة الكرام السلام عليكم ورحمة الله
هذا هو كود الدرس الثامن من دروس لغة c#
الرجاء التأكد من صحته
وإذا كات هنالك أي أسألة فأنا على أتم الاستعداد للإجابة عليها بقدر الاستطاع
ولمعرفة المزيد عن هذا الدرس الرجاء الذهاب إلى الرابط التالي
http://bashir.taibat.com/cl8.php
ولكم مني خالص الحب والتقدير
using System;
 using System.Drawing;
 using System.Windows.Forms;


 /********************
إطار  Constants
خاص بكتابة الثوابت
********************/
static class Constants
 {
 public const String ApplicationName = "الآلة الحاسبة"; //اسم البرنامج
public const String TextBoxFontName = "Times New Roman"; // نوع الحرف لمربع 
التحرير
public const int TextBoxFontSize = 31; //حجم الخط
public const int TextBoxMaxLength = 10; //عدد الأرقام التي يكمن إدخالها 
public const int TextBoxWidth = 300; //عرض ةربع التحرير
public const int TextBoxHeight = 50; // إرتفاع ةربع التحرير
public const String ButtonFontName = "Times New Roman"; // نوع الحرف للأزرار
public const int ButtonFontSize = 30; // حجم خط الأزرار
public const int ButtonWidth = 150; // عرض الأزرار
public const int ButtonHeight = TextBoxHeight; // إرتفاع الأزرار
public const String EqualButtonLength = "يساوي"; // اسم زر يساوي
public const String ClearButtonLength = "مسح"; // الاسم الذي يظهر على زر تفريغ 
مربع التحرير
}


 /********************
إطار Form1
يتم تفيذ البرنامج من هذا الإطار
********************/
class test
 {
 public static void Main()
 {
 Application.Run(new Form1());
 }
 }


 /********************
إطار Form1
إمتداد للإطار السابق
********************/
class Form1 : Form
 {
 private double answer_number; // متغير لحفظ نتيجة العمليات الحسابية
private TextBox InputTextBox;

public Form1()
 {
FormDesign();// حزمة خاصة لتحديد تصميم البرنامج
Reset();// حزمة خاصة لإعادة الإعدادات
 }

 /* حزمة FormDesign */
 /* تصميم النافذة */
public void FormDesign()
 {
 StartPosition = FormStartPosition.CenterScreen;//جعل شاشة البرنامج في منتصف 
الشاشة

 MaximizeBox = false; //تعطيل خاصية تكبير النافذة
FormBorderStyle = FormBorderStyle.Fixed3D; // تعطيل تغيير حجم الشاشة

Text = Constants.ApplicationName;// اسم البرنامج
 BackColor = SystemColors.Window;// لون خلفية البرنامج
 ClientSize = new Size(Constants.TextBoxWidth + (Constants.ButtonWidth *2), 
Constants.ButtonHeight);// حجم شاشة البرنامج . وهي بمقدار عرض مربع التحرير زائد 
حجم الزر مضروبا  في 2 وبإرتفاع مطابق لإرتفاع الأزرار. وعلمة * تعني علامة الضرب 
في معظم لغات البرمجة

 // إنشاء مربع التحرير
InputTextBox = new TextBox(); // إنشاء مربع تحرير باسم InputTextBox
 InputTextBox.Font = new Font(Constants.TextBoxFontName, 
Constants.TextBoxFontSize); // نوع وحجم الحرف
 InputTextBox.Width = Constants.TextBoxWidth; // العرض
 InputTextBox.Height = Constants.TextBoxHeight; // الارتفاع
 InputTextBox.Multiline = false; // تعطيل خاصية تعدد الأسطر
InputTextBox.TextAlign = HorizontalAlignment.Left; // تحديد مكانه إلى اليسار
InputTextBox.MaxLength = Constants.TextBoxMaxLength; // عدد الحروف والأرقام 
التي يمكن إدخالها
 InputTextBox.Parent = this;
 InputTextBox.Location = new Point(0, 0); // تعيين الحيز
 InputTextBox.TextChanged += new EventHandler(InputTextChanged); // خاصية 
للتحقق عند حدوث أي تغيير
 InputTextBox.TabIndex = 0; // جعلها أول عنصر يظهر في الشاشة تحديد الترتيب 0 
يعني الأول
 InputTextBox.TabStop = true; // السماح للإنتقال باستخدام مفتاح الحقول

 // إنشاء زر يساوي
Button EqualButton = new Button(); // إنشاء زر باسم EqualButton
 EqualButton.Font = new Font(Constants.ButtonFontName, 
Constants.ButtonFontSize); // نوع وحجم الخط
 EqualButton.TextAlign = ContentAlignment.MiddleCenter; // في مركز الوسط
EqualButton.Width = Constants.ButtonWidth; // العرض
 EqualButton.Height = Constants.ButtonHeight; // الإرتفاع
 EqualButton.Text = Constants.EqualButtonLength; // الاسم الذي يظهر على زر يساوي
 EqualButton.BackColor = SystemColors.Control; // الخلفية
 EqualButton.Parent = this;
 EqualButton.Location = new Point(Constants.TextBoxWidth, 0); // الحيز
 EqualButton.Click += new EventHandler(ButtonClick); // عند الضغط على زر يساوي
 EqualButton.TabIndex = 1; // ترتيب العنصر الثاني
 EqualButton.TabStop = true; // السماح بالإنتقال باستخدام مفتاح الحقول

 // إنشاء زر الحزف
Button ClearButton = new Button(); // إنشاء زر باسم ClearButton
 ClearButton.Font = new Font(Constants.ButtonFontName, 
Constants.ButtonFontSize); // نوع وحجم الحرف
 ClearButton.TextAlign = ContentAlignment.MiddleCenter; // تحديد  الموقع بمركز 
الوسط
ClearButton.Width = Constants.ButtonWidth; // العرض
 ClearButton.Height = Constants.ButtonHeight; // الإرتفاع
 ClearButton.Text = Constants.ClearButtonLength; // اسم ور المسح
 ClearButton.BackColor = SystemColors.Control; // لون الخلفية
 ClearButton.Parent = this;
 ClearButton.Location = new Point(Constants.TextBoxWidth + 
Constants.ButtonWidth, 0); // الحيز
 ClearButton.Click += new EventHandler(ButtonClick); // عند الضغط على زر المسح
 ClearButton.TabIndex = 2; // الترتيب الثالث
 ClearButton.TabStop = true; // hالسماح للإنتقال باستخدام مفتاح الحقول
 }

/* حزمة إعادة الضبط*/
/* إعادة قيم المتغيرات*/
public void Reset()
 {
 answer_number = 0; //جعل متغير النتيجة  في القيمة 0
 InputTextBox.Text = ""; // تفريغ مربع التحرير
 }

 /* عند حدوث تغيير في مربع التحرير*/
 /* التحقق باستخدام خاصية InputTextChanged */
public void InputTextChanged(object sender, EventArgs e)
 {
 TextBox textbox = (TextBox)sender;
 String check_string = "0123456789.+-*/"; // ابأشياء المسموح بإدخالها في مربع 
التحرير. والمسموح به هو الأرقام من 0 إلى 9 والعلامات زائد وناقص وفي وعلى
String checked_string = String.Empty;
 char get_character = '\0';
 char error_character = '\0';
 bool check_flag = true;

 for (int i=0; i<textbox.Text.Length; i++) // إعادة تكرار الأرقام التي تم 
إدخالها في مربع التحرير
{
get_character = textbox.Text[i];
 check_flag = false;
 for (int j=0; j<check_string.Length; j++) // إعادة تكرار الأرقام المسموح 
بكتابتها فقط
{
if (get_character == check_string[j]) // التأكد إذا كان ما تم إخاله صحيحا
{
check_flag = true;
 break;
 }
 }
 if (check_flag) checked_string += get_character;
 else error_character = get_character;
 }

 if (textbox.Text.Length == checked_string.Length) return;
 InputTextBox.Text = checked_string;
 MessageBox.Show("لا يمكن إدخال الحرف " + error_character, 
Constants.ApplicationName, MessageBoxButtons.OK, MessageBoxIcon.Information);
 }

 /* عند الضغط على الأزرار*/
public void ButtonClick(object sender, EventArgs e)
 {
 Button button = (Button)sender; // التعرف على نوع الزر الذي تم الضغط عليه

if (button.Text == Constants.EqualButtonLength) // إذا كان زر يساوي
 Calculation(); // الذهاب إلى حزمة إجراء العمليات الحسابية
 else if (button.Text == Constants.ClearButtonLength) // أما إذا كان زر المسح
 Reset(); // الذهاب إلى حزمة إعادة الضبط
 else // وإذا لم يكن الأمر مطابقا للحالتين السابقتين
 MessageBox.Show("هنالك خطأ ا في الضغط على الأزرار", Constants.ApplicationName, 
MessageBoxButtons.OK, MessageBoxIcon.Information);

 InputTextBox.Focus(); // جعل المؤشر على عنصر مربع التحرير
 }

 /* حزمة إجراء العمليات الحسابية Calculation */
 /* التعرف على محتوى مربع التحرير ومن ثم إجراء العمليات */
public void Calculation()
 {
 int phase = 0; // 0 استعداد. و 1 تعني مكتوب الأرقام يسار العلامة فقط. و 2 تعني 
مكتوب حتى علامة الحساب. و 3 تعني مكتوب الأرقام يمين العلامة الحسابية
char input_operator = '\0'; // نوع العلامة الحسابية
double left_number = '\0'; // الأرقام يسار العلامة الحسابية
double right_number = '\0'; // الأرقام يمين العلامة الحسابية
String buffer_string = String.Empty; // لحفظ الأرقام التي تم إدخالها

for (int i=0; i<InputTextBox.Text.Length; i++) // إعادة تكرار الحروف التي تم 
إدخالها في مربع التحرير
{
char get_character = InputTextBox.Text[i];
 if (Char.IsDigit(get_character) || get_character == '.') // في حالة الأرقام 
الصحيحة أو الكسرية
{
if (phase == 0 || phase == 2) phase += 1;
 if (get_character == '.') // التأكد ن وجود نفس العلامة لأكثر من مرة
{
for (int j=0; j<buffer_string.Length; j++) // إعادة تكرار الأرقام التي تم حفظها 
في المتغير
{
if (buffer_string[j] == '.') // في حالة الكسور
{
MessageBox.Show("تكرار في الكسور", Constants.ApplicationName, 
MessageBoxButtons.OK, MessageBoxIcon.Information);
 return;
 }
 }
 }
 buffer_string += get_character; // حفظ موقت في المتغير ريثما يتم التعرف على 
الأرقام يمين ويسار العلامة الحسابية
}
else if (get_character == '+' || get_character == '-' || get_character == '*' 
|| get_character == '/')
 {
 if (phase == 1) // في حالة كتابة الأرقام يسار العلامة الحسابية
{
phase += 1;
 left_number = double.Parse(buffer_string);
 buffer_string = String.Empty;
 input_operator = get_character;
 }
 else if (buffer_string.Length == 0)
 {
 MessageBox.Show("لا توجد أرقام يسار العلامة الحسابية", 
Constants.ApplicationName, MessageBoxButtons.OK, MessageBoxIcon.Information);
 return;
 }
 else
 {
 MessageBox.Show("لا يمكن إجراء أكثر من عملية في وقت واحد", 
Constants.ApplicationName, MessageBoxButtons.OK, MessageBoxIcon.Information);
 return;
 }
 }
 else
 {
 MessageBox.Show("لا يمكن كتابة " + get_character, Constants.ApplicationName, 
MessageBoxButtons.OK, MessageBoxIcon.Information);
 return;
 }
 }

 if (phase != 3) // في حالة عدم كتابة الأرقام يمين العلامة الحسابية
{
MessageBox.Show("الرجاء كتابة الصيغة بصورة صحيحة", Constants.ApplicationName, 
MessageBoxButtons.OK, MessageBoxIcon.Information);
 return;
 }

 right_number = double.Parse(buffer_string);

 switch(input_operator)
 {
 case '+': // في حالة الجمع
 answer_number = left_number + right_number;
 break;
 case '-': // في حالة الطرح
 answer_number = left_number - right_number;
 break;
 case '*': // في حالة الضرب
 answer_number = left_number * right_number;
 break;
 case '/': // في حالة القسمة
 answer_number = left_number / right_number;
 break;
 default:
 answer_number = 0;
 break;
 }

 InputTextBox.Text = Convert.ToString(answer_number); // إدخال النتيجة في مربع 
التحرير 
 }
 } 

Other related posts: