|
发表于 2020-4-11 11:13:22
|
显示全部楼层
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct Student
{
char id[16]; //15位数以内
//依次为物化生成绩
double wuScore;
double huaScore;
double shengScore;
};
void maxScoreList(struct Student*,int count);
int main(void)
{
puts("您想录入几个学生?");
int count;
scanf("%d",&count);
struct Student* student = (struct Student*)malloc(sizeof(struct Student)*count); //开辟相应动态内存空间,这是为了后面的动态数组
puts("请输入如:1001 85 96 73,中间用空格隔开,依次代表id,物化生成绩\n\n\n\n");
for (int i = 0; i < count; i++)
{
scanf("%s%lf%lf%lf",student[i].id, &(student[i].wuScore), &(student[i].huaScore), &(student[i].shengScore));
student[i].id[strlen(student[i].id)] = '\0'; //最后一位补\0,这是字符串结尾标志,不然会在打印时数组越界出现乱码
}
maxScoreList(student, count);
//释放动态内存资源
free(student);
student = NULL;
return 0;
}
void maxScoreList(struct Student* student, int count)
{
//初始最大成绩, 为了能被下面替换,所以设成负数
double maxWuScore = -1, maxHuaScore = -1, maxShengScore = -1;
for (int j = 0; j < count; j++)
{
if (student[j].wuScore >maxWuScore)
{
maxWuScore = student[j].wuScore;
}
if (student[j].huaScore >maxHuaScore)
{
maxHuaScore = student[j].huaScore;
}
if (student[j].shengScore >maxShengScore)
{
maxShengScore = student[j].shengScore;
}
}
//可能多个学生并列成绩 %lg相比于%lf会舍弃末尾小数0, 如3.1和3.100的差别
printf("\n\n\n--------------------光荣榜--------------------------\n\n\n");
printf("物理最高分为%lg分,对应学生id为----->", maxWuScore);
//遍历学生列表,寻找分数为maxWuScore的学生
for (int i = 0; i < count; i++)[img][/img]
{
if (student[i].wuScore == maxWuScore)
{
printf("%s ", student[i].id);
}
}
printf("\n\n\n");
//同物理
printf("化学最高分为%lg分,对应学生id为----->", maxHuaScore);
for (int i = 0; i < count; i++)
{
if (student[i].huaScore == maxHuaScore)
{
printf("%s ", student[i].id);
}
}
printf("\n\n\n");
//同物理
printf("生物最高分为%lg分,对应学生id为----->", maxShengScore);
for (int i = 0; i < count; i++)
{
if (student[i].shengScore == maxShengScore)
{
printf("%s ", student[i].id);
}
}
printf("\n\n\n");
}
|
|