题目:在一个结构体数组中存有4名学生的信息(排名、姓名、成绩),按学生姓名查询其排名和平均成绩。查询可连续进行,直到输入0时结束。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define STUNUM 10
struct stu {
char* name;
int rank;
int score;
};
int main()
{
struct stu stuArr[4];
stuArr[0].rank = 1;
stuArr[0].score = 100;
stuArr[0].name = "a";
stuArr[1].rank = 2;
stuArr[1].score = 90;
stuArr[1].name = "b";
stuArr[2].rank = 3;
stuArr[2].score = 80;
stuArr[2].name = "c";
stuArr[3].rank = 4;
stuArr[3].score = 70;
stuArr[3].name = "d";
char c[10];
printf("请输入学生姓名\n");
gets(c);
while (strcmp(c,"0") != 0) {
for (int i = 0; i < 4; i++)
{
if (strcmp(stuArr[i].name, c) == 0) {
printf("学生排名:%d,平均成绩为%d\n", stuArr[i].rank, stuArr[i].score);
}
}
printf("请输入学生姓名\n");
gets(c);
}
}