struct Student stu[]:C 語言中參數只需寫 Student stu[] 或 Student *stu(需 typedef)。max 回傳 struct Student 但實參為指標。for (i = 0; i if (stu[i].aver > stu[m].aver) 缺少條件與大括號。& 取址符。
#include
#define N 3
typedef struct {
int num;
char name[20];
float score[3];
float aver;
} Student;
void input(Student stu[]);
Student max(Student stu[]);
void print(Student s);
int main() {
Student stu[N];
input(stu);
print(max(stu));
return 0;
}
void input(Student stu[]) {
for (int i = 0; i < N; i++) {
scanf("%d %19s %f %f %f", &stu[i].num, stu[i].name,
&stu[i].score[0], &stu[i].score[1], &stu[i].score[2]);
stu[i].aver = (stu[i].score[0] + stu[i].score[1] + stu[i].score[2]) / 3.0f;
}
}
Student max(Student stu[]) {
int m = 0;
for (int i = 1; i < N; i++)
if (stu[i].aver > stu[m].aver) m = i;
return stu[m];
}
void print(Student s) {
printf("\n成績最高的學生是:\n學號:%d\n姓名:%s\n成績:%.1f,%.1f,%.1f\n平均:%.2f\n",
s.num, s.name, s.score[0], s.score[1], s.score[2], s.aver);
}
建議初學者先掌握基本語法、函數傳參、結構體用法,再嘗試完整程式。