实验环境中,给出含有n个学生的成绩表,每条记录由学号,姓名和分数组成,编写具体函数完成下列操作设计一个菜单操作界面,实习录入学生基本信息,直接插入排序,折半插入排序,冒泡排序,快速排序,简单选择排序,输入学生信息,退出等基本操作的C语言完整代码
时间: 2024-02-05 19:03:18 浏览: 75
用c++编写的一个学生信息管理系统,能进行各种操作,采用菜单录入方式。
4星 · 用户满意度95%
好的,请稍等片刻,我为您编写代码。
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_SIZE 100
typedef struct {
char id[20];
char name[20];
int score;
} Student;
int n = 0;
Student students[MAX_SIZE];
void insert(Student student) {
if (n >= MAX_SIZE) {
printf("Error: Students array is full.\n");
return;
}
students[n++] = student;
}
void directInsertSort() {
int i, j;
Student temp;
for (i = 1; i < n; i++) {
temp = students[i];
for (j = i - 1; j >= 0 && students[j].score > temp.score; j--) {
students[j + 1] = students[j];
}
students[j + 1] = temp;
}
}
void binaryInsertSort() {
int i, j, low, high, middle;
Student temp;
for (i = 1; i < n; i++) {
temp = students[i];
low = 0;
high = i - 1;
while (low <= high) {
middle = (low + high) / 2;
if (students[middle].score > temp.score) {
high = middle - 1;
} else {
low = middle + 1;
}
}
for (j = i - 1; j >= high + 1; j--) {
students[j + 1] = students[j];
}
students[high + 1] = temp;
}
}
void bubbleSort() {
int i, j;
Student temp;
for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - i - 1; j++) {
if (students[j].score > students[j + 1].score) {
temp = students[j];
students[j] = students[j + 1];
students[j + 1] = temp;
}
}
}
}
void quickSort(int left, int right) {
int i, j;
Student pivot, temp;
if (left < right) {
i = left;
j = right;
pivot = students[left];
while (i < j) {
while (i < j && students[j].score >= pivot.score) {
j--;
}
if (i < j) {
students[i++] = students[j];
}
while (i < j && students[i].score < pivot.score) {
i++;
}
if (i < j) {
students[j--] = students[i];
}
}
students[i] = pivot;
quickSort(left, i - 1);
quickSort(i + 1, right);
}
}
void selectSort() {
int i, j, k;
Student temp;
for (i = 0; i < n - 1; i++) {
k = i;
for (j = i + 1; j < n; j++) {
if (students[j].score < students[k].score) {
k = j;
}
}
if (k != i) {
temp = students[i];
students[i] = students[k];
students[k] = temp;
}
}
}
void input() {
char id[20];
char name[20];
int score;
printf("Please input student information (id, name, score):\n");
scanf("%s %s %d", id, name, &score);
Student student;
strcpy(student.id, id);
strcpy(student.name, name);
student.score = score;
insert(student);
}
void print() {
int i;
printf("id\tname\tscore\n");
for (i = 0; i < n; i++) {
printf("%s\t%s\t%d\n", students[i].id, students[i].name, students[i].score);
}
}
int main() {
int choice;
while (1) {
printf("1. Input student information\n");
printf("2. Direct insert sort\n");
printf("3. Binary insert sort\n");
printf("4. Bubble sort\n");
printf("5. Quick sort\n");
printf("6. Select sort\n");
printf("7. Print students information\n");
printf("8. Exit\n");
printf("Please input your choice (1-8):\n");
scanf("%d", &choice);
switch (choice) {
case 1:
input();
break;
case 2:
directInsertSort();
break;
case 3:
binaryInsertSort();
break;
case 4:
bubbleSort();
break;
case 5:
quickSort(0, n - 1);
break;
case 6:
selectSort();
break;
case 7:
print();
break;
case 8:
exit(0);
default:
printf("Error: Invalid choice.\n");
break;
}
}
return 0;
}
```
这是一个基于菜单操作界面的成绩表操作程序,其中包括了录入学生信息、直接插入排序、折半插入排序、冒泡排序、快速排序、简单选择排序、打印学生信息和退出等操作。您可以根据需要进行操作,如需继续了解代码,请参考注释。
阅读全文