某班最多不超过30人(具体人数由键盘输入)参加期末考试,最多不超过6门(具体 门数由键盘输入)。请使用模块化程序设计方法编程实现如下菜单驱动的学生成绩管理系统:(1)输入每个学生的学号、姓名和各科考试成绩; (2)计算每门课程的总分和平均分; (3)计算每个学生的总分和平均分; (4)按每个学生的总分由高到低排出名次表; (5)按每个学生的总分由低到高排出名次表; (6)按学号由小到大排出成绩表; (7)按学号查询学生排名及其考试成绩;(8)按姓名查询学生排名及其考试成绩;(9)按优秀(90~100、良好(80~89)、中等(70~79)、及格(60~69)、不及格(0~59) 5个类别,对每门课程分别统计每个类别的人数以及所占的百分比;(10)输出每个学生的学号、姓名、各科考试成绩,以及每门课程的总分和平均分。
时间: 2023-12-22 10:05:42 浏览: 187
设计一个菜单驱动的学生成绩管理程序
以下是一个可能的实现,包含了菜单驱动的主程序和多个模块化的函数。代码中注释解释了每个函数的作用。请注意,此代码块仅为示例,可能需要根据具体需求进行修改和调整。
```python
# 定义全局变量,用于存储学生信息和成绩数据
students = []
num_students = 0
num_courses = 0
course_names = []
scores = [[]]
def main():
global num_students, num_courses, course_names, scores
# 打印菜单并等待用户输入选择
while True:
print("\n学生成绩管理系统")
print("1. 输入学生信息和成绩")
print("2. 统计各门课程总分和平均分")
print("3. 统计每个学生的总分和平均分")
print("4. 按总分排名(由高到低)")
print("5. 按总分排名(由低到高)")
print("6. 按学号排序")
print("7. 按学号或姓名查询排名和成绩")
print("8. 统计优秀、良好、中等、及格、不及格人数和百分比")
print("9. 输出成绩单")
print("0. 退出系统")
choice = input("请选择操作:")
if choice == "1":
num_students, num_courses, course_names, scores = input_scores()
students = init_students(num_students)
input_student_info(students)
input_scores_info(students, num_courses, course_names, scores)
elif choice == "2":
course_totals, course_averages = calc_course_stats(num_students, num_courses, scores)
print_course_stats(course_names, course_totals, course_averages)
elif choice == "3":
student_totals, student_averages = calc_student_stats(num_students, num_courses, scores)
print_student_stats(students, student_totals, student_averages)
elif choice == "4":
sorted_students = sort_students_by_total(students, num_students, num_courses, scores, reverse=True)
print_ranking(sorted_students)
elif choice == "5":
sorted_students = sort_students_by_total(students, num_students, num_courses, scores, reverse=False)
print_ranking(sorted_students)
elif choice == "6":
sorted_students = sort_students_by_id(students, num_students)
print_student_info(sorted_students)
elif choice == "7":
query_student(students, num_students)
elif choice == "8":
stats_by_score_range = calc_stats_by_score_range(num_students, num_courses, scores)
print_score_range_stats(stats_by_score_range)
elif choice == "9":
print_all_scores(students, num_students, num_courses, course_names, scores)
elif choice == "0":
print("谢谢使用,再见!")
break
else:
print("输入错误,请重新选择。")
def input_scores():
# 输入学生数和课程数
num_students = int(input("请输入学生数:"))
num_courses = int(input("请输入课程数:"))
# 输入每门课程的名称
course_names = []
for i in range(num_courses):
name = input("请输入第{}门课程的名称:".format(i+1))
course_names.append(name)
# 初始化成绩矩阵
scores = [[0] * num_courses for i in range(num_students)]
return num_students, num_courses, course_names, scores
def init_students(num_students):
# 初始化学生信息
students = []
for i in range(num_students):
student = {"id": "", "name": ""}
students.append(student)
return students
def input_student_info(students):
# 输入每个学生的学号和姓名
for i, student in enumerate(students):
student["id"] = input("请输入第{}个学生的学号:".format(i+1))
student["name"] = input("请输入第{}个学生的姓名:".format(i+1))
def input_scores_info(students, num_courses, course_names, scores):
# 输入每个学生的每门成绩
for i, student in enumerate(students):
print("请输入{}的成绩:".format(student["name"]))
for j in range(num_courses):
score = float(input("{}:".format(course_names[j])))
scores[i][j] = score
def calc_course_stats(num_students, num_courses, scores):
# 计算每门课程的总分和平均分
course_totals = [0] * num_courses
course_averages = [0] * num_courses
for i in range(num_courses):
total = sum([scores[j][i] for j in range(num_students)])
average = total / num_students
course_totals[i] = total
course_averages[i] = average
return course_totals, course_averages
def print_course_stats(course_names, course_totals, course_averages):
# 打印每门课程的总分和平均分
print("课程成绩统计")
print("{:<10}{:<10}{:<10}".format("课程名称", "总分", "平均分"))
for i in range(len(course_names)):
print("{:<10}{:<10.2f}{:<10.2f}".format(course_names[i], course_totals[i], course_averages[i]))
def calc_student_stats(num_students, num_courses, scores):
# 计算每个学生的总分和平均分
student_totals = [0] * num_students
student_averages = [0] * num_students
for i in range(num_students):
total = sum(scores[i])
average = total / num_courses
student_totals[i] = total
student_averages[i] = average
return student_totals, student_averages
def print_student_stats(students, student_totals, student_averages):
# 打印每个学生的总分和平均分
print("学生成绩统计")
print("{:<10}{:<10}{:<10}{:<10}".format("学号", "姓名", "总分", "平均分"))
for i in range(len(students)):
print("{:<10}{:<10}{:<10.2f}{:<10.2f}".format(students[i]["id"], students[i]["name"], student_totals[i], student_averages[i]))
def sort_students_by_total(students, num_students, num_courses, scores, reverse=False):
# 按总分排序
sorted_students = students.copy()
sorted_students.sort(key=lambda x: sum(scores[students.index(x)]), reverse=reverse)
return sorted_students
def print_ranking(sorted_students):
# 打印排名表
print("排名表")
print("{:<10}{:<10}{:<10}{:<10}".format("排名", "学号", "姓名", "总分"))
for i, student in enumerate(sorted_students):
print("{:<10}{:<10}{:<10}{:<10.2f}".format(i+1, student["id"], student["name"], sum(scores[students.index(student)])))
def sort_students_by_id(students, num_students):
# 按学号排序
sorted_students = students.copy()
sorted_students.sort(key=lambda x: x["id"])
return sorted_students
def print_student_info(sorted_students):
# 打印学生信息和成绩
print("{:<10}{:<10}{:<20}".format("学号", "姓名", "成绩"))
for student in sorted_students:
scores_str = " ".join([str(score) for score in scores[students.index(student)]])
print("{:<10}{:<10}{:<20}".format(student["id"], student["name"], scores_str))
def query_student(students, num_students):
# 按学号或姓名查询学生排名和成绩
keyword = input("请输入学号或姓名:")
found_students = []
for student in students:
if keyword in student["id"] or keyword in student["name"]:
found_students.append(student)
if len(found_students) == 0:
print("未找到匹配的学生。")
elif len(found_students) == 1:
ranking = students.index(found_students[0]) + 1
scores_str = " ".join([str(score) for score in scores[students.index(found_students[0])]])
print("{:<10}{:<10}{:<20}".format("学号", "姓名", "成绩"))
print("{:<10}{:<10}{:<20}".format(found_students[0]["id"], found_students[0]["name"], scores_str))
print("排名:{}".format(ranking))
else:
print("找到以下{}个匹配的学生:".format(len(found_students)))
for i, student in enumerate(found_students):
print("{:<3}{:<10}{:<10}".format(i+1, student["id"], student["name"]))
def calc_stats_by_score_range(num_students, num_courses, scores):
# 按成绩范围统计人数和百分比
stats_by_score_range = {}
for i in range(num_courses):
stats_by_score_range[i] = {"优秀": 0, "良好": 0, "中等": 0, "及格": 0, "不及格": 0}
for j in range(num_students):
score = scores[j][i]
if score >= 90:
stats_by_score_range[i]["优秀"] += 1
elif score >= 80:
stats_by_score_range[i]["良好"] += 1
elif score >= 70:
stats_by_score_range[i]["中等"] += 1
elif score >= 60:
stats_by_score_range[i]["及格"] += 1
else:
stats_by_score_range[i]["不及格"] += 1
for key in stats_by_score_range[i]:
stats_by_score_range[i][key] = "{} ({:.2f}%)".format(stats_by_score_range[i][key], stats_by_score_range[i][key] / num_students * 100)
return stats_by_score_range
def print_score_range_stats(stats_by_score_range):
# 打印成绩范围统计结果
print("成绩范围统计")
print("{:<10}{:<10}{:<10}{:<10}{:<10}{:<10}".format("课程名称", "优秀", "良好", "中等", "及格", "不及格"))
for i in range(num_courses):
print("{:<10}{:<10}{:<10}{:<10}{:<10}{:<10}".format(course_names[i], stats_by_score_range[i]["优秀"], stats_by_score_range[i]["良好"], stats_by_score_range[i]["中等"], stats_by_score_range[i]["及格"], stats_by_score_range[i]["不及格"]))
def print_all_scores(students, num_students, num_courses, course_names, scores):
# 打印成绩单
print("{:<10}{:<10}{:<20}".format("学号", "姓名", "成绩"))
for i, student in enumerate(students):
scores_str = " ".join([str(score) for score in scores[i]])
total = sum(scores[i])
average = total / num_courses
print("{:<10}{:<10}{:<20}{:<10}{:<10.2f}".format(student["id"], student["name"], scores_str, total, average))
if __name__ == "__main__":
main()
```
阅读全文