c++编写程序完成给出含有n个学生的成绩表,每条记录由学号、姓名和 分数组成,编写具体函数完成下列操作:设计一个菜单操作界面,实现录入学生基本信息,直接 插入排序,折半插入排序,冒泡排序,快速排序,简单选择排序,输出学生信息,退出等基本操
时间: 2024-02-05 18:03:23 浏览: 173
以下是一个参考实现,包括菜单操作界面和各种排序算法:
```c++
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
struct Student {
string id;
string name;
int grade;
};
vector<Student> students;
void printMenu() {
cout << "1. 输入学生信息" << endl;
cout << "2. 直接插入排序" << endl;
cout << "3. 折半插入排序" << endl;
cout << "4. 冒泡排序" << endl;
cout << "5. 快速排序" << endl;
cout << "6. 简单选择排序" << endl;
cout << "7. 输出学生信息" << endl;
cout << "8. 退出" << endl;
}
void inputStudent() {
int n;
cout << "请输入学生数量:";
cin >> n;
for (int i = 0; i < n; i++) {
Student student;
cout << "请输入第" << i + 1 << "个学生的学号、姓名和分数:";
cin >> student.id >> student.name >> student.grade;
students.push_back(student);
}
cout << "学生信息录入完成!" << endl;
}
void printStudents() {
for (const auto& student : students) {
cout << student.id << " " << student.name << " " << student.grade << endl;
}
}
void directInsertionSort() {
for (int i = 1; i < students.size(); i++) {
Student temp = students[i];
int j = i - 1;
while (j >= 0 && students[j].grade > temp.grade) {
students[j + 1] = students[j];
j--;
}
students[j + 1] = temp;
}
}
void binaryInsertionSort() {
for (int i = 1; i < students.size(); i++) {
Student temp = students[i];
int left = 0, right = i - 1;
while (left <= right) {
int mid = (left + right) / 2;
if (students[mid].grade > temp.grade) {
right = mid - 1;
} else {
left = mid + 1;
}
}
for (int j = i - 1; j >= left; j--) {
students[j + 1] = students[j];
}
students[left] = temp;
}
}
void bubbleSort() {
for (int i = 0; i < students.size() - 1; i++) {
for (int j = 0; j < students.size() - 1 - i; j++) {
if (students[j].grade > students[j + 1].grade) {
swap(students[j], students[j + 1]);
}
}
}
}
void quickSort(int left, int right) {
if (left >= right) {
return;
}
int i = left, j = right;
int pivot = students[left].grade;
while (i < j) {
while (i < j && students[j].grade >= pivot) {
j--;
}
students[i] = students[j];
while (i < j && students[i].grade <= pivot) {
i++;
}
students[j] = students[i];
}
students[i].grade = pivot;
quickSort(left, i - 1);
quickSort(i + 1, right);
}
void selectionSort() {
for (int i = 0; i < students.size() - 1; i++) {
int minIndex = i;
for (int j = i + 1; j < students.size(); j++) {
if (students[j].grade < students[minIndex].grade) {
minIndex = j;
}
}
swap(students[i], students[minIndex]);
}
}
int main() {
while (true) {
printMenu();
int choice;
cout << "请输入你的选择:";
cin >> choice;
switch (choice) {
case 1:
inputStudent();
break;
case 2:
directInsertionSort();
cout << "直接插入排序完成!" << endl;
break;
case 3:
binaryInsertionSort();
cout << "折半插入排序完成!" << endl;
break;
case 4:
bubbleSort();
cout << "冒泡排序完成!" << endl;
break;
case 5:
quickSort(0, students.size() - 1);
cout << "快速排序完成!" << endl;
break;
case 6:
selectionSort();
cout << "简单选择排序完成!" << endl;
break;
case 7:
printStudents();
break;
case 8:
cout << "谢谢使用!" << endl;
return 0;
default:
cout << "无效的选择,请重新输入!" << endl;
break;
}
}
}
```
在该实现中,我们定义了一个 `Student` 结构体来表示每个学生的信息,使用了一个 `vector` 来存储所有学生的信息。在菜单操作界面中,我们提供了七个选项,分别对应输入学生信息、各种排序算法、输出学生信息和退出程序。在选择某个排序算法时,程序会对 `students` 中的学生信息进行排序,并输出排序完成的提示信息。最后,在输出学生信息时,程序会遍历 `students` 并打印每个学生的信息。
注意,在实际应用中,我们需要考虑更多的情况,例如输入异常数据时如何处理、算法效率的问题等等。此处仅提供一个基本的参考实现。
阅读全文