使用C++写出该程序实验环境中,给出含有n个学生的成绩表,每条记录由学号、姓名和分数组成,编写具体函数完成下列操作:设计一个菜单操作界面,实现录入学生基本信息,直接插入排序,折半插入排序,冒泡排序,快使排序,简单选择排序,输出学生信息,退出等基本操作,完成实验报告。
时间: 2023-10-28 22:04:30 浏览: 45
以下是一个C++实现的示例程序,完成对学生信息进行排序和输出的功能。具体实现中,使用了一个结构体来存储学生信息,使用了多种排序算法,以及一个菜单操作界面,方便用户选择不同的操作。
```
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
// 定义学生结构体
struct Student {
string id;
string name;
int score;
};
// 声明函数
void input(Student stu[], int n);
void directInsertSort(Student stu[], int n);
void binaryInsertSort(Student stu[], int n);
void bubbleSort(Student stu[], int n);
void quickSort(Student stu[], int left, int right);
void selectSort(Student stu[], int n);
void output(Student stu[], int n);
void menu();
// 实现函数
void input(Student stu[], int n) {
cout << "请按照学号、姓名、分数的顺序输入每个学生的信息:" << endl;
for (int i = 0; i < n; i++) {
cin >> stu[i].id >> stu[i].name >> stu[i].score;
}
}
void directInsertSort(Student stu[], int n) {
for (int i = 1; i < n; i++) {
Student temp = stu[i];
int j = i - 1;
while (j >= 0 && stu[j].id > temp.id) {
stu[j+1] = stu[j];
j--;
}
stu[j+1] = temp;
}
}
void binaryInsertSort(Student stu[], int n) {
for (int i = 1; i < n; i++) {
Student temp = stu[i];
int left = 0, right = i - 1;
while (left <= right) {
int mid = (left + right) / 2;
if (stu[mid].id > temp.id) {
right = mid - 1;
} else {
left = mid + 1;
}
}
for (int j = i - 1; j >= left; j--) {
stu[j+1] = stu[j];
}
stu[left] = 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].id > stu[j+1].id) {
swap(stu[j], stu[j+1]);
}
}
}
}
void quickSort(Student stu[], int left, int right) {
if (left >= right) {
return;
}
int i = left, j = right;
Student pivot = stu[left];
while (i < j) {
while (i < j && stu[j].id >= pivot.id) {
j--;
}
stu[i] = stu[j];
while (i < j && stu[i].id <= pivot.id) {
i++;
}
stu[j] = stu[i];
}
stu[i] = pivot;
quickSort(stu, left, i-1);
quickSort(stu, i+1, right);
}
void selectSort(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].id < stu[minIndex].id) {
minIndex = j;
}
}
if (minIndex != i) {
swap(stu[i], stu[minIndex]);
}
}
}
void output(Student stu[], int n) {
cout << left << setw(10) << "学号" << setw(10) << "姓名" << setw(10) << "分数" << endl;
for (int i = 0; i < n; i++) {
cout << left << setw(10) << stu[i].id << setw(10) << stu[i].name << setw(10) << stu[i].score << endl;
}
}
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 << "8. 退出" << endl;
}
// 主函数
int main() {
int n, choice;
cout << "请输入学生人数: ";
cin >> n;
Student stu[n];
do {
menu();
cin >> choice;
switch (choice) {
case 1:
input(stu, n);
break;
case 2:
directInsertSort(stu, n);
cout << "直接插入排序完成" << endl;
break;
case 3:
binaryInsertSort(stu, n);
cout << "折半插入排序完成" << endl;
break;
case 4:
bubbleSort(stu, n);
cout << "冒泡排序完成" << endl;
break;
case 5:
quickSort(stu, 0, n-1);
cout << "快速排序完成" << endl;
break;
case 6:
selectSort(stu, n);
cout << "简单选择排序完成" << endl;
break;
case 7:
output(stu, n);
break;
case 8:
cout << "程序已退出" << endl;
break;
default:
cout << "输入错误,请重新输入" << endl;
}
} while (choice != 8);
return 0;
}
```