一个综合的学生成绩管理系统,要求能够管理若干个学生若干门课程成绩,需要实现以下功能:读取以数据文件(如图 3-1:student.txt)形式存储的学生信息(若没有数据文件则需创建新的数据文件):能浏览查看学生信息;能添加学生信息:能够修改和删除学生的基本信息:能计算每个学生的总分并进行排名:能统计每门课的最高分、最低分和平均分:能按照学号、姓名、名次等方式 查询学生信息:对学生信息的修改最终都能保存到数据文件中去
时间: 2024-03-08 13:47:30 浏览: 63
基于您提供的需求,一个简单的学生成绩管理系统大致可以分为以下模块:
1. 数据文件读写模块:负责读取和保存学生信息到数据文件中。
2. 学生信息管理模块:可以浏览、添加、修改和删除学生的基本信息。
3. 成绩计算和排名模块:计算每个学生的总分并进行排名,以及统计每门课的最高分、最低分和平均分。
4. 学生信息查询模块:可以按照学号、姓名、名次等方式查询学生信息。
下面是一个简单的Python实现示例,用于参考:
```python
import os
# 定义学生信息类
class Student:
def __init__(self, id, name, sex, age):
self.id = id
self.name = name
self.sex = sex
self.age = age
self.scores = {}
def add_score(self, course, score):
self.scores[course] = score
def get_total_score(self):
return sum(self.scores.values())
def __str__(self):
return f'{self.id}\t{self.name}\t{self.sex}\t{self.age}\t{self.get_total_score():.2f}'
# 数据文件读写模块
class DataManager:
def __init__(self, filename):
self.filename = filename
def read_data(self):
if not os.path.exists(self.filename):
with open(self.filename, 'w') as f:
pass
students = []
with open(self.filename, 'r') as f:
for line in f:
data = line.strip().split('\t')
id, name, sex, age = data[:4]
student = Student(id, name, sex, age)
scores = data[4:]
for i in range(0, len(scores), 2):
course, score = scores[i], float(scores[i+1])
student.add_score(course, score)
students.append(student)
return students
def save_data(self, students):
with open(self.filename, 'w') as f:
for student in students:
scores = [course + '\t' + str(score) for course, score in student.scores.items()]
line = '\t'.join([student.id, student.name, student.sex, student.age] + scores) + '\n'
f.write(line)
# 学生信息管理模块
class StudentManager:
def __init__(self, students):
self.students = students
def add_student(self, id, name, sex, age):
student = Student(id, name, sex, age)
self.students.append(student)
def modify_student(self, id, name, sex, age):
for student in self.students:
if student.id == id:
student.name = name
student.sex = sex
student.age = age
def delete_student(self, id):
for student in self.students:
if student.id == id:
self.students.remove(student)
def __str__(self):
return '\n'.join([str(student) for student in self.students])
# 成绩计算和排名模块
class ScoreManager:
def __init__(self, students):
self.students = students
self.course_scores = {}
def calc_course_scores(self):
for student in self.students:
for course, score in student.scores.items():
if course not in self.course_scores:
self.course_scores[course] = []
self.course_scores[course].append(score)
def get_course_stats(self, course):
scores = self.course_scores[course]
return max(scores), min(scores), sum(scores) / len(scores)
def calc_student_ranks(self):
total_scores = [student.get_total_score() for student in self.students]
ranks = [sorted(total_scores, reverse=True).index(score) + 1 for score in total_scores]
for student, rank in zip(self.students, ranks):
student.rank = rank
def __str__(self):
lines = []
for course in self.course_scores:
max_score, min_score, avg_score = self.get_course_stats(course)
lines.append(f'{course}: max={max_score:.2f}, min={min_score:.2f}, avg={avg_score:.2f}')
return '\n'.join(lines)
# 学生信息查询模块
class QueryManager:
def __init__(self, students):
self.students = students
def query_by_id(self, id):
for student in self.students:
if student.id == id:
return student
return None
def query_by_name(self, name):
result = []
for student in self.students:
if student.name == name:
result.append(student)
return result
def query_by_rank(self, rank):
result = []
for student in self.students:
if student.rank == rank:
result.append(student)
return result
# 主程序
def main():
# 初始化
data_manager = DataManager('student.txt')
students = data_manager.read_data()
student_manager = StudentManager(students)
score_manager = ScoreManager(students)
query_manager = QueryManager(students)
# 计算每门课的最高分、最低分和平均分
score_manager.calc_course_scores()
print(score_manager)
# 计算每个学生的总分并进行排名
score_manager.calc_student_ranks()
# 浏览查看学生信息
print(student_manager)
# 添加学生信息
student_manager.add_student('20210001', '张三', '男', 20)
data_manager.save_data(students)
# 修改学生信息
student_manager.modify_student('20210001', '李四', '女', 21)
data_manager.save_data(students)
# 删除学生信息
student_manager.delete_student('20210001')
data_manager.save_data(students)
# 查询学生信息
print(query_manager.query_by_id('20200001'))
print(query_manager.query_by_name('张三'))
print(query_manager.query_by_rank(1))
if __name__ == '__main__':
main()
```
上述代码实现了基本的学生成绩管理系统功能,您可以根据实际需求进行修改和扩展。同时,您需要注意数据的安全性和准确性,例如数据文件的备份和防止数据丢失等。
阅读全文