用python写一段代码完成以下功能。 设计一个学生信息管理系统,包括学生成绩信息数据的求和、排序、查找、模糊查找、分类统计、输出、读写文件等基本操作,要求编程实现如下菜单驱动的学生信息管理系统: (1)录入每个学生的学号、姓名、学院和各科考试成绩; (2)计算每个学生的总分和平均分; (3)计算每门课程的总分和平均分; (4)计算每个学院的学生人数; (5)按每个学生的总分由高到低排出名次表; (6)按姓名的字典顺序排出成绩表; (7)按学号查询学生排名及其考试成绩; (8)按姓名查询学生排名及其考试成绩; (9)模糊查找:打印出学号以用户输入的数字开头的学生的信息; (10)按A(90~100)、B(70~89)、C(0~69)3个类别,对每门课程分别统计每个类别的人数以及所占的百分比; (11)输出每个学生的学号、姓名、学院、各科考试成绩,以及所有课程的总分、平均分、排名; (12)将每个学生的记录信息写入文件; (13)从文件中读出每个学生的记录信息并显示;
时间: 2023-12-16 17:26:24 浏览: 91
以下是一个简单的Python代码,实现了学生信息管理系统的基本功能,包括录入学生信息、计算学生总分和平均分、计算每门课程的总分和平均分、按每个学生的总分排序、按姓名的字典顺序排序、按学号和姓名查询学生信息、模糊查找、分类统计、输出学生信息、写入文件和读取文件等操作。需要注意的是,这只是一个简单的示例,实际情况下需要根据具体需求进行修改和完善。
```python
import operator
students = []
def input_student():
while True:
id = input("请输入学生学号(输入0结束录入):")
if id == "0":
break
name = input("请输入学生姓名:")
college = input("请输入学生学院:")
scores = []
for i in range(3):
score = input("请输入第{}门课的成绩:".format(i+1))
scores.append(int(score))
student = {"id": id, "name": name, "college": college, "scores": scores}
students.append(student)
def cal_total_avg():
for student in students:
total = sum(student["scores"])
avg = total / len(student["scores"])
student["total"] = total
student["avg"] = avg
def cal_course_avg():
course_scores = [[0, 0] for _ in range(3)] # 每门课程的总分和人数
for student in students:
for i in range(3):
course_scores[i][0] += student["scores"][i]
course_scores[i][1] += 1
course_avgs = [score[0] / score[1] for score in course_scores]
return course_scores, course_avgs
def cal_college_num():
college_nums = {}
for student in students:
college = student["college"]
if college in college_nums:
college_nums[college] += 1
else:
college_nums[college] = 1
return college_nums
def sort_by_total():
students.sort(key=operator.itemgetter("total"), reverse=True)
def sort_by_name():
students.sort(key=operator.itemgetter("name"))
def query_by_id(id):
for i, student in enumerate(students):
if student["id"] == id:
return i, student
return -1, None
def query_by_name(name):
for i, student in enumerate(students):
if student["name"] == name:
return i, student
return -1, None
def fuzzy_query(num):
for student in students:
if student["id"].startswith(num):
print(student)
def classify():
grades = {"A": [90, 100], "B": [70, 89], "C": [0, 69]}
for i in range(3):
for grade in grades:
count = sum(1 for student in students if student["scores"][i] >= grades[grade][0] and student["scores"][i] <= grades[grade][1])
percent = count / len(students) * 100
print("第{}门课,{}级别人数:{},所占百分比:{:.2f}%".format(i+1, grade, count, percent))
def output_student():
for student in students:
print("学号:{},姓名:{},学院:{},成绩:{},总分:{},平均分:{},排名:{}".format(student["id"], student["name"], student["college"], student["scores"], student["total"], student["avg"], students.index(student)+1))
def write_to_file():
with open("students.txt", "w") as f:
for student in students:
f.write("{},{},{},{},{},{},{},{},{}\n".format(student["id"], student["name"], student["college"], student["scores"][0], student["scores"][1], student["scores"][2], student["total"], student["avg"], students.index(student)+1))
def read_from_file():
global students
students = []
with open("students.txt", "r") as f:
for line in f:
data = line.strip().split(",")
student = {"id": data[0], "name": data[1], "college": data[2], "scores": [int(data[3]), int(data[4]), int(data[5])], "total": int(data[6]), "avg": float(data[7])}
students.append(student)
while True:
print("学生信息管理系统")
print("1. 录入学生信息")
print("2. 计算学生总分和平均分")
print("3. 计算每门课程的总分和平均分")
print("4. 计算每个学院的学生人数")
print("5. 按每个学生的总分由高到低排出名次表")
print("6. 按姓名的字典顺序排出成绩表")
print("7. 按学号查询学生排名及其考试成绩")
print("8. 按姓名查询学生排名及其考试成绩")
print("9. 模糊查找")
print("10. 分类统计")
print("11. 输出每个学生的信息")
print("12. 写入文件")
print("13. 从文件读取")
print("0. 退出")
choice = input("请输入您的选择:")
if choice == "1":
input_student()
elif choice == "2":
cal_total_avg()
elif choice == "3":
course_scores, course_avgs = cal_course_avg()
for i in range(3):
print("第{}门课的总分为{},平均分为{:.2f}".format(i+1, course_scores[i][0], course_avgs[i]))
elif choice == "4":
college_nums = cal_college_num()
for college in college_nums:
print("{}学院有{}名学生".format(college, college_nums[college]))
elif choice == "5":
sort_by_total()
output_student()
elif choice == "6":
sort_by_name()
output_student()
elif choice == "7":
id = input("请输入学号:")
i, student = query_by_id(id)
if i == -1:
print("学生不存在")
else:
print("排名:{},成绩:{}".format(i+1, student["scores"]))
elif choice == "8":
name = input("请输入姓名:")
i, student = query_by_name(name)
if i == -1:
print("学生不存在")
else:
print("排名:{},成绩:{}".format(i+1, student["scores"]))
elif choice == "9":
num = input("请输入数字:")
fuzzy_query(num)
elif choice == "10":
classify()
elif choice == "11":
output_student()
elif choice == "12":
write_to_file()
elif choice == "13":
read_from_file()
elif choice == "0":
break
else:
print("无效选择,请重新输入")
```
阅读全文