Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file not shown.
Binary file not shown.
Binary file added .vs/Homework/DesignTimeBuild/.dtbcache.v2
Binary file not shown.
Binary file not shown.
Binary file added .vs/Homework/v17/.futdcache.v2
Binary file not shown.
Binary file added .vs/Homework/v17/.suo
Binary file not shown.
37 changes: 37 additions & 0 deletions .vs/Homework/v17/DocumentLayout.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"Version": 1,
"WorkspaceRootPath": "D:\\Program Files\\Unity\\Projects\\CSharpHomework2025\\",
"Documents": [
{
"AbsoluteMoniker": "D:0:0:{FCF2C4E1-EB48-4D53-A8D7-EF81464309F4}|Homework\\Homework.csproj|d:\\program files\\unity\\projects\\csharphomework2025\\homework\\program.cs||{A6C744A8-0E4A-4FC6-886A-064283054674}",
"RelativeMoniker": "D:0:0:{FCF2C4E1-EB48-4D53-A8D7-EF81464309F4}|Homework\\Homework.csproj|solutionrelative:homework\\program.cs||{A6C744A8-0E4A-4FC6-886A-064283054674}"
}
],
"DocumentGroupContainers": [
{
"Orientation": 0,
"VerticalTabListWidth": 256,
"DocumentGroups": [
{
"DockedWidth": 200,
"SelectedChildIndex": 0,
"Children": [
{
"$type": "Document",
"DocumentIndex": 0,
"Title": "Program.cs",
"DocumentMoniker": "D:\\Program Files\\Unity\\Projects\\CSharpHomework2025\\Homework\\Program.cs",
"RelativeDocumentMoniker": "Homework\\Program.cs",
"ToolTip": "D:\\Program Files\\Unity\\Projects\\CSharpHomework2025\\Homework\\Program.cs",
"RelativeToolTip": "Homework\\Program.cs",
"ViewState": "AgIAAGsBAAAAAAAAAAAcwIUBAAABAAAAAAAAAA==",
"Icon": "ae27a6b0-e345-4288-96df-5eaf394ee369.000738|",
"WhenOpened": "2025-08-10T11:53:05.294Z",
"EditorCaption": ""
}
]
}
]
}
]
}
Binary file added .vs/ProjectEvaluation/homework.metadata.v9.bin
Binary file not shown.
Binary file added .vs/ProjectEvaluation/homework.projects.v9.bin
Binary file not shown.
Binary file added .vs/ProjectEvaluation/homework.strings.v9.bin
Binary file not shown.
185 changes: 152 additions & 33 deletions Homework/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace StudentManagementSystem
public enum Grade
{
// TODO: 定义成绩等级 F(0), D(60), C(70), B(80), A(90)

F, D, C, B, A
}

// 泛型仓储接口
Expand All @@ -20,32 +20,40 @@ public interface IRepository<T>
// Remove(T item) 返回bool
// GetAll() 返回List<T>
// Find(Func<T, bool> predicate) 返回List<T>

void Add(T item);
bool Remove(T item);
List<T> GetAll();
List<T> Find(Func<T, bool> predicate);
}

