给出含有n个学生的成绩表,每条记录由学号、姓名和分数组成,设计程序完成下列操作:设计一个菜单操作界面,如下所示: 1、录入学生基本信息 2、直接插入排序3、折半插入排序4、冒泡排序5、快速排序6、简单选择排序7、输出学生信息8、退出
时间: 2023-07-10 18:26:47 浏览: 83
好的,以下是针对你的问题的回答:
为了完成这个程序,我们需要定义一个学生类,包含学号、姓名和分数属性。然后创建一个学生数组,用于存储所有学生的信息。接下来,我们可以编写一个菜单操作界面,其中包含上述的七种排序方法和一个退出选项。用户可以选择其中一种排序方法,程序将按照用户选择的方法对学生数组进行排序。排序完成后,用户可以选择输出所有学生的信息。
下面是一个简单的代码示例,供你参考:
```
#include <iostream>
#include <string>
using namespace std;
class Student {
public:
string id;
string name;
int score;
};
void inputStudent(Student* s, int n) {
for (int i = 0; i < n; i++) {
cout << "请输入第" << i+1 << "个学生的信息:" << endl;
cout << "学号:";
cin >> s[i].id;
cout << "姓名:";
cin >> s[i].name;
cout << "分数:";
cin >> s[i].score;
}
}
void outputStudent(Student* s, int n) {
for (int i = 0; i < n; i++) {
cout << "学号:" << s[i].id << " 姓名:" << s[i].name << " 分数:" << s[i].score << endl;
}
}
void insertSort(Student* s, int n) {
for (int i = 1; i < n; i++) {
int j = i - 1;
Student temp = s[i];
while (j >= 0 && s[j].score > temp.score) {
s[j + 1] = s[j];
j--;
}
s[j + 1] = temp;
}
}
void binaryInsertSort(Student* s, int n) {
for (int i = 1; i < n; i++) {
int left = 0, right = i - 1, mid;
Student temp = s[i];
while (left <= right) {
mid = (left + right) / 2;
if (s[mid].score > temp.score) {
right = mid - 1;
} else {
left = mid + 1;
}
}
for (int j = i - 1; j >= left; j--) {
s[j + 1] = s[j];
}
s[left] = temp;
}
}
void bubbleSort(Student* s, int n) {
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - 1 - i; j++) {
if (s[j].score > s[j + 1].score) {
Student temp = s[j];
s[j] = s[j + 1];
s[j + 1] = temp;
}
}
}
}
void quickSort(Student* s, int left, int right) {
if (left >= right) {
return;
}
int i = left, j = right;
Student pivot = s[left];
while (i < j) {
while (i < j && s[j].score >= pivot.score) {
j--;
}
s[i] = s[j];
while (i < j && s[i].score <= pivot.score) {
i++;
}
s[j] = s[i];
}
s[i] = pivot;
quickSort(s, left, i - 1);
quickSort(s, i + 1, right);
}
void selectSort(Student* s, int n) {
for (int i = 0; i < n - 1; i++) {
int minIndex = i;
for (int j = i + 1; j < n; j++) {
if (s[j].score < s[minIndex].score) {
minIndex = j;
}
}
if (minIndex != i) {
Student temp = s[i];
s[i] = s[minIndex];
s[minIndex] = temp;
}
}
}
int main() {
int n;
cout << "请输入学生人数:";
cin >> n;
Student* students = new Student[n];
int choice;
do {
cout << "请选择操作:" << endl;
cout << "1、录入学生基本信息" << endl;
cout << "2、直接插入排序" << endl;
cout << "3、折半插入排序" << endl;
cout << "4、冒泡排序" << endl;
cout << "5、快速排序" << endl;
cout << "6、简单选择排序" << endl;
cout << "7、输出学生信息" << endl;
cout << "8、退出" << endl;
cout << "请输入选项:";
cin >> choice;
switch (choice) {
case 1:
inputStudent(students, n);
break;
case 2:
insertSort(students, n);
cout << "排序完成!" << endl;
break;
case 3:
binaryInsertSort(students, n);
cout << "排序完成!" << endl;
break;
case 4:
bubbleSort(students, n);
cout << "排序完成!" << endl;
break;
case 5:
quickSort(students, 0, n - 1);
cout << "排序完成!" << endl;
break;
case 6:
selectSort(students, n);
cout << "排序完成!" << endl;
break;
case 7:
outputStudent(students, n);
break;
case 8:
cout << "谢谢使用!" << endl;
break;
default:
cout << "输入有误,请重新输入!" << endl;
break;
}
} while (choice != 8);
delete[] students;
return 0;
}
```
阅读全文