给出含有n个学生的成绩表,每条记录由学号、姓名和分数组成,用C++语言编写具体函数完成下列操作:设计一个菜单操作界面,实现录入学生基本信息,直接插入排序,折半插入排序,冒泡排序,快速排序,简单选择排序,输出学生信息,退出等基本操作
时间: 2024-03-21 16:43:17 浏览: 89
好的,以下是一个基于C++语言的实现,具体代码如下:
```
#include <iostream>
#include <string>
using namespace std;
// 定义学生信息结构体
struct Student {
int id; // 学号
string name; // 姓名
int score; // 分数
};
// 录入学生信息
void inputStudentInfo(Student stu[], int n) {
for (int i = 0; i < n; i++) {
cout << "请输入第" << i + 1 << "个学生的学号、姓名和分数:";
cin >> stu[i].id >> stu[i].name >> stu[i].score;
}
}
// 直接插入排序
void insertionSort(Student stu[], int n) {
for (int i = 1; i < n; i++) {
Student temp = stu[i];
int j = i - 1;
while (j >= 0 && temp.score < stu[j].score) {
stu[j + 1] = stu[j];
j--;
}
stu[j + 1] = temp;
}
}
// 折半插入排序
void binaryInsertionSort(Student stu[], int n) {
for (int i = 1; i < n; i++) {
Student temp = stu[i];
int low = 0, high = i - 1;
while (low <= high) {
int mid = (low + high) / 2;
if (temp.score < stu[mid].score) {
high = mid - 1;
} else {
low = mid + 1;
}
}
for (int j = i - 1; j >= high + 1; j--) {
stu[j + 1] = stu[j];
}
stu[high + 1] = temp;
}
}
// 冒泡排序
void bubbleSort(Student stu[], int n) {
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (stu[j].score > stu[j + 1].score) {
Student temp = stu[j];
stu[j] = stu[j + 1];
stu[j + 1] = temp;
}
}
}
}
// 快速排序
void quickSort(Student stu[], int left, int right) {
if (left >= right) {
return;
}
Student pivot = stu[left];
int i = left, j = right;
while (i < j) {
while (i < j && stu[j].score >= pivot.score) {
j--;
}
while (i < j && stu[i].score <= pivot.score) {
i++;
}
if (i < j) {
Student temp = stu[i];
stu[i] = stu[j];
stu[j] = temp;
}
}
stu[left] = stu[i];
stu[i] = pivot;
quickSort(stu, left, i - 1);
quickSort(stu, i + 1, right);
}
// 简单选择排序
void selectionSort(Student stu[], int n) {
for (int i = 0; i < n - 1; i++) {
int minIndex = i;
for (int j = i + 1; j < n; j++) {
if (stu[j].score < stu[minIndex].score) {
minIndex = j;
}
}
if (minIndex != i) {
Student temp = stu[i];
stu[i] = stu[minIndex];
stu[minIndex] = temp;
}
}
}
// 输出学生信息
void outputStudentInfo(Student stu[], int n) {
for (int i = 0; i < n; i++) {
cout << "学号:" << stu[i].id << ",姓名:" << stu[i].name << ",分数:" << stu[i].score << endl;
}
}
// 菜单操作界面
void menu(Student stu[], int n) {
int choice;
while (true) {
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:
inputStudentInfo(stu, n);
break;
case 2:
insertionSort(stu, n);
break;
case 3:
binaryInsertionSort(stu, n);
break;
case 4:
bubbleSort(stu, n);
break;
case 5:
quickSort(stu, 0, n - 1);
break;
case 6:
selectionSort(stu, n);
break;
case 7:
outputStudentInfo(stu, n);
break;
case 8:
return;
default:
cout << "输入无效,请重新输入!" << endl;
}
}
}
int main() {
int n;
cout << "请输入学生人数:";
cin >> n;
Student stu[n];
menu(stu, n);
return 0;
}
```
注意:该代码只是一个参考实现,可能还需要进一步优化等操作。
阅读全文