给出含有n个学生的成绩表 每条记录由学号 姓名 和分数组成 设计程序完成下列操作 设计一个菜单操作界面 该系统操作需要1.录入学生基本信息 2.直接插入排序 3.折半排序 4.冒泡排序 5.快速排序 6.简单选择排序 7.输出学生信息 8.退出
时间: 2024-02-25 20:59:27 浏览: 72
编程实现:(1) 键入某组学生(共 10 人)的学号、姓名、组成原理考试 成绩、数据结构考试成绩、汇编语言考试成绩
以下是一个C++程序,用于实现上述功能:
```c++
#include <iostream>
#include <string>
using namespace std;
struct Student {
string id; // 学号
string name; // 姓名
int score; // 分数
};
void insertSort(Student arr[], int n) {
int i, j;
Student key;
for (i = 1; i < n; i++) {
key = arr[i];
j = i - 1;
while (j >= 0 && arr[j].id > key.id) {
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}
void binarySort(Student arr[], int n) {
int i, j, low, high, mid;
Student key;
for (i = 1; i < n; i++) {
key = arr[i];
low = 0;
high = i - 1;
while (low <= high) {
mid = (low + high) / 2;
if (arr[mid].id > key.id) {
high = mid - 1;
}
else {
low = mid + 1;
}
}
for (j = i - 1; j >= low; j--) {
arr[j + 1] = arr[j];
}
arr[low] = key;
}
}
void bubbleSort(Student arr[], int n) {
int i, j;
Student temp;
for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - i - 1; j++) {
if (arr[j].id > arr[j + 1].id) {
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
int partition(Student arr[], int low, int high) {
Student pivot = arr[high];
int i = low - 1;
Student temp;
for (int j = low; j <= high - 1; j++) {
if (arr[j].id < pivot.id) {
i++;
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
temp = arr[i + 1];
arr[i + 1] = arr[high];
arr[high] = temp;
return (i + 1);
}
void quickSort(Student arr[], int low, int high) {
if (low < high) {
int pi = partition(arr, low, high);
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}
void selectionSort(Student arr[], int n) {
int i, j, min_idx;
Student temp;
for (i = 0; i < n - 1; i++) {
min_idx = i;
for (j = i + 1; j < n; j++) {
if (arr[j].id < arr[min_idx].id) {
min_idx = j;
}
}
temp = arr[min_idx];
arr[min_idx] = arr[i];
arr[i] = temp;
}
}
void printStudents(Student arr[], int n) {
cout << "学号\t姓名\t分数" << endl;
for (int i = 0; i < n; i++) {
cout << arr[i].id << "\t" << arr[i].name << "\t" << arr[i].score << endl;
}
}
int main() {
int n = 0;
Student students[100];
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;
cin >> choice;
switch (choice) {
case 1:
cout << "请输入学生数目:";
cin >> n;
for (int i = 0; i < n; i++) {
cout << "请输入学生" << i + 1 << "的学号、姓名和分数:";
cin >> students[i].id >> students[i].name >> students[i].score;
}
break;
case 2:
insertSort(students, n);
cout << "直接插入排序完成!" << endl;
break;
case 3:
binarySort(students, n);
cout << "折半排序完成!" << endl;
break;
case 4:
bubbleSort(students, n);
cout << "冒泡排序完成!" << endl;
break;
case 5:
quickSort(students, 0, n - 1);
cout << "快速排序完成!" << endl;
break;
case 6:
selectionSort(students, n);
cout << "简单选择排序完成!" << endl;
break;
case 7:
printStudents(students, n);
break;
case 8:
return 0;
default:
cout << "无效操作!" << endl;
break;
}
}
return 0;
}
```
该程序使用`struct`定义了一个名为`Student`的结构体,其中包含学号、姓名和分数三个属性。程序使用一个`while`循环来提供菜单操作界面,通过`switch`语句对用户的选择进行响应。对于每个排序算法,程序定义了一个相应的函数,用于对学生信息数组进行排序。`printStudents`函数用于输出学生信息。
阅读全文