#include<stdio.h> #define MAXN 100 struct student{ char name[20]; int score; }; void cnt_score( struct student *p, int n ); int main() { int i, n; struct student stu[MAXN]; scanf("%d", &n); for(i = 0; i < n; i++){ scanf("%s%d", stu[i].name, &stu[i].score); } cnt_score(stu, n); return 0; } void cnt_score(struct student *p, int n) { int cnt_a = 0, cnt_p = 0, cnt_f = 0; while ( p <= q ){ if ( ) cnt_a++; else if ( ) cnt_p++; else cnt_f++; p++; } printf("%d %d %d\n", cnt_a, cnt_p, cnt_f); }
时间: 2023-11-27 09:50:34 浏览: 61
这段代码有一些问题,需要完善才能正常运行。下面是一些需要注意的地方:
1. 函数 `cnt_score` 中的指针参数 `p` 和 `n` 分别表示学生结构体数组的首地址和长度,但是代码中没有定义指针 `q`,应该将 `p + n` 赋值给一个新的指针变量,用于指向数组的最后一个元素。
2. 需要在函数 `cnt_score` 中加入判断条件,判断学生成绩的等级,并统计每个等级的人数。可以使用 if-else 语句或者 switch-case 语句实现。
3. 在函数 `cnt_score` 中,统计完人数后需要输出结果。可以使用 printf 函数输出,注意格式控制符的使用。
下面是完善后的代码:
```c
#include <stdio.h>
#define MAXN 100
struct student {
char name[20];
int score;
};
void cnt_score(struct student *p, int n);
int main() {
int i, n;
struct student stu[MAXN];
scanf("%d", &n);
for (i = 0; i < n; i++) {
scanf("%s%d", stu[i].name, &stu[i].score);
}
cnt_score(stu, n);
return 0;
}
void cnt_score(struct student *p, int n) {
int cnt_a = 0, cnt_p = 0, cnt_f = 0;
struct student *q = p + n - 1;
while (p <= q) {
if (p->score >= 90) {
cnt_a++;
} else if (p->score >= 60) {
cnt_p++;
} else {
cnt_f++;
}
p++;
}
printf("%d %d %d\n", cnt_a, cnt_p, cnt_f);
}
```