إنشاء التقارير
جزء من قسم المنطق وتدفق التنفيذ في رحلة C# على Coddy. الدرس 44 من 66.
التحدي
متوسطالآن بعد أن أصبح لدينا نظام التقييم، لننشئ وحدة لإعداد التقارير تُنشئ تقارير منسقة عن أداء الطلاب. أنشئ فئة باسم ReportGenerator تتضمن هذه الأساليب:
GenerateStudentReport(int[][] scoreGrid, int studentIndex): تُرجع سلسلة منسقة تعرض درجات طالب محدد ومتوسطه وتقديره بالحروف.- التنسيق: "Student #X | Average: YY.Y | Grade: Z\nAssignment scores: S1, S2, S3, ..." حيث تمثل S1 وS2 وS3، ... الدرجات الرقمية لكل واجب.
- بالنسبة إلى الواجبات التي لم تُقيَّم (درجتها -1)، اعرض "N/A" بدلًا من الدرجة.
- أعِد "Invalid student index" إذا كان فهرس الطالب غير صالح.
GenerateClassSummary(int[][] scoreGrid): تُرجع سلسلة منسقة تلخص أداء الصف.- التنسيق: "Class Summary\nTotal Students: X\nClass Average: YY.Y\nGrade Distribution: A: #, B: #, C: #, D: #, F: #"
GenerateAssignmentReport(int[][] scoreGrid, int assignmentIndex): تُرجع سلسلة منسقة تتضمن إحصاءات عن واجب محدد.- التنسيق: "Assignment #X | Average: YY.Y | Completion Rate: Z%"
- معدل الإكمال هو النسبة المئوية للطلاب الذين لديهم درجة لهذا الواجب (ليست -1).
- أعِد "Invalid assignment index" إذا كان فهرس الواجب غير صالح.
جرّب بنفسك
using System; // لا تحذف هذا السطر
using System.Text;
public class ReportGenerator
{
public static string GenerateStudentReport(int[][] scoreGrid, int studentIndex)
{
// اكتب الكود الخاص بك هنا
}
public static string GenerateClassSummary(int[][] scoreGrid)
{
// اكتب الكود الخاص بك هنا
}
public static string GenerateAssignmentReport(int[][] scoreGrid, int assignmentIndex)
{
// اكتب الكود الخاص بك هنا
}
}
// مطلوب للاختبار - لا تعدّله
public class DataCollector
{
public static int[][] CreateScoreGrid(int students, int assignments)
{
int[][] scoreGrid = new int[students][];
for (int i = 0; i < students; i++)
{
scoreGrid[i] = new int[assignments];
}
return scoreGrid;
}
public static bool ValidateScore(int score)
{
return score >= 0 && score <= 100;
}
public static int[][] PopulateWithDefaultValues(int[][] scoreGrid)
{
for (int i = 0; i < scoreGrid.Length; i++)
{
for (int j = 0; j < scoreGrid[i].Length; j++)
{
scoreGrid[i][j] = -1;
}
}
return scoreGrid;
}
}
public class DataEntry
{
public static int SetStudentScore(int[][] scoreGrid, int studentIndex, int assignmentIndex, int score)
{
// تحقق مما إذا كانت الفهارس خارج الحدود
if (studentIndex < 0 || studentIndex >= scoreGrid.Length ||
assignmentIndex < 0 || assignmentIndex >= scoreGrid[studentIndex].Length)
{
return -1;
}
// تحقق من صحة الدرجة
if (!DataCollector.ValidateScore(score))
{
return -2;
}
// عيّن الدرجة
scoreGrid[studentIndex][assignmentIndex] = score;
return 0;
}
public static int UpdateAllScores(int[][] scoreGrid, int[] studentIndices, int assignmentIndex, int score)
{
int successCount = 0;
for (int i = 0; i < studentIndices.Length; i++)
{
int result = SetStudentScore(scoreGrid, studentIndices[i], assignmentIndex, score);
if (result == 0)
{
successCount++;
}
}
return successCount;
}
}
public class DataAnalyzer
{
public static double CalculateStudentAverage(int[][] scoreGrid, int studentIndex)
{
// تحقق مما إذا كان فهرس الطالب صالحًا
if (studentIndex < 0 || studentIndex >= scoreGrid.Length)
{
return -1;
}
int sum = 0;
int count = 0;
// احسب مجموع الدرجات الصالحة
for (int j = 0; j < scoreGrid[studentIndex].Length; j++)
{
int score = scoreGrid[studentIndex][j];
if (score != -1) // تجاهل الواجبات التي لم تُقيَّم
{
sum += score;
count++;
}
}
// أعد المتوسط، أو 0 إذا لم توجد درجات صالحة
return count > 0 ? (double)sum / count : 0;
}
public static double CalculateAssignmentAverage(int[][] scoreGrid, int assignmentIndex)
{
// تحقق مما إذا كان هناك أي طلاب
if (scoreGrid.Length == 0)
{
return -1;
}
// تحقق مما إذا كان فهرس الواجب صالحًا
if (assignmentIndex < 0 || assignmentIndex >= scoreGrid[0].Length)
{
return -1;
}
int sum = 0;
int count = 0;
// احسب مجموع الدرجات الصالحة للواجب
for (int i = 0; i < scoreGrid.Length; i++)
{
if (assignmentIndex < scoreGrid[i].Length)
{
int score = scoreGrid[i][assignmentIndex];
if (score != -1) // تجاهل الواجبات التي لم تُقيَّم
{
sum += score;
count++;
}
}
}
// أعد المتوسط، أو 0 إذا لم توجد درجات صالحة
return count > 0 ? (double)sum / count : 0;
}
public static int[] FindHighestScore(int[][] scoreGrid)
{
int highestStudentIndex = 0;
int highestAssignmentIndex = 0;
int highestScore = -1;
// ابحث عن أعلى درجة
for (int i = 0; i < scoreGrid.Length; i++)
{
for (int j = 0; j < scoreGrid[i].Length; j++)
{
int currentScore = scoreGrid[i][j];
if (currentScore > highestScore)
{
highestScore = currentScore;
highestStudentIndex = i;
highestAssignmentIndex = j;
}
}
}
return new int[] { highestStudentIndex, highestAssignmentIndex, highestScore };
}
}
public class GradingSystem
{
public static string ConvertToLetterGrade(double score)
{
if (score < 0 || score > 100)
{
return "N/A";
}
else if (score >= 90)
{
return "A";
}
else if (score >= 80)
{
return "B";
}
else if (score >= 70)
{
return "C";
}
else if (score >= 60)
{
return "D";
}
else
{
return "F";
}
}
public static string GetStudentGrade(int[][] scoreGrid, int studentIndex)
{
double average = DataAnalyzer.CalculateStudentAverage(scoreGrid, studentIndex);
if (average == -1)
{
return "N/A";
}
return ConvertToLetterGrade(average);
}
public static int[] GetClassDistribution(int[][] scoreGrid)
{
int[] distribution = new int[5]; // [A, B, C, D, F]
for (int i = 0; i < scoreGrid.Length; i++)
{
string grade = GetStudentGrade(scoreGrid, i);
switch (grade)
{
case "A":
distribution[0]++;
break;
case "B":
distribution[1]++;
break;
case "C":
distribution[2]++;
break;
case "D":
distribution[3]++;
break;
case "F":
distribution[4]++;
break;
// لا تُحتسب درجات N/A ضمن التوزيع
}
}
return distribution;
}
}جميع دروس المنطق وتدفق التنفيذ
1المصفوفات متعددة الأبعاد
أساسيات المصفوفات ثنائية الأبعادتعريف وتهيئة المصفوفات ثنائية الأبعادالوصول إلى عناصر المصفوفة ثنائية الأبعادالحلقات المتداخلة مع المصفوفات ثنائية الأبعادالمصفوفات المتعرجة (Jagged Arrays)عمليات المصفوفات الشائعةمراجعة - المصفوفات متعددة الأبعاد4تقنيات التحكم في التدفق
الإرجاع المبكرشروط الحمايةجمل القفز (goto)Break و Continueتسطيح الشروط المتداخلة7المعاملات المنطقية المتقدمة
التقييم المختصرالمعاملات المنطقية الشرطيةأولوية المعاملاتمراجعة - المعاملات المتقدمة2اتخاذ القرارات المتقدم
الشروط المتعددةالمنطق البولياني المعقدمقارنة بين If و Switchجمل Switch المتداخلةمراجعة - القرارات المتقدمة5معالجة الاستثناءات
أساسيات Try-Catchأنواع الاستثناءاتكتل Catch المتعددةالتعامل مع الملفاتكتلة FinallyUsing مقابل Try-Finallyاستثناءات مخصصةمراجعة - معالجة الأخطاء8نظام تحليل البيانات
إعداد جمع البياناتمنطق إدخال البيانات11HashSet الجزء الأول
ما هو الـ HashSet؟إضافة عنصرحذف عنصرالتحقق من وجود عنصرالفراغ والحجمملخص - HashSet3تحسينات الحلقات التكرارية
أداء الحلقات التكراريةالتكرار عبر البيانات المعقدةأنواع الحلقات التكراريةإعادة هيكلة الحلقات التكراريةملخص - الحلقات المحسنة6التعامل مع الـ Null
أساسيات الـ Null Referenceأنواع القيم القابلة للـ Nullأنماط التحقق من الـ Nullالبرمجة الدفاعيةملخص - أمان الـ Nullتدرّب بنفسك: مترجم C# عبر الإنترنت