利用c语言完成 给出n个学生的考试成绩表,每条记录由学号、姓名和分数和名次组成,设计算法完成下列操作: (1)设计一个显示对学生信息操作的菜单函数如下所示: 1、录入学生基本信息 2、直接插入排序 3、冒泡排序 4、快速排序 5、输出学生信息 6、退出 算法设计要求:按分数从高到低的顺序进行排序,分数相同的为同一名次。输入的学生信息存入文件中,每选择一种排序方法,必须从文件中取出数据。
时间: 2024-02-21 19:01:16 浏览: 70
好的,这是一个关于学生成绩信息管理的问题,需要用到文件读写和排序算法。以下是我的解答:
首先,我们需要定义一个学生信息的结构体,包括学号、姓名、分数和名次四个属性。然后,我们可以编写一个函数,用于录入学生信息,将学生信息保存到一个文件中。
```c
typedef struct Student {
int id;
char name[20];
int score;
int rank;
} Student;
void inputStudent() {
FILE *fp = fopen("students.dat", "wb");
if (fp == NULL) {
printf("Failed to open file.");
return;
}
int n;
printf("Please input the number of students: ");
scanf("%d", &n);
Student s;
for (int i = 0; i < n; i++) {
printf("Please input the information of student %d:\n", i + 1);
printf("ID: ");
scanf("%d", &s.id);
printf("Name: ");
scanf("%s", s.name);
printf("Score: ");
scanf("%d", &s.score);
s.rank = 0;
fwrite(&s, sizeof(Student), 1, fp);
}
fclose(fp);
}
```
接下来,我们需要编写三种排序算法:直接插入排序、冒泡排序和快速排序。这里我们以分数为关键字进行排序,分数相同的按照学号从小到大排序。
```c
void directInsertionSort(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].score == temp.score && students[j].id > temp.id))) {
students[j + 1] = students[j];
j--;
}
students[j + 1] = temp;
}
}
void bubbleSort(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 || (students[j].score == students[j + 1].score && students[j].id > students[j + 1].id)) {
Student temp = students[j];
students[j] = students[j + 1];
students[j + 1] = temp;
}
}
}
}
void quickSort(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 || (students[j].score == pivot.score && students[j].id >= pivot.id)))
j--;
students[i] = students[j];
while (i < j && (students[i].score > pivot.score || (students[i].score == pivot.score && students[i].id <= pivot.id)))
i++;
students[j] = students[i];
}
students[i] = pivot;
quickSort(students, left, i - 1);
quickSort(students, i + 1, right);
}
```
最后,我们需要编写一个函数,用于读取文件中的学生信息,进行排序,并输出。
```c
void outputStudent() {
FILE *fp = fopen("students.dat", "rb");
if (fp == NULL) {
printf("Failed to open file.");
return;
}
fseek(fp, 0, SEEK_END);
int n = ftell(fp) / sizeof(Student);
rewind(fp);
Student *students = (Student *)malloc(n * sizeof(Student));
fread(students, sizeof(Student), n, fp);
fclose(fp);
int i;
printf("Please choose a sort method:\n");
printf("1. Direct insertion sort\n");
printf("2. Bubble sort\n");
printf("3. Quick sort\n");
scanf("%d", &i);
switch (i) {
case 1:
directInsertionSort(students, n);
break;
case 2:
bubbleSort(students, n);
break;
case 3:
quickSort(students, 0, n - 1);
break;
default:
printf("Invalid option.\n");
break;
}
int rank = 1;
students[0].rank = 1;
for (int i = 1; i < n; i++) {
if (students[i].score == students[i - 1].score) {
students[i].rank = rank;
} else {
students[i].rank = ++rank;
}
}
printf("ID\tName\tScore\tRank\n");
for (int i = 0; i < n; i++) {
printf("%d\t%s\t%d\t%d\n", students[i].id, students[i].name, students[i].score, students[i].rank);
}
free(students);
}
```
最后,我们可以编写一个主函数,用于显示菜单,根据用户选择调用相应的函数。
```c
int main() {
int i;
while (1) {
printf("\nPlease choose an operation:\n");
printf("1. Input student information\n");
printf("2. Direct insertion sort\n");
printf("3. Bubble sort\n");
printf("4. Quick sort\n");
printf("5. Output student information\n");
printf("6. Quit\n");
scanf("%d", &i);
switch (i) {
case 1:
inputStudent();
break;
case 2:
case 3:
case 4:
outputStudent();
break;
case 5:
return 0;
default:
printf("Invalid option.\n");
break;
}
}
return 0;
}
```
以上就是完整的代码实现。需要注意的是,为了方便起见,这里将学生信息保存在一个二进制文件中,如果需要保存到文本文件中,可以使用`fprintf`和`fscanf`函数进行读写。
阅读全文