Generowanie raportów
Część sekcji Logika i sterowanie przepływem ścieżki C# w Coddy. Lekcja 44 z 66.
Wyzwanie
ŚredniTeraz, gdy mamy już system oceniania, utwórzmy moduł raportowania do generowania sformatowanych raportów dotyczących wyników uczniów. Utwórz klasę o nazwie ReportGenerator z następującymi metodami:
GenerateStudentReport(int[][] scoreGrid, int studentIndex): Zwraca sformatowany ciąg znaków zawierający wyniki, średnią i ocenę literową konkretnego ucznia.- Format: "Student #X | Average: YY.Y | Grade: Z\nAssignment scores: S1, S2, S3, ..." gdzie S1, S2, S3, ... to wyniki liczbowe za poszczególne zadania.
- W przypadku nieocenionych zadań (wynik równy -1) wyświetl "N/A" zamiast wyniku.
- Zwróć "Invalid student index" dla dowolnego nieprawidłowego indeksu ucznia.
GenerateClassSummary(int[][] scoreGrid): Zwraca sformatowany ciąg znaków podsumowujący wyniki klasy.- Format: "Class Summary\nTotal Students: X\nClass Average: YY.Y\nGrade Distribution: A: #, B: #, C: #, D: #, F: #"
GenerateAssignmentReport(int[][] scoreGrid, int assignmentIndex): Zwraca sformatowany ciąg znaków ze statystykami dotyczącymi konkretnego zadania.- Format: "Assignment #X | Average: YY.Y | Completion Rate: Z%"
- Wskaźnik ukończenia to odsetek uczniów, którzy mają ocenę za to zadanie (inną niż -1).
- Zwróć "Invalid assignment index" dla dowolnego nieprawidłowego indeksu zadania.
Spróbuj swoich sił
using System; // Nie usuwaj tej linii
using System.Text;
public class ReportGenerator
{
public static string GenerateStudentReport(int[][] scoreGrid, int studentIndex)
{
// Wpisz tutaj swój kod
}
public static string GenerateClassSummary(int[][] scoreGrid)
{
// Wpisz tutaj swój kod
}
public static string GenerateAssignmentReport(int[][] scoreGrid, int assignmentIndex)
{
// Wpisz tutaj swój kod
}
}
// Wymagane do testowania – nie modyfikuj
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)
{
// Sprawdź, czy indeksy mieszczą się w dozwolonym zakresie
if (studentIndex < 0 || studentIndex >= scoreGrid.Length ||
assignmentIndex < 0 || assignmentIndex >= scoreGrid[studentIndex].Length)
{
return -1;
}
// Sprawdź poprawność oceny
if (!DataCollector.ValidateScore(score))
{
return -2;
}
// Ustaw ocenę
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)
{
// Sprawdź, czy indeks ucznia jest prawidłowy
if (studentIndex < 0 || studentIndex >= scoreGrid.Length)
{
return -1;
}
int sum = 0;
int count = 0;
// Oblicz sumę prawidłowych ocen
for (int j = 0; j < scoreGrid[studentIndex].Length; j++)
{
int score = scoreGrid[studentIndex][j];
if (score != -1) // Pomiń nieocenione zadania
{
sum += score;
count++;
}
}
// Zwróć średnią lub 0, jeśli nie ma prawidłowych ocen
return count > 0 ? (double)sum / count : 0;
}
public static double CalculateAssignmentAverage(int[][] scoreGrid, int assignmentIndex)
{
// Sprawdź, czy są jacyś uczniowie
if (scoreGrid.Length == 0)
{
return -1;
}
// Sprawdź, czy indeks zadania jest prawidłowy
if (assignmentIndex < 0 || assignmentIndex >= scoreGrid[0].Length)
{
return -1;
}
int sum = 0;
int count = 0;
// Oblicz sumę poprawnych ocen za zadanie
for (int i = 0; i < scoreGrid.Length; i++)
{
if (assignmentIndex < scoreGrid[i].Length)
{
int score = scoreGrid[i][assignmentIndex];
if (score != -1) // Pomiń nieocenione zadania
{
sum += score;
count++;
}
}
}
// Zwróć średnią lub 0, jeśli nie ma prawidłowych ocen
return count > 0 ? (double)sum / count : 0;
}
public static int[] FindHighestScore(int[][] scoreGrid)
{
int highestStudentIndex = 0;
int highestAssignmentIndex = 0;
int highestScore = -1;
// Znajdź najwyższą ocenę
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;
// Oceny N/A nie są uwzględniane w rozkładzie ocen
}
}
return distribution;
}
}Wszystkie lekcje w sekcji Logika i sterowanie przepływem
1Tablice wielowymiarowe
Podstawy tablic 2DDeklarowanie i inicjalizacja tablic 2DDostęp do elementów tablic 2DZagnieżdżone pętle z tablicami 2DTablice postrzępionePodstawowe operacje na macierzachPodsumowanie — tablice wielowymiarowe4Techniki sterowania przepływem
Wczesne zwrotyKlauzule ochronneInstrukcje skoku (goto)Break i continueSpłaszczanie zagnieżdżonych warunków7Zaawansowane operatory logiczne
Ewaluacja z krótkim spięciemWarunkowe operatory logicznePriorytet operatorówPowtórzenie — zaawansowane operatory2Zaawansowane podejmowanie decyzji
Wiele warunkówZłożona logika Boole’aPorównanie if i switchZagnieżdżone instrukcje switchPodsumowanie — zaawansowane decyzje5Obsługa wyjątków
Podstawy try-catchTypy wyjątkówWiele bloków catchPraca z plikamiBlok finallyusing a try-finallyWłasne wyjątkiPowtórzenie — obsługa błędów8System analizy danych
Konfiguracja zbierania danychLogika wprowadzania danych3Udoskonalenia pętli
Wydajność pętliIterowanie po złożonych strukturachKażdy typ pętliRefaktoryzacja pętliPodsumowanie — zoptymalizowane pętle6Obsługa wartości null
Podstawy referencji nullTypy wartości dopuszczające nullWzorce sprawdzania nullProgramowanie defensywnePodsumowanie — bezpieczeństwo nullPoćwicz samodzielnie: Kompilator C# online