Menu
CoddyTech

Tạo báo cáo

Thuộc phần Logic và luồng chương trình trong hành trình C# của Coddy. Bài học 44 trên 66.

challenge icon

Thử thách

Trung bình

Giờ đây, khi đã có hệ thống chấm điểm, hãy tạo một module báo cáo để tạo báo cáo có định dạng về thành tích của học sinh. Tạo một lớp có tên ReportGenerator với các phương thức sau:

  1. GenerateStudentReport(int[][] scoreGrid, int studentIndex): Trả về một chuỗi có định dạng, báo cáo điểm số, điểm trung bình và điểm chữ của một học sinh cụ thể.
    • Định dạng: "Student #X | Average: YY.Y | Grade: Z\nAssignment scores: S1, S2, S3, ..." trong đó S1, S2, S3, ... là điểm số dạng số cho từng bài tập.
    • Với các bài tập chưa được chấm điểm (điểm số là -1), hiển thị "N/A" thay cho điểm số.
    • Trả về "Invalid student index" nếu chỉ số học sinh không hợp lệ.
  2. GenerateClassSummary(int[][] scoreGrid): Trả về một chuỗi có định dạng, tóm tắt thành tích của cả lớp.
    • Định dạng: "Class Summary\nTotal Students: X\nClass Average: YY.Y\nGrade Distribution: A: #, B: #, C: #, D: #, F: #"
  3. GenerateAssignmentReport(int[][] scoreGrid, int assignmentIndex): Trả về một chuỗi có định dạng, bao gồm số liệu thống kê cho một bài tập cụ thể.
    • Định dạng: "Assignment #X | Average: YY.Y | Completion Rate: Z%"
    • Tỷ lệ hoàn thành là phần trăm học sinh đã có điểm cho bài tập này (không phải -1).
    • Trả về "Invalid assignment index" nếu chỉ số bài tập không hợp lệ.

Tự làm thử

using System; // Đừng xóa dòng này
using System.Text;

public class ReportGenerator
{
    public static string GenerateStudentReport(int[][] scoreGrid, int studentIndex)
    {
        // Viết code của bạn ở đây
        
    }
    
    public static string GenerateClassSummary(int[][] scoreGrid)
    {
        // Viết code của bạn ở đây
        
    }
    
    public static string GenerateAssignmentReport(int[][] scoreGrid, int assignmentIndex)
    {
        // Viết code của bạn ở đây
        
    }
}

// Cần cho việc kiểm thử, đừng chỉnh sửa
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)
    {
        // Kiểm tra các chỉ số có vượt phạm vi không
        if (studentIndex < 0 || studentIndex >= scoreGrid.Length || 
            assignmentIndex < 0 || assignmentIndex >= scoreGrid[studentIndex].Length)
        {
            return -1;
        }
        
        // Kiểm tra điểm có hợp lệ không
        if (!DataCollector.ValidateScore(score))
        {
            return -2;
        }
        
        // Gán điểm
        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)
    {
        // Kiểm tra chỉ số học sinh có hợp lệ không
        if (studentIndex < 0 || studentIndex >= scoreGrid.Length)
        {
            return -1;
        }
        
        int sum = 0;
        int count = 0;
        
        // Tính tổng các điểm hợp lệ
        for (int j = 0; j < scoreGrid[studentIndex].Length; j++)
        {
            int score = scoreGrid[studentIndex][j];
            if (score != -1)  // Bỏ qua các bài tập chưa chấm điểm
            {
                sum += score;
                count++;
            }
        }
        
        // Trả về điểm trung bình, hoặc 0 nếu không có điểm hợp lệ
        return count > 0 ? (double)sum / count : 0;
    }
    
    public static double CalculateAssignmentAverage(int[][] scoreGrid, int assignmentIndex)
    {
        // Kiểm tra xem có học sinh nào không
        if (scoreGrid.Length == 0)
        {
            return -1;
        }
        
        // Kiểm tra chỉ số bài tập có hợp lệ không
        if (assignmentIndex < 0 || assignmentIndex >= scoreGrid[0].Length)
        {
            return -1;
        }
        
        int sum = 0;
        int count = 0;
        
        // Tính tổng các điểm hợp lệ của bài tập
        for (int i = 0; i < scoreGrid.Length; i++)
        {
            if (assignmentIndex < scoreGrid[i].Length)
            {
                int score = scoreGrid[i][assignmentIndex];
                if (score != -1)  // Bỏ qua các bài tập chưa chấm điểm
                {
                    sum += score;
                    count++;
                }
            }
        }
        
        // Trả về điểm trung bình, hoặc 0 nếu không có điểm hợp lệ
        return count > 0 ? (double)sum / count : 0;
    }
    
    public static int[] FindHighestScore(int[][] scoreGrid)
    {
        int highestStudentIndex = 0;
        int highestAssignmentIndex = 0;
        int highestScore = -1;
        
        // Tìm điểm cao nhất
        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;
                // Không tính các mức điểm N/A vào phân bố điểm
            }
        }
        
        return distribution;
    }
}

Tất cả bài học trong Logic và luồng chương trình

Tự luyện tập: Trình biên dịch C# online