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
157 changes: 125 additions & 32 deletions Homework/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// See https://aka.ms/new-console-template for more information
// See https://aka.ms/new-console-template for more information
using System;
using System.Collections.Generic;
using System.IO;
Expand All @@ -9,43 +9,58 @@ namespace StudentManagementSystem
public enum Grade
{
// TODO: 定义成绩等级 F(0), D(60), C(70), B(80), A(90)

F = 0,
D = 60,
C = 70,
B = 80,
A = 90
}

// 泛型仓储接口
public interface IRepository<T>
{
// TODO: 定义接口方法
// Add(T item)
// 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;
public string Name;
public int Age;


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

if (string.IsNullOrWhiteSpace(studentId))
throw new ArgumentException("学号不能为空", nameof(studentId));
if (string.IsNullOrWhiteSpace(name))
throw new ArgumentException("姓名不能为空", nameof(name));
if (age <= 0)
throw new ArgumentException("年龄必须大于0", nameof(age));
StudentId = studentId;
Name = name;
Age = age;
}

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 +78,24 @@ public override int GetHashCode()
public class Score
{
// TODO: 定义字段 Subject, Points


string Subject;
public double Points;

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

if (string.IsNullOrWhiteSpace(subject))
throw new ArgumentException("科目不能为空", nameof(subject));
if (points < 0 || points > 100)
throw new ArgumentException("分数必须在0-100之间", nameof(points));
Subject = subject;
Points = points;
}

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

return $"{Subject}: {Points:F1}";
}
}

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

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

public void Add(Student item)
{
Expand All @@ -96,69 +117,122 @@ public void Add(Student item)
public bool Remove(Student item)
{
// TODO: 实现Remove方法

if (item == null)
throw new ArgumentNullException(nameof(item));
if (students.Contains(item))
students.Remove(item);
else
return false;
return true;
}

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

return new List<Student>(students);
}

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

List<Student> 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判断实现年龄范围查询

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


// 成绩管理类
public class ScoreManager
{
// TODO: 定义私有字段存储成绩字典
// 提示:使用Dictionary<string, List<Score>>存储


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

if (studentId == null || score == null)
throw new ArgumentNullException();
if (!scores.ContainsKey(studentId))
scores[studentId] = new List<Score>();
scores[studentId].Add(score);

}

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

if (scores.ContainsKey(studentId))
return new List<Score>(scores[studentId]);
else
return new List<Score>();
}

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

double total = 0;
foreach (var score in scores[studentId])
{
total += score.Points;
}
if (total==0)
return 0;
else
return total / scores[studentId].Count;
}

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

switch (score)
{
case var s when s >= 90:
return Grade.A;
case var s when s >= 80:
return Grade.B;
case var s when s >= 70:
return Grade.C;
case var s when s >= 60:
return Grade.D;
default:
return Grade.F;
}
}

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

List<(string StudentId, double Average)> result = new List<(string StudentId, double Average)>();
foreach (var studentId in scores.Keys)
{
double average = CalculateAverage(studentId);
result.Add((studentId, average));
}
result.Sort((a, b) => b.Average.CompareTo(a.Average));
return result.GetRange(0, count);
}

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

