用C语言编写,并补充最后main函数的编写
时间: 2024-12-23 17:20:55 浏览: 11
好的,以下是基于您提供的实验要求,使用C语言编写的代码示例,并包括了 `main` 函数:
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MAX_STUDENTS 100
// 定义学生结构体
typedef struct {
int id;
float score;
} Student;
// 随机生成学生成绩表
void generateScores(Student students[], int n) {
srand(time(NULL));
for (int i = 0; i < n; i++) {
students[i].id = i + 1;
students[i].score = (float)(rand() % 61 + 40) / 10.0; // 成绩范围在40.0到100.0之间
}
}
// 冒泡排序
void bubbleSort(Student students[], int n) {
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - 1 - i; j++) {
if (students[j].score > students[j + 1].score) {
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;
float pivot = students[left].score;
while (i != j) {
while (students[j].score >= pivot && i < j) j--;
while (students[i].score <= pivot && i < j) i++;
if (i < j) {
Student temp = students[i];
students[i] = students[j];
students[j] = temp;
}
}
students[left] = students[i];
students[i].score = pivot;
quickSort(students, left, i - 1);
quickSort(students, i + 1, right);
}
// 顺序查找
int sequentialSearch(Student students[], int n, float target) {
for (int i = 0; i < n; i++) {
if (students[i].score == target) {
return i;
}
}
return -1;
}
// 折半查找(要求数组已排序)
int binarySearch(Student students[], int n, float target) {
int left = 0, right = n - 1;
while (left <= right) {
int mid = (left + right) / 2;
if (students[mid].score == target) {
return mid;
} else if (students[mid].score < target) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return -1;
}
// 主函数
int main() {
int n;
printf("请输入学生人数: ");
scanf("%d", &n);
if (n > MAX_STUDENTS || n <= 0) {
printf("学生人数超出范围或无效!\n");
return 1;
}
Student students[MAX_STUDENTS];
generateScores(students, n);
printf("生成的成绩表:\n");
for (int i = 0; i < n; i++) {
printf("ID: %d, Score: %.1f\n", students[i].id, students[i].score);
}
// 排序
printf("\n选择排序方式:\n1. 冒泡排序\n2. 快速排序\n");
int choice;
scanf("%d", &choice);
switch (choice) {
case 1:
bubbleSort(students, n);
break;
case 2:
quickSort(students, 0, n - 1);
break;
default:
printf("无效的选择!\n");
return 1;
}
printf("排序后的成绩表:\n");
for (int i = 0; i < n; i++) {
printf("ID: %d, Score: %.1f\n", students[i].id, students[i].score);
}
// 查找
printf("\n请输入要查找的成绩: ");
float target;
scanf("%f", &target);
int seqIndex = sequentialSearch(students, n, target);
if (seqIndex != -1) {
printf("顺序查找结果: 找到成绩 %.1f,位置为 %d\n", target, seqIndex);
} else {
printf("顺序查找结果: 没有找到成绩 %.1f\n", target);
}
int binIndex = binarySearch(students, n, target);
if (binIndex != -1) {
printf("折半查找结果: 找到成绩 %.1f,位置为 %d\n", target, binIndex);
} else {
printf("折半查找结果: 没有找到成绩 %.1f\n", target);
}
return 0;
}
```
### 说明
1. **生成成绩表**:使用 `generateScores` 函数随机生成学生的成绩。
2. **排序**:提供了冒泡排序和快速排序两种方法。
3. **查找**:实现了顺序查找和折半查找两种方法。
4. **主函数**:用户可以输入学生人数、选择排序方式、输入要查找的成绩,并显示相应的结果。
希望这段代码能满足您的需求。如果有任何问题或需要进一步的帮助,请随时告知。
阅读全文