struct student { char name[20]; int id; int room; }; // 冒泡排序函数 void bubble_sort(struct student stu[], int n, int key) { int i, j; struct student temp; switch (key) { case 1: // 按姓名排序 for (i = 0; i < n-1; i++) { for (j = 0; j < n-i-1; j++) { if (strcmp(stu[j].name, stu[j+1].name) > 0) { temp = stu[j]; stu[j] = stu[j+1]; stu[j+1] = temp; } } } break; case 2: // 按学号排序 for (i = 0; i < n-1; i++) { for (j = 0; j < n-i-1; j++) { if (stu[j].id > stu[j+1].id) { temp = stu[j]; stu[j] = stu[j+1]; stu[j+1] = temp; } } } break; case 3: // 按房号排序 for (i = 0; i < n-1; i++) { for (j = 0; j < n-i-1; j++) { if (stu[j].room > stu[j+1].room) { temp = stu[j]; stu[j] = stu[j+1]; stu[j+1] = temp; } } } break; } } // 二分查找函数 int binary_search(struct student stu[], int n, int key, int value) { int low = 0, high = n-1, mid; while (low <= high) { mid = (low + high) / 2; switch (key) { case 1: // 按姓名查找 if (strcmp(stu[mid].name, value) == 0) { return mid; } else if (strcmp(stu[mid].name, value) > 0) { high = mid - 1; } else { low = mid + 1; } break; case 2: // 按学号查找 if (stu[mid].id == value) { return mid; } else if (stu[mid].id > value) { high = mid - 1; } else { low = mid + 1; } break; case 3: // 按房号查找 if (stu[mid].room == value) { return mid; } else if (stu[mid].room > value) { high = mid - 1; } else { low = mid + 1; } break; } } return -1; // 没有找到返回-1 } int main() { int n, i, key, value, index; struct student stu[10000]; printf("请输入学生人数:"); scanf("%d", &n); for (i = 0; i < n; i++) { printf("请输入第%d个学生的姓名、学号、房号:", i+1); scanf("%s %d %d", stu[i].name, &stu[i].id, &stu[i].room); } printf("请选择排序关键字:\n1.姓名\n2.学号\n3.房号\n"); scanf("%d", &key); bubble_sort(stu, n, key); printf("排序后的结果:\n"); for (i = 0; i < n; i++) { printf("%s %d %d\n", stu[i].name, stu[i].id, stu[i].room); } while (1) { printf("请选择查询关键字:\n1.姓名\n2.学号\n3.房号\n"); scanf("%d", &key); printf("请输入要查询的值:"); scanf("%d", &value); index = binary_search(stu, n, key, value); if (index != -1) { printf("查询结果:\n%s %d %d\n", stu[index].name, stu[index].id, stu[index].room); } else { printf("没有找到该学生!\n"); } printf("是否继续查询?(1.是 2.否)"); scanf("%d", &i); if (i == 2) break; } return 0; }按姓名查找出错
时间: 2024-03-05 10:49:58 浏览: 120
在按姓名查找时,程序使用了 `scanf` 函数来读取要查找的姓名,但是在 `binary_search` 函数中使用了 `strcmp` 函数来比较字符串。由于 `scanf` 函数读取字符串时会包含换行符 `\n`,而 `strcmp` 函数会将换行符也视为字符串的一部分,因此会导致查找失败。
解决方法是在读取字符串之后使用 `getchar` 函数来读取并丢弃换行符。修改代码如下:
```
case 1: // 按姓名查找
printf("请输入要查询的姓名:");
scanf("%s", value);
getchar(); // 读取并丢弃换行符
index = binary_search(stu, n, key, value);
break;
```
相关问题
另外定义一个struct book stu1[]来储存排序后的数据,并不改变原数据,基于此重新写这个代码
好的,以下是根据您的要求重新编写的代码:
```c
#include <stdio.h>
#include <string.h>
struct book {
char name[50];
float price;
};
int main() {
int i, j, n;
struct book temp;
printf("Enter the number of books you want to sort: ");
scanf("%d", &n);
struct book books[n], sorted_books[n];
printf("Enter book names and prices:\n");
for (i = 0; i < n; i++) {
printf("Book %d:\n", i+1);
scanf("%s %f", books[i].name, &books[i].price);
}
// copy original array to sorted array
memcpy(sorted_books, books, sizeof(struct book) * n);
// bubble sort
for (i = 0; i < n-1; i++) {
for (j = 0; j < n-i-1; j++) {
if (sorted_books[j].price > sorted_books[j+1].price) {
temp = sorted_books[j];
sorted_books[j] = sorted_books[j+1];
sorted_books[j+1] = temp;
}
}
}
printf("\nOriginal array:\n");
for (i = 0; i < n; i++) {
printf("%s %.2f\n", books[i].name, books[i].price);
}
printf("\nSorted array:\n");
for (i = 0; i < n; i++) {
printf("%s %.2f\n", sorted_books[i].name, sorted_books[i].price);
}
return 0;
}
```
在这个代码中,我们定义了一个新的 `struct book sorted_books[]` 数组来储存排序后的数据,使用了 `memcpy` 函数将原始数据复制到新数组中,然后在新数组上进行排序,最后输出原数组和排序后的数组。
要求输入某班10个学生的信息(包括学号、姓名、性别、成绩),要求按成绩由高到低输出该班学生信息。 分析:问题的分解: 1)定义学生信息的结构体类型; 2)定义结构体类型数组变量; 3)编写学生信息输入函数; 4)编写学生信息输出函数; 5)编写按照学生成绩排序函数;(要求排序算法为选择排序和冒泡排序两种) 编写主函数对子函数进行函数调用,实现功能。
好的,我可以帮你解答这个问题。
首先,我们需要定义一个学生信息的结构体类型,包括学号、姓名、性别和成绩。代码如下:
```c
struct Student {
int id; // 学号
char name[20]; // 姓名
char gender; // 性别
float score; // 成绩
};
```
接下来,我们需要定义一个结构体类型数组变量,用于存储学生信息。代码如下:
```c
struct Student stu[10];
```
然后,我们需要编写一个学生信息输入函数,用于输入学生信息。代码如下:
```c
void input(struct Student *stu) {
int i;
for (i = 0; i < 10; i++) {
printf("请输入第%d个学生的信息:\n", i + 1);
printf("学号:");
scanf("%d", &stu[i].id);
printf("姓名:");
scanf("%s", stu[i].name);
printf("性别:");
scanf(" %c", &stu[i].gender);
printf("成绩:");
scanf("%f", &stu[i].score);
}
}
```
接着,我们需要编写一个学生信息输出函数,用于输出学生信息。代码如下:
```c
void output(struct Student *stu) {
int i;
printf("按成绩由高到低输出该班学生信息:\n");
printf("学号\t姓名\t性别\t成绩\n");
for (i = 0; i < 10; i++) {
printf("%d\t%s\t%c\t%.1f\n", stu[i].id, stu[i].name, stu[i].gender, stu[i].score);
}
}
```
然后,我们需要编写一个按照学生成绩排序的函数,要求排序算法为选择排序和冒泡排序两种。代码如下:
```c
void selection_sort(struct Student *stu) {
int i, j, max;
struct Student temp;
for (i = 0; i < 9; i++) {
max = i;
for (j = i + 1; j < 10; j++) {
if (stu[j].score > stu[max].score) {
max = j;
}
}
if (max != i) {
temp = stu[i];
stu[i] = stu[max];
stu[max] = temp;
}
}
}
void bubble_sort(struct Student *stu) {
int i, j;
struct Student temp;
for (i = 0; i < 9; i++) {
for (j = 0; j < 9 - i; j++) {
if (stu[j].score < stu[j + 1].score) {
temp = stu[j];
stu[j] = stu[j + 1];
stu[j + 1] = temp;
}
}
}
}
```
最后,我们需要在主函数中对子函数进行函数调用,实现功能。代码如下:
```c
int main() {
input(stu);
selection_sort(stu); // 使用选择排序算法进行排序
output(stu);
bubble_sort(stu); // 使用冒泡排序算法进行排序
output(stu);
return 0;
}
```
这样,就实现了按成绩由高到低输出该班学生信息的功能。
阅读全文
相关推荐
















