题面
请根据程序中的要求完善程序——注意,**除指定位置外,不能对程序中已有部分作任何修改或重新编写一个程序,否则作0分处理**。
程序的功能是:有五个学生,每个学生的数据包括学号、姓名(最长19字节)、三门课的成绩,从键盘输入五个学生的数据,并计算每个学生的平均成绩,最后显示最高平均分的学生的信息(包括学号,姓名,三门课的成绩,平均分数)。
程序的运行效果应类似地如图1所示,图1中的:
2001 wanghai 78 83.5 90
2003 zhangzhong 82 80 98.5
2005 liumei 95.2 81 90
2006 sunqian 70 77.2 90
2009 jiansansa 63 87 90.3
是从键盘输入的内容。
Please input students info:Num Name score1 score2 score3
1:2001 wanghai 78 83.5 90
2:2003 zhangzhong 82 80 98.5
3:2005 liumei 95.2 81 90
4:2006 sunqian 70 77.2 90
5:2009 jiansansa 63 87 90.3
The Highest is liumei(2005)
score1=95.20 score2=81.00 score3=90.00 aver=88.73
原码
#include <stdio.h>
struct student
{
int num;
char name[20];
float score1, score2, score3;
float aver;
};
void Input(struct student *pStu, int n);
int Highest(struct student *pStu, int n);
int main(void)
{
int high;
struct student myClass[5];
printf("\nThe Highest is %s(%d)\nscore1=%.2f score2=%.2f score3=%.2f aver=%.2f\n",
myClass[high].name, myClass[high].num,
myClass[high].score1, myClass[high].score2, myClass[high].score3, myClass[high].aver);
return 0;
}
void Input(struct student *pStu, int n)
{
int i;
struct student tmpStu;
printf("Please input students info:Num Name score1 score2 score3\n");
for (i=0; i<n; i++)
{
printf("%d:", i+1);
scanf("%d%s%f%f%f", &tmpStu.num, tmpStu.name,
&tmpStu.score1, &tmpStu.score2, &tmpStu.score3);
pStu[i] = tmpStu;
pStu[i].aver = (pStu[i].score1 + pStu[i].score2 + pStu[i].score3) / 3.0f;
}
}
int Highest(struct student *pStu, int n)
{
int i, pos = 0;
return pos;
}
改后
#include <stdio.h>
struct student
{
int num;
char name[20];
float score1, score2, score3;
float aver;
};
void Input(struct student *pStu, int n);
int Highest(struct student *pStu, int n);
int main(void)
{
int high;
struct student myClass[5];
Input(myClass, 5);
high = Highest(myClass, 5);
printf("\nThe Highest is %s(%d)\nscore1=%.2f score2=%.2f score3=%.2f aver=%.2f\n",
myClass[high].name, myClass[high].num,
myClass[high].score1, myClass[high].score2, myClass[high].score3, myClass[high].aver);
return 0;
}
void Input(struct student *pStu, int n)
{
int i;
struct student tmpStu;
printf("Please input students info:Num Name score1 score2 score3\n");
for (i=0; i<n; i++)
{
printf("%d:", i+1);
scanf("%d%s%f%f%f", &tmpStu.num, tmpStu.name,
&tmpStu.score1, &tmpStu.score2, &tmpStu.score3);
pStu[i] = tmpStu;
pStu[i].aver = (pStu[i].score1 + pStu[i].score2 + pStu[i].score3) / 3.0f;
}
}
int Highest(struct student *pStu, int n)
{
int i, pos = 0;
struct student highAver=pStu[0];
pos = 0;
for(i=1;i<n;i++)
{
if(highAver.aver<pStu[i].aver)
{
highAver = pStu[i];
pos = i;
}
}
return pos;
}
知识点
- 结构体数组作为函数参数
- 输入数据时注意格式
- 打擂算法,记录位置