#include <stdio.h>
#include <string.h>
#define M 1701 // 学号
#define N 10
struct student // 结构体
{
int num; // 学号
char name[N]; // 姓名
float score; // 成绩
} stu1[N];
void main()
{
int i;
printf("请依次输入学生的成绩,姓名:\n");
for (i = 0; i < N; i++)
{
stu1[i].num = M + i; // 学号赋值
printf("%d\t", stu1[i].num); // 打印学号
scanf("%f%s", &stu1[i].score, stu1[i].name); // 输入成绩,姓名
}
printf("下面按成绩大小排序,按任意键继续\n");
getchar(); // 任意键继续
for (i = 0; i < N - 1; i++) // 冒泡排序
{
int max = i;
for (int j = i + 1; j < N; j++)
{
if (stu1[j].score > stu1[max].score)
max = j;
struct student t; // 引入一个结构体
memcpy(&t, &stu1[max], sizeof(stu1));
memcpy(&stu1[max], &stu1[i], sizeof(stu1));
memcpy(&stu1[i], &t, sizeof(stu1));
}
printf("%d\t%s\t%f\n", stu1[i].num, stu1[i].name, stu1[i].score); // 打印排序
}
}
添加回答
举报
0/150
提交
取消