보고서 생성
Coddy C# 여정의 논리 및 흐름 섹션에 포함된 레슨. 66개 중 44번째.
챌린지
중급이제 채점 시스템이 있으므로, 학생 성과에 대한 형식이 지정된 보고서를 생성하는 보고 모듈을 만들어 보겠습니다. 다음 메서드를 포함하는 ReportGenerator라는 클래스를 만드세요:
GenerateStudentReport(int[][] scoreGrid, int studentIndex): 특정 학생의 점수, 평균 및 문자 등급을 보고하는 형식이 지정된 문자열을 반환합니다.- 형식: "Student #X | Average: YY.Y | Grade: Z\nAssignment scores: S1, S2, S3, ..." 여기서 S1, S2, S3, ...은 각 assignment의 숫자 점수입니다.
- 채점되지 않은 assignment(점수가 -1인 경우)는 점수 대신 "N/A"를 표시합니다.
- 유효하지 않은 studentIndex에 대해서는 "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의 통계가 포함된 형식이 지정된 문자열을 반환합니다.- 형식: "Assignment #X | Average: YY.Y | Completion Rate: Z%"
- Completion rate는 이 assignment에 대해 점수(-1이 아님)가 있는 학생의 비율입니다.
- 유효하지 않은 assignmentIndex에 대해서는 "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)
{
// Check if student index is valid
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;
}
// Check if assignment index is valid
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 grades are not counted in the distribution
}
}
return distribution;
}
}논리 및 흐름의 모든 레슨
직접 연습해 보세요: 온라인 C# 컴파일러