创建含有n个学生记录的文件,每条记录由学号、姓名和分数组成,编写具体函数完成下列操作:设计一个菜单操作界面,实现录入学生基本信息,直接插入排序,折半插入排序,冒泡排序,快速排序,简单选择排序,输出学生信息,退出等基本操作
时间: 2024-01-13 19:03:24 浏览: 79
好的,我可以为你提供一些帮助。首先,你需要定义一个学生结构体,包含学号、姓名和分数三个成员变量。然后,你需要设计一个菜单操作界面,让用户可以选择需要执行的操作。根据用户的选择,你可以调用相应的函数来完成对学生记录的操作。下面是一些函数的示例代码:
```c++
#include <iostream>
#include <fstream>
#include <cstring>
#include <algorithm>
using namespace std;
struct Student {
string id;
string name;
int score;
};
void input(Student* students, int n) {
for (int i = 0; i < n; i++) {
cout << "请输入第" << i + 1 << "个学生的信息:" << endl;
cout << "学号:";
cin >> students[i].id;
cout << "姓名:";
cin >> students[i].name;
cout << "分数:";
cin >> students[i].score;
}
}
void output(Student* students, int n) {
cout << "学生信息如下:" << endl;
for (int i = 0; i < n; i++) {
cout << "学号:" << students[i].id << ",姓名:" << students[i].name << ",分数:" << students[i].score << endl;
}
}
bool cmp(Student a, Student b) {
return a.id < b.id;
}
void directInsertSort(Student* students, int n) {
for (int i = 1; i < n; i++) {
int j = i - 1;
Student temp = students[i];
while (j >= 0 && cmp(temp, students[j])) {
students[j + 1] = students[j];
j--;
}
students[j + 1] = temp;
}
}
int binarySearch(Student* students, int left, int right, Student x) {
while (left <= right) {
int mid = (left + right) / 2;
if (cmp(x, students[mid])) {
right = mid - 1;
} else {
left = mid + 1;
}
}
return left;
}
void binaryInsertSort(Student* students, int n) {
for (int i = 1; i < n; i++) {
Student temp = students[i];
int pos = binarySearch(students, 0, i - 1, temp);
for (int j = i - 1; j >= pos; j--) {
students[j + 1] = students[j];
}
students[pos] = temp;
}
}
void bubbleSort(Student* students, int n) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (cmp(students[j + 1], students[j])) {
swap(students[j], students[j + 1]);
}
}
}
}
int partition(Student* students, int left, int right) {
Student pivot = students[left];
while (left < right) {
while (left < right && cmp(students[right], pivot)) {
right--;
}
students[left] = students[right];
while (left < right && cmp(pivot, students[left])) {
left++;
}
students[right] = students[left];
}
students[left] = pivot;
return left;
}
void quickSort(Student* students, int left, int right) {
if (left < right) {
int pivot = partition(students, left, right);
quickSort(students, left, pivot - 1);
quickSort(students, pivot + 1, right);
}
}
void selectSort(Student* students, int n) {
for (int i = 0; i < n; i++) {
int k = i;
for (int j = i + 1; j < n; j++) {
if (cmp(students[j], students[k])) {
k = j;
}
}
swap(students[i], students[k]);
}
}
int main() {
int n;
cout << "请输入学生人数:";
cin >> n;
Student* students = new Student[n];
input(students, n);
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;
int choice;
cin >> choice;
switch (choice) {
case 1:
directInsertSort(students, n);
break;
case 2:
binaryInsertSort(students, n);
break;
case 3:
bubbleSort(students, n);
break;
case 4:
quickSort(students, 0, n - 1);
break;
case 5:
selectSort(students, n);
break;
case 6:
output(students, n);
break;
case 7:
return 0;
default:
cout << "无效的选择,请重新输入。" << endl;
break;
}
}
return 0;
}
```
这段代码定义了一个 `Student` 结构体,包含学号、姓名和分数三个成员变量。然后,它提供了一个 `input` 函数,用于从用户输入中读取学生记录。接着,它提供了几个排序函数,包括直接插入排序、折半插入排序、冒泡排序、快速排序和简单选择排序。最后,它提供了一个菜单操作界面,让用户可以选择需要执行的操作。根据用户的选择,它会调用相应的函数来完成对学生记录的操作。
阅读全文