给出含有n个学生的成绩表,每条记录由学号、姓名和分数组成,编写具体函数完成下列操作:设计一个菜单操作界面,实现录入学生基本信息,直接插入排序,折半插入排序,冒泡排序,快速排序,简单选择排序,输出学生信息,退出等基本操作
时间: 2024-01-07 18:05:48 浏览: 81
好的,我会为你解答这个问题。
首先,我们需要定义一个学生的结构体,包括学号、姓名和分数。
```c++
struct Student {
int number; // 学号
string name; // 姓名
int score; // 分数
};
```
接下来,我们可以定义一个动态数组来存储学生信息。
```c++
vector<Student> students;
```
接下来,我们可以通过一个循环菜单来实现各种操作。具体实现如下:
```c++
#include <iostream>
#include <vector>
#include <string>
using namespace std;
struct Student {
int number;
string name;
int score;
};
// 直接插入排序
void insertSort(vector<Student>& students) {
for (int i = 1; i < students.size(); i++) {
Student temp = students[i];
int j;
for (j = i - 1; j >= 0 && students[j].number > temp.number; j--) {
students[j + 1] = students[j];
}
students[j + 1] = temp;
}
}
// 折半插入排序
void binaryInsertSort(vector<Student>& students) {
for (int i = 1; i < students.size(); i++) {
Student temp = students[i];
int low = 0, high = i - 1;
while (low <= high) {
int mid = (low + high) / 2;
if (students[mid].number > temp.number) {
high = mid - 1;
} else {
low = mid + 1;
}
}
for (int j = i - 1; j >= high + 1; j--) {
students[j + 1] = students[j];
}
students[high + 1] = temp;
}
}
// 冒泡排序
void bubbleSort(vector<Student>& students) {
for (int i = 0; i < students.size() - 1; i++) {
for (int j = 0; j < students.size() - i - 1; j++) {
if (students[j].number > students[j + 1].number) {
swap(students[j], students[j + 1]);
}
}
}
}
// 快速排序
int partition(vector<Student>& students, int low, int high) {
int pivot = students[low].number;
while (low < high) {
while (low < high && students[high].number >= pivot) {
high--;
}
students[low] = students[high];
while (low < high && students[low].number <= pivot) {
low++;
}
students[high] = students[low];
}
students[low].number = pivot;
return low;
}
void quickSort(vector<Student>& students, int low, int high) {
if (low < high) {
int pivotPos = partition(students, low, high);
quickSort(students, low, pivotPos - 1);
quickSort(students, pivotPos + 1, high);
}
}
// 简单选择排序
void selectSort(vector<Student>& students) {
for (int i = 0; i < students.size() - 1; i++) {
int k = i;
for (int j = i + 1; j < students.size(); j++) {
if (students[j].number < students[k].number) {
k = j;
}
}
if (k != i) {
swap(students[k], students[i]);
}
}
}
// 输出学生信息
void printStudents(vector<Student>& students) {
cout << "学号\t姓名\t分数" << endl;
for (int i = 0; i < students.size(); i++) {
cout << students[i].number << "\t" << students[i].name << "\t" << students[i].score << endl;
}
}
int main() {
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;
int choice;
cin >> choice;
if (choice == 1) {
Student s;
cout << "请输入学号:";
cin >> s.number;
cout << "请输入姓名:";
cin >> s.name;
cout << "请输入分数:";
cin >> s.score;
students.push_back(s);
cout << "录入成功!" << endl;
} else if (choice == 2) {
insertSort(students);
cout << "排序成功!" << endl;
} else if (choice == 3) {
binaryInsertSort(students);
cout << "排序成功!" << endl;
} else if (choice == 4) {
bubbleSort(students);
cout << "排序成功!" << endl;
} else if (choice == 5) {
quickSort(students, 0, students.size() - 1);
cout << "排序成功!" << endl;
} else if (choice == 6) {
selectSort(students);
cout << "排序成功!" << endl;
} else if (choice == 7) {
printStudents(students);
} else if (choice == 8) {
break;
} else {
cout << "输入有误,请重新输入!" << endl;
}
}
return 0;
}
```
这样,我们就完成了一个基本的学生信息管理系统。
阅读全文