创建成绩功能:根据键盘输入创建各班学生各学科成绩信息。 根据班级查找功能:根据输入的班级信息,查询该班学生的分数信息及班级排名。 根据课程查找功能:根据输入的课程名称,查询该课程的均分,学生排名、分数等。 根据学生查找功能:根据输入的学生信息,查询该生的各科成绩、总分及排名等。 输出成绩功能:遍历输出所有学生的成绩信息、班级排名等。 修改功能:修改更新某个学生的成绩信息。 文件操作功能:使用文件保存学生成绩的信息,需要时读取。需要用到链表,并用代码实现
时间: 2024-03-25 09:38:01 浏览: 58
好的,以下是使用链表实现学生成绩管理系统的示例代码,包括上述所有功能:
```python
class Student:
def __init__(self, name, grade, scores):
self.name = name
self.grade = grade
self.scores = scores
self.next = None
class ScoreList:
def __init__(self):
self.head = None
def add_student(self, name, grade, scores):
new_student = Student(name, grade, scores)
if self.head is None:
self.head = new_student
else:
current = self.head
while current.next is not None:
current = current.next
current.next = new_student
def search_by_grade(self, grade):
current = self.head
grade_list = []
while current is not None:
if current.grade == grade:
grade_list.append((current.name, current.scores))
current = current.next
grade_list.sort(key=lambda x: sum(x[1]), reverse=True)
for i, student in enumerate(grade_list):
print(f"{i+1}. {student[0]}: {student[1]}, Total: {sum(student[1])}")
def search_by_course(self, course):
current = self.head
course_list = []
while current is not None:
if course in current.scores:
course_list.append((current.name, current.scores[course]))
current = current.next
course_list.sort(key=lambda x: x[1], reverse=True)
total_score = sum([x[1] for x in course_list])
avg_score = total_score / len(course_list)
print(f"Average score for {course}: {avg_score}")
for i, student in enumerate(course_list):
print(f"{i+1}. {student[0]}: {student[1]}")
def search_by_name(self, name):
current = self.head
while current is not None:
if current.name == name:
scores = current.scores
total_score = sum(scores.values())
rank = 1
temp = self.head
while temp is not None:
if temp != current and sum(temp.scores.values()) > total_score:
rank += 1
temp = temp.next
print(f"{name}: {scores}, Total: {total_score}, Rank: {rank}")
return
current = current.next
print(f"{name} not found")
def output_scores(self):
current = self.head
grade_dict = {}
while current is not None:
grade_dict.setdefault(current.grade, []).append((current.name, current.scores))
current = current.next
for grade, student_list in grade_dict.items():
print(f"Grade {grade}:")
student_list.sort(key=lambda x: sum(x[1]), reverse=True)
for i, student in enumerate(student_list):
print(f"{i+1}. {student[0]}: {student[1]}, Total: {sum(student[1])}")
def modify_scores(self, name, course, score):
current = self.head
while current is not None:
if current.name == name:
current.scores[course] = score
return
current = current.next
print(f"{name} not found")
def load_data(self, file_name):
try:
with open(file_name, "r") as f:
lines = f.readlines()
for line in lines:
data = line.strip().split(",")
name = data[0]
grade = int(data[1])
scores = {}
for i in range(2, len(data), 2):
course = data[i]
score = int(data[i+1])
scores[course] = score
self.add_student(name, grade, scores)
except FileNotFoundError:
print("Data file not found")
def save_data(self, file_name):
current = self.head
with open(file_name, "w") as f:
while current is not None:
line = current.name + "," + str(current.grade)
for course, score in current.scores.items():
line += "," + course + "," + str(score)
line += "\n"
f.write(line)
current = current.next
```
你可以根据需要进一步完善和优化这个代码,比如添加输入验证、错误处理等。
阅读全文