C++给出含有n个学生的成绩表,每条记录由学号、姓名和分数组成,编写具体函数完成下列操作:设计一个菜单操作界面,实现录入学生基本信息,直接插入排序,折半插入排序,冒泡排序,快速排序,简单选择排序,输出学生信息,退出等基本操作并给出操作结果
时间: 2024-03-21 14:44:39 浏览: 48
好的,我为您写了一个示例程序,请您参考:
```c++
#include <iostream>
#include <string>
using namespace std;
#define MAX_STU_NUM 50 // 最大学生数
// 学生信息结构体
struct Student {
string id; // 学号
string name; // 姓名
int score; // 分数
};
// 学生信息表结构体
struct StudentList {
Student stuList[MAX_STU_NUM]; // 学生信息数组
int length = 0; // 数组长度
};
// 菜单函数
int 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;
cout << "------------------------------------" << endl;
cout << "请输入您的选择:";
int choice;
cin >> choice;
return choice;
}
// 录入学生基本信息
void inputStuInfo(StudentList &stuList) {
cout << "请输入学生数目:";
cin >> stuList.length;
for (int i = 0; i < stuList.length; i++) {
cout << "请输入第" << i + 1 << "个学生的学号:";
cin >> stuList.stuList[i].id;
cout << "请输入第" << i + 1 << "个学生的姓名:";
cin >> stuList.stuList[i].name;
cout << "请输入第" << i + 1 << "个学生的分数:";
cin >> stuList.stuList[i].score;
}
cout << "录入成功!" << endl;
}
// 直接插入排序
void straightInsertionSort(StudentList &stuList) {
for (int i = 1; i < stuList.length; i++) {
if (stuList.stuList[i].score < stuList.stuList[i - 1].score) {
Student tmp = stuList.stuList[i];
int j;
for (j = i - 1; j >= 0 && stuList.stuList[j].score > tmp.score; j--) {
stuList.stuList[j + 1] = stuList.stuList[j];
}
stuList.stuList[j + 1] = tmp;
}
}
cout << "直接插入排序完成!" << endl;
}
// 折半插入排序
void binaryInsertionSort(StudentList &stuList) {
for (int i = 1; i < stuList.length; i++) {
if (stuList.stuList[i].score < stuList.stuList[i - 1].score) {
Student tmp = stuList.stuList[i];
int low = 0, high = i - 1;
while (low <= high) {
int mid = (low + high) / 2;
if (stuList.stuList[mid].score > tmp.score) {
high = mid - 1;
} else {
low = mid + 1;
}
}
for (int j = i - 1; j >= high + 1; j--) {
stuList.stuList[j + 1] = stuList.stuList[j];
}
stuList.stuList[high + 1] = tmp;
}
}
cout << "折半插入排序完成!" << endl;
}
// 冒泡排序
void bubbleSort(StudentList &stuList) {
for (int i = 0; i < stuList.length - 1; i++) {
bool flag = true;
for (int j = 0; j < stuList.length - 1 - i; j++) {
if (stuList.stuList[j].score > stuList.stuList[j + 1].score) {
swap(stuList.stuList[j], stuList.stuList[j + 1]);
flag = false;
}
}
if (flag) {
break;
}
}
cout << "冒泡排序完成!" << endl;
}
// 快速排序
void quickSort(StudentList &stuList, int left, int right) {
if (left >= right) {
return;
}
int i = left, j = right;
Student pivot = stuList.stuList[left];
while (i < j) {
while (i < j && stuList.stuList[j].score >= pivot.score) {
j--;
}
stuList.stuList[i] = stuList.stuList[j];
while (i < j && stuList.stuList[i].score <= pivot.score) {
i++;
}
stuList.stuList[j] = stuList.stuList[i];
}
stuList.stuList[i] = pivot;
quickSort(stuList, left, i - 1);
quickSort(stuList, i + 1, right);
cout << "快速排序完成!" << endl;
}
// 简单选择排序
void simpleSelectionSort(StudentList &stuList) {
for (int i = 0; i < stuList.length - 1; i++) {
int minIdx = i;
for (int j = i + 1; j < stuList.length; j++) {
if (stuList.stuList[j].score < stuList.stuList[minIdx].score) {
minIdx = j;
}
}
if (minIdx != i) {
swap(stuList.stuList[i], stuList.stuList[minIdx]);
}
}
cout << "简单选择排序完成!" << endl;
}
// 输出学生信息
void outputStuInfo(StudentList &stuList) {
cout << "学号\t姓名\t分数" << endl;
for (int i = 0; i < stuList.length; i++) {
cout << stuList.stuList[i].id << "\t" << stuList.stuList[i].name << "\t" << stuList.stuList[i].score << endl;
}
}
int main() {
StudentList stuList;
while (true) {
int choice = menu();
switch (choice) {
case 1:
inputStuInfo(stuList);
break;
case 2:
straightInsertionSort(stuList);
break;
case 3:
binaryInsertionSort(stuList);
break;
case 4:
bubbleSort(stuList);
break;
case 5:
quickSort(stuList, 0, stuList.length - 1);
break;
case 6:
simpleSelectionSort(stuList);
break;
case 7:
outputStuInfo(stuList);
break;
case 8:
cout << "谢谢使用!" << endl;
return 0;
default:
cout << "输入错误,请重新输入!" << endl;
break;
}
}
return 0;
}
```
这是一个基本的菜单操作界面,可以实现录入学生基本信息,直接插入排序,折半插入排序,冒泡排序,快速排序,简单选择排序,输出学生信息,退出等基本操作。您可以根据自己的需要进行修改和完善。
阅读全文