定义并创建一个包含学生信息(学号,姓名,成绩)的顺序表,使其具有如下功能: (1)根据指定学生个数,逐个输入学生信息并以顺序结构存储; (2)逐个显示学生表中所有学生的相关信息; (3)根据姓名进行查找,返回此学生的学号和成绩; (4)根据指定的位置可返回相应的学生信息(学号,姓名,成绩)(5)给定一个学生信息,插入到表中指定的位置; (6)删除指定位置的学生记录; (7)统计表中学生个数
时间: 2023-06-02 15:01:32 浏览: 150
定义一个包含学生信息(学号,姓名,成绩)的顺序表
以Python语言为例,代码如下:
```
class Student:
def __init__(self, id, name, score):
self.id = id
self.name = name
self.score = score
class StudentList:
def __init__(self):
self.students = []
def add(self, student):
self.students.append(student)
def show_all(self):
for student in self.students:
print(student.id, student.name, student.score)
def search_by_name(self, name):
for student in self.students:
if student.name == name:
return (student.id, student.score)
return None
def get_by_index(self, index):
if index < 0 or index >= len(self.students):
return None
student = self.students[index]
return (student.id, student.name, student.score)
def insert(self, index, student):
if index < 0 or index > len(self.students):
return None
self.students.insert(index, student)
def delete(self, index):
if index < 0 or index >= len(self.students):
return None
student = self.students.pop(index)
return (student.id, student.name, student.score)
def count(self):
return len(self.students)
# 测试代码
student_list = StudentList()
n = int(input("请输入学生个数:"))
for i in range(n):
id = input("请输入学号:")
name = input("请输入姓名:")
score = float(input("请输入成绩:"))
student = Student(id, name, score)
student_list.add(student)
student_list.show_all()
name = input("请输入要查找的姓名:")
result = student_list.search_by_name(name)
if result:
print("学号:", result[0], "成绩:", result[1])
else:
print("未找到该学生")
index = int(input("请输入要查找的位置:"))
result = student_list.get_by_index(index)
if result:
print("学号:", result[0], "姓名:", result[1], "成绩:", result[2])
else:
print("未找到该学生")
id = input("请输入要插入的学号:")
name = input("请输入要插入的姓名:")
score = float(input("请输入要插入的成绩:"))
student = Student(id, name, score)
index = int(input("请输入要插入的位置:"))
student_list.insert(index, student)
student_list.show_all()
index = int(input("请输入要删除的位置:"))
result = student_list.delete(index)
if result:
print("学号:", result[0], "姓名:", result[1], "成绩:", result[2], "已删除")
else:
print("未找到该学生")
print("学生个数:", student_list.count())
```
阅读全文