以下是使用 Python 实现的学生成绩管理程序代码: ```python # 定义学生类 class Student: def __init__(self, student_id, name, score1, score2, score3): self.student_id = student_id self.name = name self.score1 = score1 self.score2 = score2 self.score3 = score3 self.total_score = score1 + score2 + score3 # 计算总分 # 存储学生信息的列表 students = [] # 添加学生信息的函数 def add_student(): student_id = int(input("请输入学号:")) score1 = float(input("请输入课程成绩 1:")) score2 = float(input("请输入课程成绩 2:")) score3 = float(input("请输入课程成绩 3:")) name = input("请输入姓名:") # 检查是否有重复学号 for student in students: if student.student_id == student_id: student.name = name student.score1 = score1 student.score2 = score2 student.score3 = score3 return new_student = Student(student_id, name, score1, score2, score3) students.append(new_student) # 修改学生信息的函数 def modify_student(): student_id = int(input("请输入学号:")) found = False for student in students: if student.student_id == student_id: found = True score1 = float(input("请输入新的课程成绩 1:")) score2 = float(input("请输入新的课程成绩 2:")) score3 = float(input("请输入新的课程成绩 3:")) name = input("请输入新的姓名:") student.name = name student.score1 = score1 student.score2 = score2 student.score3 = score3 if not found: print("未找到该学号的学生,无法修改。") # 按学号删除学生的函数 def delete_student_by_id(): student_id = int(input("请输入学号:")) found = False for student in students: if student.student_id == student_id: found = True students.remove(student) if not found: print("未找到该学号的学生,无法删除。") # 按学号查询学生信息的函数 def query_student_by_id(): student_id = int(input("请输入学号:")) found = False for student in students: if student.student_id == student_id: found = True print(f"学号:{student.student_id},姓名:{student.name},课程成绩 1:{student.score1},课程成绩 2:{student.score2},课程成绩 3:{student.score3}") if not found: print("未找到该学号的学生。") # 按姓名查询学生信息的函数 def query_student_by_name(): name = input("请输入姓名:") found = False sorted_students = sorted(students, key=lambda x: x.student_id) # 按学号升序排序 for student in sorted_students: if student.name == name: found = True print(f"学号:{student.student_id},姓名:{student.name},课程成绩 1:{student.score1},课程成绩 2:{student.score2},课程成绩 3:{student.score3}") if not found: print("未找到该姓名的学生。") # 按学号升序排序的函数 def sort_by_student_id(): students.sort(key=lambda x: x.student_id) # 按总分降序排序的函数 def sort_by_total_score(): students.sort(key=lambda x: (-x.total_score, x.student_id)) # 主函数 def main(): while True: choice = int(input("请选择操作(0:退出,1:添加学生信息,2:修改学生信息,3:按学号删除学生,4:按学号查询学生信息,5:按姓名查询学生信息,6:按学号升序排序,7:按总分降序排序):")) if choice == 0: break elif choice == 1: add_student() elif choice == 2: modify_student() elif choice == 3: delete_student_by_id() elif choice == 4: query_student_by_id() elif choice == 5: query_student_by_name() elif choice == 6: sort_by_student_id() elif choice == 7: sort_by_total_score() if __name__ == "__main__": main() ``` 额外需要注意的逻辑点: 1. 在处理输入时,要做好异常情况的处理,比如输入的学号不是整数,成绩不是浮点数等。 2. 排序函数中的比较逻辑要确保准确,特别是对于总分相同的情况。 3. 在进行修改和删除操作时,要确保找到对应的学生信息,避免出现误操作。 [2024-12-12 11:57:52 | AI写代码神器 | 1203点数解答]