用python设计编程实现如下菜单驱动的学生信息管理系统: (1)录入每个学生的学号、姓名、学院和各科考试成绩; (2)计算每个学生的总分和平均分; (3)计算每门课程的总分和平均分; (4)计算每个学院的学生人数; (5)按每个学生的总分由高到低排出名次表; (6)按姓名的字典顺序排出成绩表; (7)按学号查询学生排名及其考试成绩; (8)按姓名查询学生排名及其考试成绩; (9)模糊查找:打印出学号以用户输入的数字开头的学生的信息; (10)按A(90~100)、B(70~89)、C(0~69)3个类别,对每门课程分别统计每个类别的人数以及所占的百分比; (11)输出每个学生的学号、姓名、学院、各科考试成绩,以及所有课程的总分、平均分、排名; (12)将每个学生的记录信息写入文件; (13)从文件中读出每个学生的记录信息并显示;
时间: 2024-01-26 12:02:50 浏览: 99
以下是一个简单的学生信息管理系统的Python代码实现:
```python
import operator
class Student:
def __init__(self, id, name, college, score_list):
self.id = id
self.name = name
self.college = college
self.score_list = score_list
self.total_score = sum(score_list)
self.average_score = self.total_score / len(score_list)
def __str__(self):
return f"ID: {self.id}, Name: {self.name}, College: {self.college}, Scores: {self.score_list}, Total Score: {self.total_score}, Average Score: {self.average_score}"
class StudentSystem:
def __init__(self):
self.students = []
self.college_dict = {}
self.id_dict = {}
self.name_dict = {}
def add_student(self, id, name, college, score_list):
student = Student(id, name, college, score_list)
self.students.append(student)
if college in self.college_dict:
self.college_dict[college] += 1
else:
self.college_dict[college] = 1
self.id_dict[id] = student
self.name_dict[name] = student
def calculate_course_score(self):
course_dict = {}
for student in self.students:
for i in range(len(student.score_list)):
if i in course_dict:
course_dict[i].append(student.score_list[i])
else:
course_dict[i] = [student.score_list[i]]
for key, value in course_dict.items():
total_score = sum(value)
average_score = total_score / len(value)
print(f"Course {key+1}: Total Score: {total_score}, Average Score: {average_score}")
def calculate_college_student_number(self):
for college, student_number in self.college_dict.items():
print(f"College {college}: Student Number: {student_number}")
def sort_by_total_score(self):
sorted_students = sorted(self.students, key=operator.attrgetter('total_score'), reverse=True)
for i, student in enumerate(sorted_students):
print(f"Rank {i+1}: {student}")
def sort_by_name(self):
sorted_students = sorted(self.students, key=operator.attrgetter('name'))
for i, student in enumerate(sorted_students):
print(f"Rank {i+1}: {student}")
def search_by_id(self, id):
if id in self.id_dict:
student = self.id_dict[id]
rank = self.get_rank(student)
print(f"Rank {rank}: {student}")
else:
print(f"Student ID {id} not found.")
def search_by_name(self, name):
if name in self.name_dict:
student = self.name_dict[name]
rank = self.get_rank(student)
print(f"Rank {rank}: {student}")
else:
print(f"Student name {name} not found.")
def search_by_prefix(self, prefix):
for student in self.students:
if student.id.startswith(prefix):
print(student)
def calculate_course_category(self):
for i in range(len(self.students[0].score_list)):
A_count = 0
B_count = 0
C_count = 0
for student in self.students:
score = student.score_list[i]
if score >= 90:
A_count += 1
elif score >= 70:
B_count += 1
else:
C_count += 1
total_count = A_count + B_count + C_count
A_percent = A_count / total_count * 100
B_percent = B_count / total_count * 100
C_percent = C_count / total_count * 100
print(f"Course {i+1}: A: {A_count} ({A_percent:.2f}%), B: {B_count} ({B_percent:.2f}%), C: {C_count} ({C_percent:.2f}%)")
def get_rank(self, student):
sorted_students = sorted(self.students, key=operator.attrgetter('total_score'), reverse=True)
rank = 1
for s in sorted_students:
if s.total_score > student.total_score:
rank += 1
elif s.total_score == student.total_score and s.id != student.id:
rank += 1
return rank
def output_all_students(self):
for student in self.students:
rank = self.get_rank(student)
print(f"Rank {rank}: {student}")
def write_to_file(self, filename):
with open(filename, 'w') as f:
for student in self.students:
f.write(f"{student.id}, {student.name}, {student.college}, {','.join(map(str, student.score_list))}\n")
def read_from_file(self, filename):
with open(filename, 'r') as f:
lines = f.readlines()
for line in lines:
parts = line.strip().split(',')
id = parts[0].strip()
name = parts[1].strip()
college = parts[2].strip()
score_list = list(map(int, parts[3:]))
self.add_student(id, name, college, score_list)
# Example usage:
system = StudentSystem()
system.add_student("1001", "Alice", "Engineering", [85, 90, 92])
system.add_student("1002", "Bob", "Science", [80, 85, 88])
system.add_student("1003", "Charlie", "Engineering", [90, 92, 95])
system.add_student("1004", "David", "Arts", [75, 80, 82])
system.add_student("1005", "Eve", "Science", [70, 75, 78])
system.calculate_course_score()
system.calculate_college_student_number()
system.sort_by_total_score()
system.sort_by_name()
system.search_by_id("1003")
system.search_by_name("Alice")
system.search_by_prefix("10")
system.calculate_course_category()
system.output_all_students()
system.write_to_file("students.txt")
system.read_from_file("students.txt")
```
这个程序中,我们定义了一个 `Student` 类,用于存储每个学生的信息,包括学号、姓名、学院和各科考试成绩。我们还定义了一个 `StudentSystem` 类,用于管理学生信息,包括添加学生、计算总分和平均分、计算每门课程的总分和平均分、计算每个学院的学生人数、按总分排序、按姓名排序、按学号查询学生排名及其考试成绩、按姓名查询学生排名及其考试成绩、模糊查找、按类别统计每门课程、输出每个学生的信息、将学生信息写入文件以及从文件中读取学生信息等操作。
在这个程序中,我们使用了一些Python的内置函数和库,如 `operator` 模块、 `sorted()` 函数和文件读写操作等。
当然,这个程序还有很多可以改进的地方,比如可以添加更多的输入验证、错误处理和用户交互等功能,以提高程序的健壮性和用户体验。
阅读全文