修改这段代码中的错误并注释:#include<stdio.h> #include<conio.h> #include<string.h> struct { char name[10], degree[10]; }ST; void sort(ST x[], int n, char key[][10], int m) { int i, j, k, con1, con2; ST v; for (i = 0; i < n - 1; i++) for (j = 0; j < n - i - 1; j++) { for (k = 0; k < m; k++) if (strcmp(x[j].degree, key[k]) == 0) { con1 = k; break; } for (k = 0; k < m; k++) if (strcmp(x[j + 1].degree, key[k]) == 0) { con2 = k; break; } if (con1 > con2 || con1 == con2 && strcmp(x[j].name, x[j + 1].name) < 0) { v = x[j]; x[j] = x[j + 1]; x[j + 1] = v; } } } int main(void) { ST s[5] = { {"Andy","master"},{"Beth","bachelor"},{"Can","doctor"}, {"Deo","bachelor"},{"Emy","master"} }; char sortkey[3] = { "bachelor","master","doctor" }; int i; sort(s, 5, sortkey, 3); for (i = 0; i < 5; i++) printf("\n%s %s", s[i]->name, s[i]->degree); return 0; }
时间: 2023-06-24 15:05:13 浏览: 48
修改后的代码如下:
```c
#include<stdio.h>
#include<string.h>
struct student {
char name[10], degree[10];
};
void sort(struct student x[], int n, char key[][10], int m) {
int i, j, k, con1, con2;
struct student v;
for (i = 0; i < n - 1; i++)
for (j = 0; j < n - i - 1; j++) {
for (k = 0; k < m; k++)
if (strcmp(x[j].degree, key[k]) == 0) {
con1 = k;
break;
}
for (k = 0; k < m; k++)
if (strcmp(x[j + 1].degree, key[k]) == 0) {
con2 = k;
break;
}
if (con1 > con2 || con1 == con2 && strcmp(x[j].name, x[j + 1].name) > 0) {
v = x[j];
x[j] = x[j + 1];
x[j + 1] = v;
}
}
}
int main(void) {
struct student s[5] = {
{"Andy","master"},
{"Beth","bachelor"},
{"Can","doctor"},
{"Deo","bachelor"},
{"Emy","master"}
};
char sortkey[3][10] = {"bachelor", "master", "doctor"};
int i;
sort(s, 5, sortkey, 3);
for (i = 0; i < 5; i++)
printf("\n%s %s", s[i].name, s[i].degree);
return 0;
}
```
注释:
1. 结构体定义时应该指定类型名称,所以将原代码中的“ST”改为“student”。
2. 在 main 函数中,sortkey 应该是一个二维字符数组,表示多个字符串,因此将原代码中的“char sortkey[3]”改为“char sortkey[3][10]”。
3. 在 sort 函数中,如果 con1 > con2 或者 con1 等于 con2 且 x[j].name 大于 x[j+1].name,则交换 x[j] 和 x[j+1] 的位置。所以将原代码中的“<”改为“>”。
4. 在输出结果时,应该使用点号(.)而不是箭头(->)来访问结构体成员,因为 s[i] 是结构体变量而不是指针。
相关推荐
![text/x-c](https://img-home.csdnimg.cn/images/20210720083646.png)
![text/x-c](https://img-home.csdnimg.cn/images/20210720083646.png)
![image/gif](https://img-home.csdnimg.cn/images/20210720083352.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)