using (StreamWriter writer = new StreamWriter(filePath))
{
foreach (var student in students)
{
writer.WriteLine($"{student.StudentId},{student.Name},{student.Age}");
}
}
}
catch (Exception ex)
{
Expand All @@ -194,7 +274,20 @@ public List<Student> LoadStudentsFromFile(string filePath)
try
{
// 在这里实现文件读取逻辑

using (StreamReader reader = new StreamReader(filePath))
{
string? line;
while ((line = reader.ReadLine()) != null)
{
string[] parts = line.Split(',');
if (parts.Length != 3)
continue;
string studentId = parts[0];
string name = parts[1];
int age = int.Parse(parts[2]);
students.Add(new Student(studentId, name, age));
}
}
}
catch (Exception ex)
{
Expand Down Expand Up @@ -264,8 +357,8 @@ static void Main(string[] args)
Console.WriteLine($"程序执行过程中发生错误: {ex.Message}");
}

Console.WriteLine("\n程序执行完毕,按任意键退出...");
Console.ReadKey();
Console.WriteLine("\n程序执行完毕,按回车键退出...");
Console.ReadLine();
}
}
}
23 changes: 23 additions & 0 deletions Homework/bin/Debug/net9.0/Homework.deps.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"runtimeTarget": {
"name": ".NETCoreApp,Version=v9.0",
"signature": ""
},
"compilationOptions": {},
"targets": {
".NETCoreApp,Version=v9.0": {
"Homework/1.0.0": {
"runtime": {
"Homework.dll": {}
}
}
}
},
"libraries": {
"Homework/1.0.0": {
"type": "project",
"serviceable": false,
"sha512": ""
}
}
}
Binary file added Homework/bin/Debug/net9.0/Homework.dll
Binary file not shown.
Binary file added Homework/bin/Debug/net9.0/Homework.exe
Binary file not shown.
Binary file added Homework/bin/Debug/net9.0/Homework.pdb
Binary file not shown.
12 changes: 12 additions & 0 deletions Homework/bin/Debug/net9.0/Homework.runtimeconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"runtimeOptions": {
"tfm": "net9.0",
"framework": {
"name": "Microsoft.NETCore.App",
"version": "9.0.0"
},
"configProperties": {
"System.Runtime.Serialization.EnableUnsafeBinaryFormatterSerialization": false
}
}
}
2 changes: 1 addition & 1 deletion Homework/obj/Debug/net9.0/Homework.AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
[assembly: System.Reflection.AssemblyCompanyAttribute("Homework")]
[assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+3439ef5fa11ea974a437d4e71f6d7e82763f847e")]
[assembly: System.Reflection.AssemblyProductAttribute("Homework")]
[assembly: System.Reflection.AssemblyTitleAttribute("Homework")]
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
92fcf719969c7f59361926c468f07a76ab2866f8147167aa553b98c3b9d7ed15
3e80dbcd1ff00cdcba3863df6d82f161321270261e04e5ccbff3d91e740385e0
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ build_property.PlatformNeutralAssembly =
build_property.EnforceExtendedAnalyzerRules =
build_property._SupportedPlatformList = Linux,macOS,Windows
build_property.RootNamespace = Homework
build_property.ProjectDir = C:\Users\ms169\Desktop\Homework\Homework\
build_property.ProjectDir = E:\summerproject\hw\finished\3\CSharpHomework2025\Homework\
build_property.EnableComHosting =
build_property.EnableGeneratedComInterfaceComImportInterop =
build_property.EffectiveAnalysisLevelStyle = 9.0
Expand Down
Binary file modified Homework/obj/Debug/net9.0/Homework.assets.cache
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1fd125f1881af0b1d5af23aa715b47d86fb58cf91fb04cb99d729a36a293b95c
15 changes: 15 additions & 0 deletions Homework/obj/Debug/net9.0/Homework.csproj.FileListAbsolute.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
E:\summerproject\hw\finished\3\CSharpHomework2025\Homework\bin\Debug\net9.0\Homework.exe
E:\summerproject\hw\finished\3\CSharpHomework2025\Homework\bin\Debug\net9.0\Homework.deps.json
E:\summerproject\hw\finished\3\CSharpHomework2025\Homework\bin\Debug\net9.0\Homework.runtimeconfig.json
E:\summerproject\hw\finished\3\CSharpHomework2025\Homework\bin\Debug\net9.0\Homework.dll
E:\summerproject\hw\finished\3\CSharpHomework2025\Homework\bin\Debug\net9.0\Homework.pdb
E:\summerproject\hw\finished\3\CSharpHomework2025\Homework\obj\Debug\net9.0\Homework.GeneratedMSBuildEditorConfig.editorconfig
E:\summerproject\hw\finished\3\CSharpHomework2025\Homework\obj\Debug\net9.0\Homework.AssemblyInfoInputs.cache
E:\summerproject\hw\finished\3\CSharpHomework2025\Homework\obj\Debug\net9.0\Homework.AssemblyInfo.cs
E:\summerproject\hw\finished\3\CSharpHomework2025\Homework\obj\Debug\net9.0\Homework.csproj.CoreCompileInputs.cache
E:\summerproject\hw\finished\3\CSharpHomework2025\Homework\obj\Debug\net9.0\Homework.sourcelink.json
E:\summerproject\hw\finished\3\CSharpHomework2025\Homework\obj\Debug\net9.0\Homework.dll
E:\summerproject\hw\finished\3\CSharpHomework2025\Homework\obj\Debug\net9.0\refint\Homework.dll
E:\summerproject\hw\finished\3\CSharpHomework2025\Homework\obj\Debug\net9.0\Homework.pdb
E:\summerproject\hw\finished\3\CSharpHomework2025\Homework\obj\Debug\net9.0\Homework.genruntimeconfig.cache
E:\summerproject\hw\finished\3\CSharpHomework2025\Homework\obj\Debug\net9.0\ref\Homework.dll
Binary file added Homework/obj/Debug/net9.0/Homework.dll
Binary file not shown.
1 change: 1 addition & 0 deletions Homework/obj/Debug/net9.0/Homework.genruntimeconfig.cache
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
69d5b00e3f24486c4f950fabda5f365c9078cd42ae4e6d3db90f28389da708f0
Binary file added Homework/obj/Debug/net9.0/Homework.pdb
Binary file not shown.
1 change: 1 addition & 0 deletions Homework/obj/Debug/net9.0/Homework.sourcelink.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"documents":{"E:\\summerproject\\hw\\finished\\3\\CSharpHomework2025\\*":"https://raw.githubusercontent.com/RhNO3-lx/CSharpHomework2025/3439ef5fa11ea974a437d4e71f6d7e82763f847e/*"}}
Binary file added Homework/obj/Debug/net9.0/apphost.exe
Binary file not shown.
Binary file added Homework/obj/Debug/net9.0/ref/Homework.dll
Binary file not shown.
Binary file added Homework/obj/Debug/net9.0/refint/Homework.dll
Binary file not shown.
Loading