本帖最后由 小甲鱼的铁粉 于 2020-11-5 20:05 编辑
1.int i;
for (int i = 0; i < 4; i++)
int i, zuigao;
for (int i = 0; i <= 4; i++)
pingjunfen[i] = (a[i].fen1 + a[i].fen2) / 2;
for (int zuigao = 0; i <= 4; i++)
已经定义过i,zuigao了,循环里面就不要再使用int i , int zuigao了
2. for (int zuigao = 0; i <= 4; i++)
if (pingjunfen[zuigao] > pingjunfen[i])
{
zuigao = i;
}
这里面的if判断反了,应该是 for (zuigao = 0, i = 1; i < 4; i++)
if (pingjunfen[zuigao] < pingjunfen[i])
{
zuigao = i;
}
3.输入的是四个学生的信息,先把0给zuigao,然后i从1开始到 i<4 这里面比较,如果大就赋值给zuigao for (zuigao = 0, i = 1; i < 4; i++)
if (pingjunfen[zuigao] < pingjunfen[i])
{
zuigao = i;
}
4.整个子函数的逻辑问题,我们只要找到成绩最好的人的下标就好了,所以返回就返回成绩最好的人的下标就好了int max(student a[])
{
float pingjunfen[5];
int i, zuigao;
for (i = 0; i <= 4; i++)
pingjunfen[i] = (a[i].fen1 + a[i].fen2) / 2;
for (zuigao = 0, i = 1; i < 4; i++)
if (pingjunfen[zuigao] < pingjunfen[i])
{
zuigao = i;
}
return zuigao;
}
整个修改后的代码如下#include<stdio.h>
#include<string.h>
#include <stdlib.h>
typedef struct student
{
int xuehao;
char name[30];
float fen1;
float fen2;
};
int main()
{
int max(struct student a[]);
student sheng[5];
student xueba; //定义结构变量
int i;
for (i = 0; i < 4; i++)
{
printf("请输入第%d名学生的学号、姓名和2门成绩\n", i + 1);
scanf("%d%s%f%f", &sheng[i].xuehao, sheng[i].name, &sheng[i].fen1, &sheng[i].fen2);
}
i = max(sheng);
printf("平均分最高的学生的信息如下:\n");
printf("学号:%d\n姓名:%s\n2门成绩:%f,%f\n", sheng[i].xuehao, sheng[i].name, sheng[i].fen1, sheng[i].fen2);
system("pause");
return 0;
}
int max(student a[])
{
float pingjunfen[5];
int i, zuigao;
for (i = 0; i < 4; i++)
pingjunfen[i] = (a[i].fen1 + a[i].fen2) / 2;
for (zuigao = 0, i = 1; i < 4; i++)
if (pingjunfen[zuigao] < pingjunfen[i])
{
zuigao = i;
}
return zuigao;
}
|