学生成绩表信息包括学号、姓名、各科课程成绩(语文、数学、英语、政治)和总分。用带头结点的单链表管理学生成绩表,每个学生的信息依次从键盘输入,并根据需要进行插入、删除、排序、输出等操作。
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()
原文地址:https://blog.csdn.net/weixin_57111012/article/details/134618580
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.7code.cn/show_7993.html
如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。