基本信息管理模块的主要功能有学生信息的添加、删除、修改、显示和学生数据的导入导出,学生成绩管理模块的主要功能有统计课程最高分、最低分和平均分。要求按照面向对象思维进行编写,按照以下模式完成: 1、设计一个学生类,包含学号、姓名、语文成绩、数学成绩和英语成绩等数据成员。 2、引入OS模块,用于导入导出路径文件信息 3、设计一个学生管理类,用于定义各个模块的具体功能函数。 4、设计一个主菜单,两个子菜单,分别管理学生基本信息和学生成绩信息
时间: 2024-04-06 20:31:27 浏览: 105
学生成绩管理系统 系统功能基本要求 学生信息,学号、姓名、性别、专业、年级等
好的,根据您的要求,我可以给出一个简单的面向对象实现代码示例,供您参考。
```python
import os
class Student:
def __init__(self, id, name, chinese_score=0, math_score=0, english_score=0):
self.id = id
self.name = name
self.chinese_score = chinese_score
self.math_score = math_score
self.english_score = english_score
class FileManager:
def __init__(self):
self.path = os.getcwd() # 获取当前路径
def import_data(self, filename):
with open(os.path.join(self.path, filename), 'r') as f:
lines = f.readlines()
students = []
for line in lines:
data = line.strip().split(',')
id, name, chinese_score, math_score, english_score = data
student = Student(id, name, int(chinese_score), int(math_score), int(english_score))
students.append(student)
return students
def export_data(self, filename, data):
with open(os.path.join(self.path, filename), 'w') as f:
for student in data:
f.write(f"{student.id},{student.name},{student.chinese_score},{student.math_score},{student.english_score}\n")
class StudentManager:
def __init__(self):
self.students = []
def add_student(self, student):
self.students.append(student)
def remove_student(self, student):
self.students.remove(student)
def modify_student(self, student):
for i, s in enumerate(self.students):
if s.id == student.id:
self.students[i] = student
break
def show_students(self):
for student in self.students:
print(f"学号:{student.id},姓名:{student.name},语文成绩:{student.chinese_score},数学成绩:{student.math_score},英语成绩:{student.english_score}")
def get_highest_score(self, course):
scores = [getattr(student, course) for student in self.students]
return max(scores)
def get_lowest_score(self, course):
scores = [getattr(student, course) for student in self.students]
return min(scores)
def get_average_score(self, course):
scores = [getattr(student, course) for student in self.students]
return sum(scores) / len(scores)
class Menu:
def __init__(self):
self.file_manager = FileManager()
self.student_manager = StudentManager()
def run(self):
while True:
# 显示主菜单
print("1. 学生基本信息管理")
print("2. 学生成绩管理")
print("0. 退出系统")
choice = input("请输入您的选择:")
if choice == '1':
self.basic_info_menu()
elif choice == '2':
self.score_menu()
elif choice == '0':
break
def basic_info_menu(self):
while True:
# 显示子菜单
print("1. 添加学生信息")
print("2. 删除学生信息")
print("3. 修改学生信息")
print("4. 显示所有学生信息")
print("5. 导入学生信息")
print("6. 导出学生信息")
print("0. 返回上级菜单")
choice = input("请输入您的选择:")
if choice == '1':
id = input("请输入学生学号:")
name = input("请输入学生姓名:")
chinese_score = int(input("请输入学生语文成绩:"))
math_score = int(input("请输入学生数学成绩:"))
english_score = int(input("请输入学生英语成绩:"))
student = Student(id, name, chinese_score, math_score, english_score)
self.student_manager.add_student(student)
print("添加成功!")
elif choice == '2':
id = input("请输入要删除学生的学号:")
for student in self.student_manager.students:
if student.id == id:
self.student_manager.remove_student(student)
print("删除成功!")
break
else:
print("未找到该学生!")
elif choice == '3':
id = input("请输入要修改学生的学号:")
for student in self.student_manager.students:
if student.id == id:
name = input("请输入学生姓名:")
chinese_score = int(input("请输入学生语文成绩:"))
math_score = int(input("请输入学生数学成绩:"))
english_score = int(input("请输入学生英语成绩:"))
student.name = name
student.chinese_score = chinese_score
student.math_score = math_score
student.english_score = english_score
self.student_manager.modify_student(student)
print("修改成功!")
break
else:
print("未找到该学生!")
elif choice == '4':
self.student_manager.show_students()
elif choice == '5':
filename = input("请输入要导入的文件名:")
students = self.file_manager.import_data(filename)
self.student_manager.students.extend(students)
print("导入成功!")
elif choice == '6':
filename = input("请输入要导出的文件名:")
self.file_manager.export_data(filename, self.student_manager.students)
print("导出成功!")
elif choice == '0':
break
def score_menu(self):
while True:
# 显示子菜单
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':
highest_score = self.student_manager.get_highest_score('chinese_score')
print(f"语文最高分为:{highest_score}")
elif choice == '2':
lowest_score = self.student_manager.get_lowest_score('chinese_score')
print(f"语文最低分为:{lowest_score}")
elif choice == '3':
average_score = self.student_manager.get_average_score('chinese_score')
print(f"语文平均分为:{average_score:.2f}")
elif choice == '4':
highest_score = self.student_manager.get_highest_score('math_score')
print(f"数学最高分为:{highest_score}")
elif choice == '5':
lowest_score = self.student_manager.get_lowest_score('math_score')
print(f"数学最低分为:{lowest_score}")
elif choice == '6':
average_score = self.student_manager.get_average_score('math_score')
print(f"数学平均分为:{average_score:.2f}")
elif choice == '7':
highest_score = self.student_manager.get_highest_score('english_score')
print(f"英语最高分为:{highest_score}")
elif choice == '8':
lowest_score = self.student_manager.get_lowest_score('english_score')
print(f"英语最低分为:{lowest_score}")
elif choice == '9':
average_score = self.student_manager.get_average_score('english_score')
print(f"英语平均分为:{average_score:.2f}")
elif choice == '0':
break
if __name__ == '__main__':
menu = Menu()
menu.run()
```
以上代码实现了一个简单的学生基本信息管理和学生成绩管理程序,具体操作流程如下:
1. 运行程序,进入主菜单;
2. 选择子菜单“学生基本信息管理”;
3. 在子菜单中选择添加、删除、修改、显示、导入或导出学生信息;
4. 返回上级菜单;
5. 选择子菜单“学生成绩管理”;
6. 在子菜单中选择统计语文、数学或英语的最高分、最低分或平均分;
7. 返回上级菜单;
8. 退出程序。
希望对您有所帮助!
阅读全文