void P(Student students[],int m,int n) { int i; for(i=0;i<n;i++) { int k; char keyword[21]; scanf("%d%s",&k,keyword); int found=0; int j; for(j=0;j<m;j++) { if(k==1&&strcmp(students[j].id,keyword)==0) { printf("%d\n",j+1); found=1; break; } else if(k==2&&strcmp(students[j].phone,keyword)==0) { printf("%d\n",j+1); found=1; break; } else if(k==3&&strcmp(students[j].email,keyword)==0) { printf("%d\n",j+1); found=1; break; } } if(!found) { printf("Not Found\n"); } } }
时间: 2023-12-10 19:05:11 浏览: 33
这段代码是一个函数P,它有三个参数:一个Student类型的数组students,一个整型m表示students数组中元素的个数,一个整型n表示要查找的关键字的数量。函数的作用是根据关键字在students数组中查找对应的元素,并输出其在数组中的下标。
具体来说,函数会循环n次,每次输入一个整型k和一个字符串keyword,表示要根据哪个属性进行查找,以及要查找的关键字。然后函数会遍历students数组,查找是否存在符合条件的元素。如果找到了,则输出该元素在数组中的下标(注意下标从1开始)。如果没有找到,则输出"Not Found"。
其中,关键字的属性(id、phone、email)分别用1、2、3来表示。函数中使用了strcmp函数来比较字符串。
相关问题
// 获取学生总分 void get_total_score(Student *student) { int total = 0; for (int i = 0; i < MAX_COURSE_NUM; i++) { student->total_score = total; } } // 显示学生信息 int show_student_info( Student *student) { printf("学号:%d\t姓名:%s\t成绩:%d %d %d\t总分:%d\n",student->id, student->name,student->scores[0], student->scores[1], student->scores[2],student->total_score); } // 显示所有学生信息及总排名 void show_all_students(Student *student) { // 计算总分 for (int i = 0; i < student_count; i++) { students[i].total_score = get_total_score(&students[i]); } // 排序 for (int i = 0; i < student_count - 1; i++) { for (int j = i + 1; j < student_count; j++) { if (students[i].total_score < students[j].total_score) { Student temp = students[i]; students[i] = students[j]; students[j] = temp; } } } // 显示信息 printf("\n所有学生信息及总排名:\n"); for (int i = 0; i < student_count; i++) { show_student_info(&students[i]); } }哪错了
在函数 get_total_score 中,你每次循环都将总分赋值为0,然后又用循环内的最后一个分数覆盖了总分,所以每个学生的总分都是最后一个分数。应该在循环结束后再将总分赋值给学生。
修改后的代码如下:
```
void get_total_score(Student *student) {
int total = 0;
for (int i = 0; i < MAX_COURSE_NUM; i++) {
total += student->scores[i];
}
student->total_score = total;
}
int show_student_info(Student *student) {
printf("学号:%d\t姓名:%s\t成绩:%d %d %d\t总分:%d\n", student->id, student->name,
student->scores[0], student->scores[1], student->scores[2], student->total_score);
}
void show_all_students(Student *students) {
// 计算总分
for (int i = 0; i < student_count; i++) {
get_total_score(&students[i]);
}
// 排序
for (int i = 0; i < student_count - 1; i++) {
for (int j = i + 1; j < student_count; j++) {
if (students[i].total_score < students[j].total_score) {
Student temp = students[i];
students[i] = students[j];
students[j] = temp;
}
}
}
// 显示信息
printf("\n所有学生信息及总排名:\n");
for (int i = 0; i < student_count; i++) {
show_student_info(&students[i]);
}
}
```
void SortStudent(){ for(int i=0;i<num-1;i++){ for(int j=0;j<num-1-i;j++){ if(students[j].All < students[j+1].All){ struct Student temp = students[j]; students[j] = students[j+1]; students[j+1] = temp; } } } for(int i=0;i<num;i++){ printf("第%d名\t ID:%s\t 姓名:%s\t 总成绩:%5.1f\n",i+1, students[i].ID,students[i].Name,students[i].All); } }
好的,这是一段 C 语言代码,下面是注释:
```
void SortStudent(){ // 声明一个无返回值的函数 SortStudent
// 使用冒泡排序算法进行排序
for(int i=0;i<num-1;i++){ // 外层循环,控制排序轮数
for(int j=0;j<num-1-i;j++){ // 内层循环,控制每轮比较次数
if(students[j].All < students[j+1].All){ // 如果前一个学生的总成绩比后一个学生低
struct Student temp = students[j]; // 则交换这两个学生的信息
students[j] = students[j+1];
students[j+1] = temp;
}
}
}
// 输出排序后的学生信息
for(int i=0;i<num;i++){
printf("第%d名\t ID:%s\t 姓名:%s\t 总成绩:%5.1f\n",i+1,
students[i].ID,students[i].Name,students[i].All);
}
}
```
这段代码定义了一个函数 `SortStudent`,它接受一个名为 `students` 的结构体数组,其中存储了学生的 ID、姓名和总成绩信息。函数使用冒泡排序算法对学生数组进行排序,并输出排序后的学生信息。在执行排序操作时,代码使用了两层循环,外层循环控制排序轮数,内层循环控制每轮比较次数。如果前一个学生的总成绩比后一个学生低,则交换这两个学生的信息。最后,函数使用 `printf` 函数输出排序后的学生信息。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)