定义两个包含学生信息(学号,姓名,成绩)的顺序表,使其具有如下功能: (1) 根据指定学生个数,逐个输入学生信息; (2) 逐个显示学生表中所有学生的相关信息; (3) 合并两个顺序表,并根据学号进行排序; (4) 逐个
时间: 2023-05-28 22:05:04 浏览: 45
查找指定学生的信息并输出。
以下是Python代码实现:
```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 input_students(self, n):
for i in range(n):
id = input("请输入学号:")
name = input("请输入姓名:")
score = input("请输入成绩:")
student = Student(id, name, score)
self.students.append(student)
def display_students(self):
print("学号\t姓名\t成绩")
for student in self.students:
print(student.id + "\t" + student.name + "\t" + student.score)
def merge_sort(self, lst1, lst2):
i = 0
j = 0
result = []
while i < len(lst1) and j < len(lst2):
if lst1[i].id < lst2[j].id:
result.append(lst1[i])
i += 1
else:
result.append(lst2[j])
j += 1
result += lst1[i:]
result += lst2[j:]
return result
def search_student(self, id):
for student in self.students:
if student.id == id:
print("学号:" + student.id + "\t姓名:" + student.name + "\t成绩:" + student.score)
return
print("没有找到该学生!")
# 创建两个学生列表
list1 = StudentList()
list2 = StudentList()
# 输入第一个列表的学生信息
n1 = int(input("请输入第一个列表的学生个数:"))
list1.input_students(n1)
# 输入第二个列表的学生信息
n2 = int(input("请输入第二个列表的学生个数:"))
list2.input_students(n2)
# 显示两个列表的学生信息
print("第一个列表的学生信息:")
list1.display_students()
print("第二个列表的学生信息:")
list2.display_students()
# 合并两个列表并按学号排序
result = list1.merge_sort(list1.students, list2.students)
print("合并后的学生信息按学号排序:")
print("学号\t姓名\t成绩")
for student in result:
print(student.id + "\t" + student.name + "\t" + student.score)
# 查找指定学生的信息
id = input("请输入要查找的学生学号:")
list1.search_student(id)
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)