设计一个学生成绩管理程序,实现按班级完成对学生成绩信息的录入和修改,并用文件保存。实现按班级输出学生的成绩单;实现按学号和姓名进行查询,按平均成绩进行排序功能。用户调试
时间: 2024-04-02 10:33:37 浏览: 214
以下是一个简单的学生成绩管理程序,包含录入学生信息、修改学生信息、保存学生信息、输出学生成绩单、查询学生信息和排序学生信息功能。具体实现细节可以根据需要进行调整。
```python
import os
class Student:
def __init__(self, name, number, score):
self.name = name
self.number = number
self.score = score
def __str__(self):
return f"{self.name},{self.number},{self.score}"
class Grade:
def __init__(self, name):
self.name = name
self.students = []
def add_student(self, student):
self.students.append(student)
def find_student_by_number(self, number):
for student in self.students:
if student.number == number:
return student
return None
def find_student_by_name(self, name):
for student in self.students:
if student.name == name:
return student
return None
def sort_students_by_score(self):
self.students = sorted(self.students, key=lambda x: x.score, reverse=True)
def __str__(self):
return f"{self.name}班级"
class GradeManager:
def __init__(self):
self.grades = []
def add_grade(self, grade):
self.grades.append(grade)
def find_grade_by_name(self, name):
for grade in self.grades:
if grade.name == name:
return grade
return None
def save_grades(self, filename):
with open(filename, "w") as f:
for grade in self.grades:
for student in grade.students:
f.write(f"{grade.name},{str(student)}\n")
def load_grades(self, filename):
with open(filename, "r") as f:
lines = f.readlines()
for line in lines:
parts = line.strip().split(",")
grade_name = parts[0]
student_name = parts[1]
student_number = parts[2]
student_score = int(parts[3])
grade = self.find_grade_by_name(grade_name)
if grade is None:
grade = Grade(grade_name)
self.add_grade(grade)
student = Student(student_name, student_number, student_score)
grade.add_student(student)
def print_grade(self, grade_name):
grade = self.find_grade_by_name(grade_name)
if grade is None:
print(f"没有找到{grade_name}班级")
return
print(f"{grade}学生成绩单:")
for student in grade.students:
print(f"{student.name}\t{student.number}\t{student.score}")
def query_student_by_number(self, number):
for grade in self.grades:
student = grade.find_student_by_number(number)
if student is not None:
print(f"{grade}:{student.name}\t{student.number}\t{student.score}")
return
print(f"没有找到学号为{number}的学生")
def query_student_by_name(self, name):
for grade in self.grades:
student = grade.find_student_by_name(name)
if student is not None:
print(f"{grade}:{student.name}\t{student.number}\t{student.score}")
return
print(f"没有找到姓名为{name}的学生")
def sort_students_by_score(self):
for grade in self.grades:
grade.sort_students_by_score()
if __name__ == "__main__":
manager = GradeManager()
filename = "grades.txt"
if os.path.exists(filename):
manager.load_grades(filename)
while True:
print("欢迎使用学生成绩管理程序!")
print("1. 录入学生成绩")
print("2. 修改学生成绩")
print("3. 输出学生成绩单")
print("4. 查询学生信息")
print("5. 排序学生成绩")
print("6. 保存学生成绩")
print("0. 退出程序")
choice = input("请选择操作:")
if choice == "1":
name = input("请输入班级名称:")
grade = manager.find_grade_by_name(name)
if grade is None:
grade = Grade(name)
manager.add_grade(grade)
number = input("请输入学号:")
student = grade.find_student_by_number(number)
if student is not None:
print("该学号已存在,请重新输入!")
continue
name = input("请输入姓名:")
score = input("请输入成绩:")
student = Student(name, number, int(score))
grade.add_student(student)
print("学生成绩录入成功!")
elif choice == "2":
number = input("请输入学号:")
student = None
for grade in manager.grades:
student = grade.find_student_by_number(number)
if student is not None:
break
if student is None:
print("没有找到该学号的学生!")
continue
name = input("请输入姓名:")
score = input("请输入成绩:")
student.name = name
student.score = int(score)
print("学生成绩修改成功!")
elif choice == "3":
name = input("请输入班级名称:")
manager.print_grade(name)
elif choice == "4":
method = input("请选择查询方式(1. 按学号查询,2. 按姓名查询):")
if method == "1":
number = input("请输入学号:")
manager.query_student_by_number(number)
elif method == "2":
name = input("请输入姓名:")
manager.query_student_by_name(name)
else:
print("请输入正确的选项!")
elif choice == "5":
manager.sort_students_by_score()
print("学生成绩已按平均成绩排序!")
elif choice == "6":
manager.save_grades(filename)
print("学生成绩已保存!")
elif choice == "0":
break
else:
print("请输入正确的选项!")
```
用户可以根据提示进行操作,程序将会根据用户的输入进行相应的操作。程序将学生成绩保存在名为“grades.txt”的文件中,用户可以多次运行程序来读取和修改该文件中的数据。
阅读全文