实验环境中,给出含有n个学生的成绩表,每条记录由学号,姓名和分数组成,编写具体函数完成下列操作设计一个菜单操作界面,实习录入学生基本信息,直接插入排序,折半插入排序,冒泡排序,快速排序,简单选择排序,输入学生信息,退出等基本操作的C语言完整代码
时间: 2024-02-05 09:03:13 浏览: 21
好的,以下是基于你的要求编写的C语言代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STUDENTS 100
// 保存学生信息的结构体
typedef struct {
char id[10];
char name[20];
int score;
} Student;
int menu(); // 显示菜单
void input(Student students[], int *n); // 录入学生信息
void direct_insert_sort(Student students[], int n); // 直接插入排序
void binary_insert_sort(Student students[], int n); // 折半插入排序
void bubble_sort(Student students[], int n); // 冒泡排序
void quick_sort(Student students[], int left, int right); // 快速排序
void select_sort(Student students[], int n); // 简单选择排序
void print_students(Student students[], int n); // 打印学生信息
int main() {
Student students[MAX_STUDENTS];
int n = 0; // 学生数量
int choice;
while (1) {
choice = menu();
switch (choice) {
case 1:
input(students, &n);
break;
case 2:
direct_insert_sort(students, n);
print_students(students, n);
break;
case 3:
binary_insert_sort(students, n);
print_students(students, n);
break;
case 4:
bubble_sort(students, n);
print_students(students, n);
break;
case 5:
quick_sort(students, 0, n-1);
print_students(students, n);
break;
case 6:
select_sort(students, n);
print_students(students, n);
break;
case 7:
exit(0);
default:
printf("请输入正确的选项!\n");
break;
}
}
return 0;
}
int menu() {
int choice;
printf("******************\n");
printf("1. 输入学生信息\n");
printf("2. 直接插入排序\n");
printf("3. 折半插入排序\n");
printf("4. 冒泡排序\n");
printf("5. 快速排序\n");
printf("6. 简单选择排序\n");
printf("7. 退出\n");
printf("******************\n");
printf("请输入您的选择:");
scanf("%d", &choice);
return choice;
}
void input(Student students[], int *n) {
int i;
printf("请输入学生数量:");
scanf("%d", n);
if (*n > MAX_STUDENTS) {
printf("学生数量不能超过%d!\n", MAX_STUDENTS);
return;
}
printf("请依次输入每个学生的学号、姓名和分数:\n");
for (i = 0; i < *n; i++) {
scanf("%s %s %d", students[i].id, students[i].name, &students[i].score);
}
}
void direct_insert_sort(Student students[], int n) {
int i, j;
Student temp;
for (i = 1; i < n; i++) {
temp = students[i];
j = i - 1;
while (j >= 0 && students[j].score > temp.score) {
students[j+1] = students[j];
j--;
}
students[j+1] = temp;
}
}
void binary_insert_sort(Student students[], int n) {
int i, j, low, high, mid;
Student temp;
for (i = 1; i < n; i++) {
temp = students[i];
low = 0;
high = i - 1;
while (low <= high) {
mid = (low + high) / 2;
if (students[mid].score > temp.score) {
high = mid - 1;
} else {
low = mid + 1;
}
}
for (j = i - 1; j >= high + 1; j--) {
students[j+1] = students[j];
}
students[high+1] = temp;
}
}
void bubble_sort(Student students[], int n) {
int i, j;
Student temp;
for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - 1 - i; j++) {
if (students[j].score > students[j+1].score) {
temp = students[j];
students[j] = students[j+1];
students[j+1] = temp;
}
}
}
}
void quick_sort(Student students[], int left, int right) {
int i, j, pivot;
Student temp;
if (left < right) {
i = left;
j = right;
pivot = students[i].score;
while (i < j) {
while (i < j && students[j].score >= pivot) {
j--;
}
if (i < j) {
temp = students[i];
students[i] = students[j];
students[j] = temp;
}
while (i < j && students[i].score <= pivot) {
i++;
}
if (i < j) {
temp = students[i];
students[i] = students[j];
students[j] = temp;
}
}
quick_sort(students, left, i-1);
quick_sort(students, i+1, right);
}
}
void select_sort(Student students[], int n) {
int i, j, min_index;
Student temp;
for (i = 0; i < n - 1; i++) {
min_index = i;
for (j = i + 1; j < n; j++) {
if (students[j].score < students[min_index].score) {
min_index = j;
}
}
if (min_index != i) {
temp = students[i];
students[i] = students[min_index];
students[min_index] = temp;
}
}
}
void print_students(Student students[], int n) {
int i;
printf("学号\t姓名\t分数\n");
for (i = 0; i < n; i++) {
printf("%s\t%s\t%d\n", students[i].id, students[i].name, students[i].score);
}
}
```
以上代码实现了你所需要的各个操作,其中包括了菜单界面、学生信息的录入、直接插入排序、折半插入排序、冒泡排序、快速排序、简单选择排序以及学生信息的打印等功能。希望能够满足你的需求。
阅读全文