问题如下,后面附上的是我写的代码
你写的结构体内的成员不能用指针,要用字符数组存放学号和姓名。
#include <stdio.h>
#include <string.h>
typedef struct
{
char num[11];
char name[11];
int score1;
int score2;
int score3;
double avg;
} stu;
void selSort(stu *arr, int n)
{
int i, j, small;
stu tmp;
for (i = 0; i < n - 1; i++)
{
small = i;
for (j = i + 1; j < n; j++)
{
if (arr[j].avg < arr[small].avg)
small = j;
}
if (small != i)
{
tmp = arr[i];
arr[i] = arr[small];
arr[small] = tmp;
}
}
}
int main(int argc, char *argv[])
{
int n;
puts("输入学生个数:");
scanf("%d", &n);
stu st[n + 1];
puts("输入各个学生的信息:");
for (int i = 0; i < n; i++)
{
scanf("%10s%10s%d%d%d", st[i].num, st[i].name, &st[i].score1, &st[i].score2, &st[i].score3);
st[i].avg = (st[i].score1 + st[i].score2 + st[i].score3) / 3.0;
}
selSort(st, n);
puts("");
for (int i = n - 1; i >= 0; i--)
{
printf("%s %s %.1lf\n", st[i].num, st[i].name, st[i].avg);
}
return 0;
}