学生成绩表信息包括学号、姓名、各科课程成绩(语文、数学、英语、政治)和总分。用带头结点的单链表管理学生成绩表,每个学生的信息依次从键盘输入,并根据需要进行插入、删除、排序、输出等操作。
import json
# 初始化系统
students = []
# 结构体
class Student:
def __init__(self, ID, Name, Mark):
self.ID = ID
self.Name = Name
self.Mark = Mark
self.Total = sum(Mark)
self.Average = self.Total / len(Mark)
# 录入
def inputt():
global students
ID = input("请输入学号:")
Name = input("请输入姓名:")
Marks = []
for subject in ["数学", "英语", "语文"]:
mark = float(input(f"请输入{subject}成绩:"))
Marks.append(mark)
student = Student(ID, Name, Marks)
students.append(student)
print("录入成功。")
# 删除
def deletee():
global students
ID = input("请输入要删除的学生学号:")
found = False
for student in students:
if student.ID == ID:
students.remove(student)
found = True
print("删除成功。")
break
if not found:
print(f"未找到学号为{ID}的学生。")
# 修改
def modfiy():
global students
ID = input("请输入要修改的学生学号:")
found = False
for student in students:
if student.ID == ID:
Marks = []
for subject in ["数学", "英语", "语文"]:
mark = float(input(f"请输入新的{subject}成绩:"))
Marks.append(mark)
student.Mark = Marks
student.Total = sum(Marks)
student.Average = student.Total / len(Marks)
found = True
print("修改成功。")
break
if not found:
print(f"未找到学号为{ID}的学生。")
# 插入
def insert():
global students
position = int(input("请输入要插入的位置(1-{}):".format(len(students) + 1)))
if 1 <= position <= len(students) + 1:
ID = input("请输入学号:")
Name = input("请输入姓名:")
Marks = []
for subject in ["数学", "英语", "语文"]:
mark = float(input(f"请输入{subject}成绩:"))
Marks.append(mark)
student = Student(ID, Name, Marks)
students.insert(position - 1, student)
print("插入成功。")
else:
print("插入位置无效。")
# 排序
def sort():
global students
students.sort(key=lambda x: x.Average, reverse=True)
print("排序完成。")
# 显示
def display():
global students
if not students:
print("暂无学生信息。")
else:
print("学号\t姓名\t数学\t英语\t语文\t总分\t平均分")
for student in students:
print(f"{student.ID}\t{student.Name}\t{student.Mark[0]}\t{student.Mark[1]}\t{student.Mark[2]}\t{student.Total:.2f}\t{student.Average:.2f}")
# 查询
def Student_SearchByIndex():
global students
ID = input("请输入要查询的学生学号:")
found = False
for student in students:
if student.ID == ID:
print("学号\t姓名\t数学\t英语\t语文\t总分\t平均分")
print(f"{student.ID}\t{student.Name}\t{student.Mark[0]}\t{student.Mark[1]}\t{student.Mark[2]}\t{student.Total:.2f}\t{student.Average:.2f}")
found = True
break
if not found:
print(f"未找到学号为{ID}的学生。")
# 主函数
def main():
while True:
print("\n学生成绩管理系统")
print("1. 录入学生信息")
print("2. 删除学生信息")
print("3. 修改学生成绩")
print("4. 插入学生成绩")
print("5. 排序学生成绩")
print("6. 显示学生成绩")
print("7. 查询学生成绩")
print("8. 退出系统")
choice = input("请选择操作(1-8):")
if choice == "1":
inputt()
elif choice == "2":
deletee()
elif choice == "3":
modfiy()
elif choice == "4":
insert()
elif choice == "5":
sort()
elif choice == "6":
display()
elif choice == "7":
Student_SearchByIndex()
elif choice == "8":
break
else:
print("无效的选择,请重新输入。")
if __name__ == "__main__":
main()