用c++写考试成绩排序
时间: 2023-03-24 16:02:03 浏览: 113
好的,我来给您讲解一下如何用 C 语言编写成绩排名的程序。
首先,您需要定义一个结构体来存储每个学生的成绩和姓名信息,例如:
```
struct student {
char name[20];
int score;
};
```
然后,您可以创建一个数组来存储所有学生的信息,例如:
```
struct student stu[100];
```
接下来,您需要编写一个函数来读入学生的姓名和成绩信息,并将其存储到结构体数组中,例如:
```
void input(struct student stu[], int n) {
int i;
for (i = 0; i < n; i++) {
printf("请输入第%d个学生的姓名和成绩:", i + 1);
scanf("%s%d", stu[i].name, &stu[i].score);
}
}
```
然后,您可以编写一个函数来按照成绩从高到低对学生进行排序,例如:
```
void sort(struct student stu[], int n) {
int i, j;
struct student temp;
for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - i - 1; j++) {
if (stu[j].score < stu[j + 1].score) {
temp = stu[j];
stu[j] = stu[j + 1];
stu[j + 1] = temp;
}
}
}
}
```
最后,您可以编写一个函数来输出按照成绩排名后的学生信息,例如:
```
void output(struct student stu[], int n) {
int i;
printf("按照成绩排名后的学生信息如下:\n");
for (i = 0; i < n; i++) {
printf("%s %d\n", stu[i].name, stu[i].score);
}
}
```
在主函数中,您可以调用上述三个函数来实现成绩排名的功能,例如:
```
int main() {
int n;
printf("请输入学生的数量:");
scanf("%d", &n);
input(stu, n);
sort(stu, n);
output(stu, n);
return 0;
}
```
以上就是用 C 语言编写成绩排名的程序的一些基本步骤。希望能对您有所帮助!