1.建立一张学生成绩表,每个学生包含 学号、姓名、性别、语文、数学、英语、总分、平均分。 2.将表中所有信息按总分降序排列。 3.按学号查找某学生所有成绩。 4.能够实现某学生的成绩插入和删除。 5.实现文件操作,程序退出时将学生成绩保存在studen_grad.txt文件中;程序开始时,能够从student_grad.txt文件中加载已有的学生成绩信息。
时间: 2023-12-03 12:47:46 浏览: 215
可以在文件里面存储学生信息,可以建立并删除学生成绩,可以按班级查询学生成绩.zip
好的,这个问题可以用Python语言来实现。我们可以定义一个类`Student`来表示学生信息,然后再定义一个类`GradeTable`来表示学生成绩表。下面是代码实现:
```python
import os
class Student:
def __init__(self, id, name, gender, chinese, math, english):
self.id = id
self.name = name
self.gender = gender
self.chinese = chinese
self.math = math
self.english = english
self.total_score = chinese + math + english
self.average_score = round(self.total_score / 3, 2)
class GradeTable:
def __init__(self):
self.students = []
def add_student(self, student):
self.students.append(student)
def remove_student(self, id):
for student in self.students:
if student.id == id:
self.students.remove(student)
return True
return False
def search_student(self, id):
for student in self.students:
if student.id == id:
return student
return None
def sort_by_total_score(self):
self.students.sort(key=lambda x: x.total_score, reverse=True)
def save_to_file(self, filename):
with open(filename, 'w') as f:
for student in self.students:
f.write(f"{student.id},{student.name},{student.gender},{student.chinese},{student.math},{student.english}\n")
def load_from_file(self, filename):
if os.path.exists(filename):
with open(filename, 'r') as f:
for line in f.readlines():
data = line.strip().split(',')
student = Student(data[0], data[1], data[2], int(data[3]), int(data[4]), int(data[5]))
self.add_student(student)
if __name__ == '__main__':
grade_table = GradeTable()
grade_table.load_from_file('student_grad.txt')
while True:
print("1.添加学生")
print("2.删除学生")
print("3.查找学生")
print("4.显示成绩排名")
print("5.退出程序")
choice = input("请选择操作:")
if choice == '1':
id = input("请输入学号:")
name = input("请输入姓名:")
gender = input("请输入性别:")
chinese = int(input("请输入语文成绩:"))
math = int(input("请输入数学成绩:"))
english = int(input("请输入英语成绩:"))
student = Student(id, name, gender, chinese, math, english)
grade_table.add_student(student)
print("添加成功!")
elif choice == '2':
id = input("请输入学号:")
if grade_table.remove_student(id):
print("删除成功!")
else:
print("学号不存在!")
elif choice == '3':
id = input("请输入学号:")
student = grade_table.search_student(id)
if student:
print(f"学号:{student.id},姓名:{student.name},性别:{student.gender},语文:{student.chinese},数学:{student.math},英语:{student.english},总分:{student.total_score},平均分:{student.average_score}")
else:
print("学号不存在!")
elif choice == '4':
grade_table.sort_by_total_score()
for i, student in enumerate(grade_table.students):
print(f"{i+1} 学号:{student.id},姓名:{student.name},总分:{student.total_score}")
elif choice == '5':
grade_table.save_to_file('student_grad.txt')
print("学生成绩已保存到文件!")
break
else:
print("输入错误,请重新输入!")
```
你可以将上述代码保存为一个Python文件,比如`grade_system.py`,然后在命令行中运行`python grade_system.py`来启动程序。
阅读全文