c++实验环境中,给出含有n个学生的成绩表,每条记录由学号、姓名和分数组成,编写具体函数完成下列操作:设计一个菜单操作界面,实现录入学生基本信息,直接插入排序,折半插入排序,冒泡排序,快速排序,简单选择排序,输出学生信息,退出等基本操作
时间: 2024-03-10 15:46:02 浏览: 83
C++实现一个基本的学生成绩记录系统
好的,我可以帮你解答这个问题。
首先,我们需要定义一个结构体来存储每个学生的信息,包括学号、姓名和分数。代码如下:
```c++
struct Student {
int id; // 学号
string name; // 姓名
int score; // 分数
};
```
接下来,我们需要实现一个菜单操作界面,让用户可以选择不同的操作。代码如下:
```c++
void menu() {
cout << "=======================" << endl;
cout << "1. 录入学生基本信息" << endl;
cout << "2. 直接插入排序" << endl;
cout << "3. 折半插入排序" << endl;
cout << "4. 冒泡排序" << endl;
cout << "5. 快速排序" << endl;
cout << "6. 简单选择排序" << endl;
cout << "7. 输出学生信息" << endl;
cout << "0. 退出" << endl;
cout << "=======================" << endl;
}
```
接下来,我们需要实现每个操作的具体函数。首先是录入学生基本信息。
```c++
void input(vector<Student>& students) {
int n;
cout << "请输入学生人数:";
cin >> n;
for (int i = 0; i < n; i++) {
Student s;
cout << "请输入第 " << i + 1 << " 个学生的学号、姓名和分数(用空格隔开):";
cin >> s.id >> s.name >> s.score;
students.push_back(s);
}
}
```
接下来是直接插入排序。
```c++
void insertionSort(vector<Student>& students) {
int n = students.size();
for (int i = 1; i < n; i++) {
Student temp = students[i];
int j = i - 1;
while (j >= 0 && students[j].id > temp.id) {
students[j + 1] = students[j];
j--;
}
students[j + 1] = temp;
}
}
```
然后是折半插入排序。
```c++
void binaryInsertionSort(vector<Student>& students) {
int n = students.size();
for (int i = 1; i < n; i++) {
Student temp = students[i];
int left = 0, right = i - 1;
while (left <= right) {
int mid = (left + right) / 2;
if (students[mid].id > temp.id) {
right = mid - 1;
} else {
left = mid + 1;
}
}
for (int j = i - 1; j >= left; j--) {
students[j + 1] = students[j];
}
students[left] = temp;
}
}
```
接下来是冒泡排序。
```c++
void bubbleSort(vector<Student>& students) {
int n = students.size();
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (students[j].id > students[j + 1].id) {
swap(students[j], students[j + 1]);
}
}
}
}
```
然后是快速排序。
```c++
void quickSort(vector<Student>& students, int left, int right) {
if (left >= right) {
return;
}
int i = left, j = right;
Student pivot = students[left];
while (i < j) {
while (i < j && students[j].id >= pivot.id) {
j--;
}
students[i] = students[j];
while (i < j && students[i].id <= pivot.id) {
i++;
}
students[j] = students[i];
}
students[i] = pivot;
quickSort(students, left, i - 1);
quickSort(students, i + 1, right);
}
```
最后是简单选择排序。
```c++
void selectionSort(vector<Student>& students) {
int n = students.size();
for (int i = 0; i < n - 1; i++) {
int minIndex = i;
for (int j = i + 1; j < n; j++) {
if (students[j].id < students[minIndex].id) {
minIndex = j;
}
}
if (minIndex != i) {
swap(students[i], students[minIndex]);
}
}
}
```
最后是输出学生信息。
```c++
void output(vector<Student>& students) {
for (int i = 0; i < students.size(); i++) {
cout << students[i].id << " " << students[i].name << " " << students[i].score << endl;
}
}
```
最后是主函数。
```c++
int main() {
vector<Student> students;
int choice;
do {
menu();
cout << "请输入您的选择:";
cin >> choice;
switch (choice) {
case 1:
input(students);
break;
case 2:
insertionSort(students);
cout << "直接插入排序完成!" << endl;
break;
case 3:
binaryInsertionSort(students);
cout << "折半插入排序完成!" << endl;
break;
case 4:
bubbleSort(students);
cout << "冒泡排序完成!" << endl;
break;
case 5:
quickSort(students, 0, students.size() - 1);
cout << "快速排序完成!" << endl;
break;
case 6:
selectionSort(students);
cout << "简单选择排序完成!" << endl;
break;
case 7:
output(students);
break;
case 0:
cout << "程序已退出!" << endl;
break;
default:
cout << "输入有误,请重新输入!" << endl;
break;
}
} while (choice != 0);
return 0;
}
```
以上就是完整的代码实现,可以在c++实验环境中编译运行。
阅读全文