// 学生类
public class Student : IComparable<Student>
{
// TODO: 定义字段 StudentId, Name, Age


public string StudentId { get; }
public string Name { get; }

public int Age { get; }
public Student(string studentId, string name, int age)
{
// TODO: 实现构造方法,包含参数验证(空值检查)

StudentId = studentId ?? throw new ArgumentNullException(nameof(studentId));
Name = name ?? throw new ArgumentNullException(nameof(name));
Age = age > 0 ? age : throw new ArgumentException("年龄必须大于0");
}

public override string ToString()
{
// TODO: 返回格式化的学生信息字符串

return $"学号: {StudentId}, 姓名: {Name}, 年龄: {Age}";
}

// TODO: 实现IComparable接口,按学号排序
// 提示:使用string.Compare方法
public int CompareTo(Student? other)
{

if (other == null) return 1;
return string.Compare(StudentId, other.StudentId, StringComparison.Ordinal);
}

public override bool Equals(object? obj)
Expand All @@ -63,18 +71,23 @@ public override int GetHashCode()
public class Score
{
// TODO: 定义字段 Subject, Points


public string Subject { get; }
public double Points { get; }


public Score(string subject, double points)
{
// TODO: 实现构造方法,包含参数验证

Subject = subject ?? throw new ArgumentNullException(nameof(subject));
Points = points >= 0 && points <= 100
? points
: throw new ArgumentException("成绩必须在0-100之间");
}

public override string ToString()
{
// TODO: 返回格式化的成绩信息

return $"科目: {Subject}, 成绩: {Points}";
}
}

Expand All @@ -83,39 +96,56 @@ public class StudentManager : IRepository<Student>
{
// TODO: 定义私有字段存储学生列表
// 提示:使用List<Student>存储

private List<Student> students = new List<Student>();


public void Add(Student item)
{
// TODO: 实现添加学生的逻辑
// 1. 参数验证
// 2. 添加到列表

if (item == null) throw new ArgumentNullException(nameof(item));
if (students.Contains(item))
throw new InvalidOperationException("该学生已存在");
students.Add(item);
}

public bool Remove(Student item)
{
// TODO: 实现Remove方法

if (item == null) return false;
return students.Remove(item);
}

public List<Student> GetAll()
{
// TODO: 返回学生列表的副本

return new List<Student>(students);
}

public List<Student> Find(Func<Student, bool> predicate)
{
// TODO: 使用foreach循环查找符合条件的学生

var result = new List<Student>();
foreach (var student in students)
{
if (predicate(student))
result.Add(student);
}
return result;
}

// 查找年龄在指定范围内的学生
public List<Student> GetStudentsByAge(int minAge, int maxAge)
{
// TODO: 使用foreach循环和if判断实现年龄范围查询

var result = new List<Student>();
foreach (var student in students)
{
if (student.Age >= minAge && student.Age <= maxAge)
result.Add(student);
}
return result;
}
}

Expand All @@ -124,41 +154,82 @@ public class ScoreManager
{
// TODO: 定义私有字段存储成绩字典
// 提示:使用Dictionary<string, List<Score>>存储

private Dictionary<string, List<Score>> scores = new Dictionary<string, List<Score>>();

public void AddScore(string studentId, Score score)
{
// TODO: 实现添加成绩的逻辑
// 1. 参数验证
// 2. 初始化学生成绩列表(如不存在)
// 3. 添加成绩

if (string.IsNullOrEmpty(studentId))
throw new ArgumentNullException(nameof(studentId));
if (score == null)
throw new ArgumentNullException(nameof(score));

if (!scores.ContainsKey(studentId))
scores[studentId] = new List<Score>();

scores[studentId].Add(score);
}

public List<Score> GetStudentScores(string studentId)
{
// TODO: 获取指定学生的所有成绩

return scores.TryGetValue(studentId, out var studentScores)
? new List<Score>(studentScores)
: new List<Score>();
}

public double CalculateAverage(string studentId)
{
// TODO: 计算指定学生的平均分
// 提示:使用foreach循环计算总分,然后除以科目数

if (!scores.ContainsKey(studentId))
return 0;

double total = 0;
int count = 0;
foreach (var score in scores[studentId])
{
total += score.Points;
count++;
}
return count > 0 ? total / count : 0;
}

// TODO: 使用模式匹配实现成绩等级转换
public Grade GetGrade(double score)
{

return score switch
{
>= 90 => Grade.A,
>= 80 => Grade.B,
>= 70 => Grade.C,
>= 60 => Grade.D,
_ => Grade.F
};
}

public List<(string StudentId, double Average)> GetTopStudents(int count)
{
// TODO: 使用简单循环获取平均分最高的学生
// 提示:可以先计算所有学生的平均分,然后排序取前count个

var averages = new List<(string, double)>();

foreach (var stu in scores)
{
double total = 0;
foreach (var score in stu.Value)
{
total += score.Points;
}
double avg = stu.Value.Count > 0 ? total / stu.Value.Count : 0;
averages.Add((stu.Key, avg));
}

averages.Sort((x, y) => y.Item2.CompareTo(x.Item2));
return averages.Take(count).ToList();
}

public Dictionary<string, List<Score>> GetAllScores()
Expand All @@ -177,7 +248,11 @@ public void SaveStudentsToFile(List<Student> students, string filePath)
try
{
// 在这里实现文件写入逻辑

using var writer = new StreamWriter(filePath);
foreach (var student in students)
{
writer.WriteLine($"{student.StudentId},{student.Name},{student.Age}");
}
}
catch (Exception ex)
{
Expand All @@ -188,19 +263,32 @@ public void SaveStudentsToFile(List<Student> students, string filePath)
public List<Student> LoadStudentsFromFile(string filePath)
{
List<Student> students = new List<Student>();

// TODO: 实现从文件读取学生数据
// 提示:使用StreamReader,解析CSV格式
try
{
// 在这里实现文件读取逻辑

if (!File.Exists(filePath)) return students;

using var reader = new StreamReader(filePath);
string line;
while ((line = reader.ReadLine()) != null)
{
var parts = line.Split(',');
if (parts.Length != 3) continue;

if (int.TryParse(parts[2], out int age))
{
students.Add(new Student(parts[0], parts[1], age));
}
}
}
catch (Exception ex)
{
Console.WriteLine($"读取文件时发生错误: {ex.Message}");
}

return students;
}
}
Expand Down Expand Up @@ -230,33 +318,64 @@ static void Main(string[] args)
Console.WriteLine("\n2. 添加成绩信息:");
scoreManager.AddScore("2021001", new Score("数学", 95.5));
scoreManager.AddScore("2021001", new Score("英语", 87.0));

scoreManager.AddScore("2021002", new Score("数学", 78.5));
scoreManager.AddScore("2021002", new Score("英语", 85.5));

scoreManager.AddScore("2021003", new Score("数学", 88.0));
scoreManager.AddScore("2021003", new Score("英语", 92.0));
Console.WriteLine("成绩信息添加完成");

// 3. 测试年龄范围查询
Console.WriteLine("\n3. 查找年龄在19-20岁的学生:");
// TODO: 调用GetStudentsByAge方法并显示结果

var studentsInAgeRange = studentManager.GetStudentsByAge(19, 20);
foreach (var student in studentsInAgeRange)
{
Console.WriteLine(student);
}

// 4. 显示学生成绩统计
Console.WriteLine("\n4. 学生成绩统计:");
// TODO: 遍历所有学生,显示其成绩、平均分和等级

foreach (var student in studentManager.GetAll())
{
var scores = scoreManager.GetStudentScores(student.StudentId);
double average = scoreManager.CalculateAverage(student.StudentId);
Grade grade = scoreManager.GetGrade(average);

Console.WriteLine($"学生: {student.Name}");
foreach (var score in scores)
{
Console.WriteLine($" {score}");
}
Console.WriteLine($"平均分: {average}, 等级: {grade}");
}

// 5. 显示排名(简化版)
Console.WriteLine("\n5. 平均分最高的学生:");
// TODO: 调用GetTopStudents(1)方法显示第一名

var topStudents = scoreManager.GetTopStudents(1);
if (topStudents.Count > 0)
{
var theOne = topStudents[0];
var student = studentManager.Find(s => s.StudentId == theOne.StudentId).First();
Console.WriteLine($"{student.Name} 平均分: {theOne.Average:F2}");
}

// 6. 文件操作
Console.WriteLine("\n6. 数据持久化演示:");
// TODO: 保存和读取学生文件

string filePath = "students.csv";
dataManager.SaveStudentsToFile(studentManager.GetAll(), filePath);
Console.WriteLine($"学生数据已保存到: {filePath}");

var loadedStudents = dataManager.LoadStudentsFromFile(filePath);
Console.WriteLine($"从文件加载的学生数据 ({loadedStudents.Count} 个):");
foreach (var student in loadedStudents)
{
Console.WriteLine(student);
}

}
catch (Exception ex)
Expand Down
Loading