הפקת דוחות
חלק מהיחידה לוגיקה וזרימת תוכנית במסלול ה-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מערכים רב־ממדיים
יסודות מערכים דו־ממדייםהכרזה ואתחול של מערכים דו־ממדייםגישה לאיברי מערך דו־ממדילולאות מקוננות עם מערכים דו־ממדייםמערכים משונניםפעולות מטריצה נפוצותחזרה – מערכים רב־ממדיים7אופרטורים לוגיים מתקדמים
הערכה בקיצור דרךאופרטורים לוגיים מותניםקדימות אופרטוריםסיכום – אופרטורים מתקדמים2קבלת החלטות מתקדמת
תנאים מרוביםלוגיקה בוליאנית מורכבתהשוואה בין If ל-Switchהצהרות Switch מקוננותסיכום — קבלת החלטות מתקדמת5טיפול בחריגות
יסודות try-catchסוגי חריגותבלוקי catch מרוביםעבודה עם קבציםבלוק finallyשימוש ב-using לעומת try-finallyחריגות מותאמות אישיתסיכום – טיפול בשגיאות8מערכת ניתוח נתונים
הגדרת איסוף נתוניםלוגיקת הזנת נתונים3שיפורים בלולאות
ביצועי לולאותאיטרציה במבנים מורכביםכל אחד מסוגי הלולאותרפקטורינג של לולאותסיכום – לולאות שעברו אופטימיזציה6טיפול בערכי Null
יסודות הפניה ל-Nullטיפוסי ערכים שניתנים ל-Nullדפוסי בדיקת Nullתכנות הגנתיסיכום – בטיחות מול Nullתרגלו בעצמכם: קומפיילר C# אונליין