C语言从键盘输入10个学生的姓名和成绩,输出姓名和排完序的成绩
时间: 2023-12-17 07:04:13 浏览: 91
以下是C语言实现从键盘输入10个学生的姓名和成绩,输出姓名和排完序的成绩的代码:
```c
#include <stdio.h>
#include <string.h>
struct student {
int number;
char name[20];
int score[3];
int average;
};
void sort(struct student *p, int n) {
int i, j;
struct student temp;
for (i = 0; i < n - 1; i++) {
for (j = i + 1; j < n; j++) {
if (p[i].average < p[j].average) {
temp = p[i];
p[i] = p[j];
p[j] = temp;
}
}
}
}
int main() {
struct student person[10];
int i, j;
for (i = 0; i < 10; i++) {
printf("请输入第%d个学生的学号、姓名、三门课成绩:\n", i + 1);
scanf("%d,%s,%d,%d,%d", &person[i].number, person[i].name, &person[i].score[0], &person[i].score[1], &person[i].score[2]);
person[i].average = (person[i].score[0] + person[i].score[1] + person[i].score[2]) / 3;
}
sort(person, 10);
printf("姓名\t成绩1\t成绩2\t成绩3\t平均成绩\n");
for (i = 0; i < 10; i++) {
printf("%s\t", person[i].name);
for (j = 0; j < 3; j++) {
printf("%d\t", person[i].score[j]);
}
printf("%d\n", person[i].average);
}
return 0;
}
```
阅读全文