设计一个学生类,包含学号、姓名、语文成绩、数学成绩和英语成绩等数据成员。 引入OS模块,用于导入导出路径文件信息 设计一个学生管理类,用于定义各个模块的具体功能函数。 设计一个主菜单,两个子菜单,分别管理学生基本信息和学生成绩信息
时间: 2024-03-09 09:45:59 浏览: 65
好的,根据您的要求,我为您设计了以下代码,实现了一个基本的学生信息和成绩管理系统。由于这只是一个简单的示例,代码中可能存在一些不足之处,仅供参考。
```python
import os
class Student:
def __init__(self, id, name, chinese, math, english):
self.id = id
self.name = name
self.chinese = chinese
self.math = math
self.english = english
class StudentManager:
def __init__(self):
self.students = []
def load_students(self, path):
if not os.path.exists(path):
print("文件不存在!")
return
with open(path, "r", encoding="utf-8") as f:
lines = f.readlines()
for line in lines:
items = line.strip().split(",")
if len(items) == 5:
student = Student(items[0], items[1], float(items[2]), float(items[3]), float(items[4]))
self.students.append(student)
def save_students(self, path):
with open(path, "w", encoding="utf-8") as f:
for student in self.students:
f.write("{},{},{},{},{}\n".format(student.id, student.name, student.chinese, student.math, student.english))
def add_student(self, student):
self.students.append(student)
def remove_student(self, id):
for i, student in enumerate(self.students):
if student.id == id:
del self.students[i]
return True
return False
def modify_student(self, id, name, chinese, math, english):
for student in self.students:
if student.id == id:
student.name = name
student.chinese = chinese
student.math = math
student.english = english
return True
return False
def query_student(self, id):
for student in self.students:
if student.id == id:
return student
return None
def query_all_students(self):
return self.students
def get_chinese_max(self):
max_score = 0
for student in self.students:
if student.chinese > max_score:
max_score = student.chinese
return max_score
def get_chinese_min(self):
min_score = 100
for student in self.students:
if student.chinese < min_score:
min_score = student.chinese
return min_score
def get_chinese_avg(self):
total_score = 0
for student in self.students:
total_score += student.chinese
return total_score / len(self.students)
def get_math_max(self):
max_score = 0
for student in self.students:
if student.math > max_score:
max_score = student.math
return max_score
def get_math_min(self):
min_score = 100
for student in self.students:
if student.math < min_score:
min_score = student.math
return min_score
def get_math_avg(self):
total_score = 0
for student in self.students:
total_score += student.math
return total_score / len(self.students)
def get_english_max(self):
max_score = 0
for student in self.students:
if student.english > max_score:
max_score = student.english
return max_score
def get_english_min(self):
min_score = 100
for student in self.students:
if student.english < min_score:
min_score = student.english
return min_score
def get_english_avg(self):
total_score = 0
for student in self.students:
total_score += student.english
return total_score / len(self.students)
def main_menu():
print("1. 学生基本信息管理")
print("2. 学生成绩管理")
print("0. 退出系统")
def student_menu():
print("1. 显示所有学生信息")
print("2. 添加学生信息")
print("3. 删除学生信息")
print("4. 修改学生信息")
print("5. 查找学生信息")
print("0. 返回主菜单")
def grade_menu():
print("1. 统计语文成绩")
print("2. 统计数学成绩")
print("3. 统计英语成绩")
print("0. 返回主菜单")
def input_student_info():
id = input("请输入学号:")
name = input("请输入姓名:")
chinese = float(input("请输入语文成绩:"))
math = float(input("请输入数学成绩:"))
english = float(input("请输入英语成绩:"))
return Student(id, name, chinese, math, english)
def print_student_info(student):
print("学号:{},姓名:{},语文成绩:{},数学成绩:{},英语成绩:{}".format(student.id, student.name, student.chinese, student.math, student.english))
if __name__ == "__main__":
manager = StudentManager()
while True:
main_menu()
choice = input("请选择操作:")
if choice == "1":
while True:
student_menu()
choice = input("请选择操作:")
if choice == "1":
students = manager.query_all_students()
for student in students:
print_student_info(student)
elif choice == "2":
student = input_student_info()
manager.add_student(student)
print("添加成功!")
elif choice == "3":
id = input("请输入要删除的学生学号:")
if manager.remove_student(id):
print("删除成功!")
else:
print("删除失败,学号不存在!")
elif choice == "4":
id = input("请输入要修改的学生学号:")
student = manager.query_student(id)
if student is not None:
print_student_info(student)
name = input("请输入修改后的姓名:")
chinese = float(input("请输入修改后的语文成绩:"))
math = float(input("请输入修改后的数学成绩:"))
english = float(input("请输入修改后的英语成绩:"))
if manager.modify_student(id, name, chinese, math, english):
print("修改成功!")
else:
print("修改失败!")
else:
print("查无此人!")
elif choice == "5":
id = input("请输入要查找的学生学号:")
student = manager.query_student(id)
if student is not None:
print_student_info(student)
else:
print("查无此人!")
elif choice == "0":
break
elif choice == "2":
while True:
grade_menu()
choice = input("请选择操作:")
if choice == "1":
print("语文最高分:{}".format(manager.get_chinese_max()))
print("语文最低分:{}".format(manager.get_chinese_min()))
print("语文平均分:{}".format(manager.get_chinese_avg()))
elif choice == "2":
print("数学最高分:{}".format(manager.get_math_max()))
print("数学最低分:{}".format(manager.get_math_min()))
print("数学平均分:{}".format(manager.get_math_avg()))
elif choice == "3":
print("英语最高分:{}".format(manager.get_english_max()))
print("英语最低分:{}".format(manager.get_english_min()))
print("英语平均分:{}".format(manager.get_english_avg()))
elif choice == "0":
break
elif choice == "0":
break
```
阅读全文