运用C语言的指针编写程序:有四名学生,每个学生包括学号、姓名、成绩,设计程序要求找出成绩最高者的学号
定义学生结构和数组
输入四名学生信息
比较成绩,获得最大值,输出对应的学生学号
//四名学生,每个学生包括学号、姓名、成绩,设计程序要求找出成绩最高者的学号
#include <stdio.h>
typedef struct _student
{
char id[20];
char name[20];
float f;
}student;
int main()
{
student s[4];
int i=0,maxid=0;
for(i=0;i<4;i++)
{
scanf("%s%s%f",s[i].id,s[i].name,&s[i].f);
if(s[maxid].f < s[i].f)
maxid = i;
}
printf("%s",s[maxid].id);
return 0;
}
参考一下这篇文章,应该是原题吧,你自己参考一下
https://zhidao.baidu.com/question/266461908.html
使用结构体数组,结构体包含学号、姓名、成绩等信息,然后利用查找算法计算出成绩最高者的索引,读取学号就行。
VS上能跑,记住把SDL检查否掉
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int num;
char name[10];
int score;
}st;
int compare(st* p)
{
int max = p[0].score;
int j = 0;
for (int i = 1; i < 4; i++)
{
if (p[i].score > max)
{
max = p[i].score;
j = i;
}
}
printf("%d", p[j].num);
}
int main()
{
st id[4];
for (int i = 0; i < 4; i++)
{
scanf("%d %s %d", &id[i].num, &id[i].name, &id[i].score);
}
compare(id);
return 0;
}