用c++写一个满足下列条件的算法给出n个学生的考试成绩表,每条记录由学号、姓名和分数和名次组成,设计算法完成下列操作: (1)设计一个显示对学生信息操作的菜单函数如下所示: **** * * 1、录入学生基本信息 2、直接插入排序 3、冒泡排序 4、快速排序 5、简单选择排序 6、堆排序 7、输出学生信息 0、退出 (2)算法设计要求:按分数从高到低的顺序进行排序,分数相同的为同一名次。
时间: 2024-03-09 21:46:46 浏览: 108
学生成绩管理 ,这是一个学生成绩管理的实例,有C++实现。
以下是一个基于C++的算法,可以实现对学生信息的录入、排序和输出操作:
```c++
#include <iostream>
#include <string>
using namespace std;
struct Student {
string id;
string name;
int score;
int rank;
};
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 direct_insert_sort(Student* students, int n) {
for (int i = 1; i < n; i++) {
Student temp = students[i];
int j = i - 1;
while (j >= 0 && students[j].score < temp.score) {
students[j + 1] = students[j];
j--;
}
students[j + 1] = temp;
}
for (int i = 0; i < n; i++) {
if (i == 0 || students[i].score != students[i - 1].score) {
students[i].rank = i + 1;
} else {
students[i].rank = students[i - 1].rank;
}
}
}
void bubble_sort(Student* students, int n) {
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (students[j].score < students[j + 1].score) {
swap(students[j], students[j + 1]);
}
}
}
for (int i = 0; i < n; i++) {
if (i == 0 || students[i].score != students[i - 1].score) {
students[i].rank = i + 1;
} else {
students[i].rank = students[i - 1].rank;
}
}
}
void quick_sort(Student* students, int left, int right) {
if (left >= right) return;
int i = left, j = right;
Student pivot = students[left];
while (i < j) {
while (i < j && students[j].score <= pivot.score) {
j--;
}
students[i] = students[j];
while (i < j && students[i].score >= pivot.score) {
i++;
}
students[j] = students[i];
}
students[i] = pivot;
quick_sort(students, left, i - 1);
quick_sort(students, i + 1, right);
for (int k = 0; k < right - left + 1; k++) {
if (k == 0 || students[left + k].score != students[left + k - 1].score) {
students[left + k].rank = left + k + 1;
} else {
students[left + k].rank = students[left + k - 1].rank;
}
}
}
void simple_select_sort(Student* students, int n) {
for (int i = 0; i < n - 1; i++) {
int min_index = i;
for (int j = i + 1; j < n; j++) {
if (students[j].score > students[min_index].score) {
min_index = j;
}
}
if (min_index != i) {
swap(students[i], students[min_index]);
}
}
for (int i = 0; i < n; i++) {
if (i == 0 || students[i].score != students[i - 1].score) {
students[i].rank = i + 1;
} else {
students[i].rank = students[i - 1].rank;
}
}
}
void adjust_heap(Student* students, int i, int n) {
int child = 2 * i + 1;
while (child < n) {
if (child + 1 < n && students[child + 1].score > students[child].score) {
child++;
}
if (students[i].score < students[child].score) {
swap(students[i], students[child]);
i = child;
child = 2 * i + 1;
} else {
break;
}
}
}
void heap_sort(Student* students, int n) {
for (int i = n / 2 - 1; i >= 0; i--) {
adjust_heap(students, i, n);
}
for (int i = n - 1; i > 0; i--) {
swap(students[0], students[i]);
adjust_heap(students, 0, i);
}
for (int i = 0; i < n; i++) {
if (i == 0 || students[i].score != students[i - 1].score) {
students[i].rank = i + 1;
} else {
students[i].rank = students[i - 1].rank;
}
}
}
void output(const Student* students, int n) {
cout << "学号\t姓名\t分数\t名次" << endl;
for (int i = 0; i < n; i++) {
cout << students[i].id << "\t" << students[i].name << "\t" << students[i].score << "\t" << students[i].rank << endl;
}
}
int main() {
int n = 0;
cout << "请输入学生人数:";
cin >> n;
Student* students = new Student[n];
int choice = -1;
while (choice != 0) {
cout << "请选择操作:" << endl;
cout << "1、录入学生基本信息" << endl;
cout << "2、直接插入排序" << endl;
cout << "3、冒泡排序" << endl;
cout << "4、快速排序" << endl;
cout << "5、简单选择排序" << endl;
cout << "6、堆排序" << endl;
cout << "7、输出学生信息" << endl;
cout << "0、退出" << endl;
cin >> choice;
switch (choice) {
case 1:
input(students, n);
break;
case 2:
direct_insert_sort(students, n);
cout << "排序完成!" << endl;
break;
case 3:
bubble_sort(students, n);
cout << "排序完成!" << endl;
break;
case 4:
quick_sort(students, 0, n - 1);
cout << "排序完成!" << endl;
break;
case 5:
simple_select_sort(students, n);
cout << "排序完成!" << endl;
break;
case 6:
heap_sort(students, n);
cout << "排序完成!" << endl;
break;
case 7:
output(students, n);
break;
case 0:
cout << "程序已退出!" << endl;
break;
default:
cout << "输入有误,请重新输入!" << endl;
break;
}
}
delete[] students;
return 0;
}
```
以上算法实现了对学生信息的录入、六种排序算法和学生信息的输出。其中,直接插入排序、冒泡排序、快速排序、简单选择排序和堆排序均实现了按照分数从高到低排序,并将分数相同的学生排在同一名次。在排序完成后,算法会输出每个学生的学号、姓名、分数和名次。
阅读全文