给出含有n个学生的成绩表,每条记录由学号、姓名和分数组成,编写具体函数完成下列操作:设计一个菜单操作界面,实现录入学生基本信息,直接插入排序,折半插入排序,冒泡排序,快速排序,简单选择排序,输出学生信
时间: 2023-09-14 07:15:59 浏览: 213
息,查询指定学生信息,以及退出系统等功能。
以下是一个可能的实现:
```python
class Student:
def __init__(self, id, name, score):
self.id = id
self.name = name
self.score = score
def __str__(self):
return f"ID: {self.id}, Name: {self.name}, Score: {self.score}"
class ScoreTable:
def __init__(self):
self.students = []
def add_student(self):
id = int(input("Enter student ID: "))
name = input("Enter student name: ")
score = int(input("Enter student score: "))
student = Student(id, name, score)
self.students.append(student)
print("Student added.")
def direct_insertion_sort(self):
for i in range(1, len(self.students)):
temp = self.students[i]
j = i - 1
while j >= 0 and self.students[j].id > temp.id:
self.students[j + 1] = self.students[j]
j -= 1
self.students[j + 1] = temp
print("Direct insertion sort finished.")
def binary_insertion_sort(self):
for i in range(1, len(self.students)):
temp = self.students[i]
left, right = 0, i - 1
while left <= right:
mid = (left + right) // 2
if self.students[mid].id < temp.id:
left = mid + 1
else:
right = mid - 1
for j in range(i - 1, right, -1):
self.students[j + 1] = self.students[j]
self.students[right + 1] = temp
print("Binary insertion sort finished.")
def bubble_sort(self):
for i in range(len(self.students) - 1):
for j in range(len(self.students) - 1 - i):
if self.students[j].id > self.students[j + 1].id:
self.students[j], self.students[j + 1] = self.students[j + 1], self.students[j]
print("Bubble sort finished.")
def quick_sort(self, left, right):
if left >= right:
return
pivot = self.students[left].id
i, j = left, right
while i < j:
while i < j and self.students[j].id >= pivot:
j -= 1
while i < j and self.students[i].id <= pivot:
i += 1
if i < j:
self.students[i], self.students[j] = self.students[j], self.students[i]
self.students[left], self.students[i] = self.students[i], self.students[left]
self.quick_sort(left, i - 1)
self.quick_sort(i + 1, right)
print("Quick sort finished.")
def selection_sort(self):
for i in range(len(self.students) - 1):
min_index = i
for j in range(i + 1, len(self.students)):
if self.students[j].id < self.students[min_index].id:
min_index = j
self.students[i], self.students[min_index] = self.students[min_index], self.students[i]
print("Selection sort finished.")
def print_students(self):
for student in self.students:
print(student)
def query_student(self):
id = int(input("Enter student ID: "))
for student in self.students:
if student.id == id:
print(student)
return
print("Student not found.")
def menu(self):
while True:
print("Score Table Management System")
print("1. Add student")
print("2. Direct insertion sort")
print("3. Binary insertion sort")
print("4. Bubble sort")
print("5. Quick sort")
print("6. Selection sort")
print("7. Print students")
print("8. Query student")
print("9. Exit")
choice = int(input("Enter your choice: "))
if choice == 1:
self.add_student()
elif choice == 2:
self.direct_insertion_sort()
elif choice == 3:
self.binary_insertion_sort()
elif choice == 4:
self.bubble_sort()
elif choice == 5:
self.quick_sort(0, len(self.students) - 1)
elif choice == 6:
self.selection_sort()
elif choice == 7:
self.print_students()
elif choice == 8:
self.query_student()
elif choice == 9:
print("Goodbye!")
break
else:
print("Invalid choice.")
if __name__ == '__main__':
score_table = ScoreTable()
score_table.menu()
```
这个程序定义了一个 `Student` 类来表示学生信息,以及一个 `ScoreTable` 类来管理学生信息表。`ScoreTable` 类提供了各种方法来实现不同的操作,包括录入学生信息、直接插入排序、折半插入排序、冒泡排序、快速排序、简单选择排序、输出学生信息、查询指定学生信息等。
在 `menu` 方法中,我们使用一个循环来接受用户的选择,然后分别调用相应的方法来完成操作。用户可以一直操作直到选择退出系统为止。
阅读全文