要查询到同一个房间的所有人 printf("要查找的房间\n"); int room; scanf("%d",&room); getchar(); int indexb; indexb=SearchByroom(stu, n, room); if(indexb !=-1) { printf("查询结果:\n%s %s %d\n", stu[indexb].name, stu[indexb].id, stu[indexb].room); } else { printf("没有找到该学生\n"); }break; case 0: b=0; printf("已经退出查询\n"); break; default: printf("输入的类型无效,请重新输入。\n"); break; } }int SearchByroom(struct student stu[],int n,int value) { int left = 0, right = n - 1; while (left <= right) { int mid = (left + right) / 2; if (stu[mid].room == value) { return mid; } else if (stu[mid].room > value) { right = mid-1; } else { left = mid+1; } } return -1; }的代码
时间: 2024-03-21 14:40:26 浏览: 15
这段代码可以在一个学生信息的数组中,通过输入房间号来查询该房间内的所有学生信息。具体实现是:
1. 用户输入要查询的房间号。
2. 调用SearchByroom函数,返回该房间的第一个学生在数组中的索引值。
3. 使用while循环,不断地输出该房间内的学生信息,直到遇到一个房间号不等于输入的房间号,或者已经输出了所有该房间内的学生信息。
需要注意的是,在输出学生信息时,可以将SearchByroom函数返回的第一个学生索引值作为起始索引,然后不断往后遍历数组,直到输出了所有该房间内的学生信息为止。这里可以使用for循环或者while循环实现,具体代码可以参考以下示例:
```
printf("要查找的房间\n");
int room;
scanf("%d",&room);
getchar();
int indexb;
indexb=SearchByroom(stu, n, room); // 调用SearchByroom函数查找该房间的第一个学生
if(indexb !=-1)
{
printf("查询结果:\n");
// 循环输出该房间内的所有学生信息
for(int i=indexb; i<n && stu[i].room==room; i++)
{
printf("%s %s %d\n", stu[i].name, stu[i].id, stu[i].room);
}
}
else
{
printf("没有找到该学生\n");
}
```
这样就可以实现查询同一房间所有人的功能了。
相关推荐
![text/x-c](https://img-home.csdnimg.cn/images/20210720083646.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)