#include<stdio.h>
/*
有4名学生,每个学生的属性包括学号、姓名、成绩,要求通过指针方法
找出成绩最高者的姓名和成绩。
*/
struct stu
{
int id;
char name[20];
float score;
};
void main()
{
int i;
float max;
struct stu student[4];
struct stu *p;
struct stu *t;//指向最大值数据的指针
//输入数据
for(p = student,i = 1; p < student + 4; p++, i++)
{
printf("please input %d student's id\tname\tscore:\n", i);
scanf("%d%s%f",&p->id, p->name, &p->score);
}
p = student;
//比较
for(max = p->score; p < student + 4; p++)
{
if(student->score > max)
{
max = p->score;
}
t = p;
}
//输出最大值信息
printf("No.%d\t name:%s\t score:%f\n",t->id, t->name, t->score);
}
/*
void main()
{
struct student
{
int num;
char name[20];
float score;
};
struct student stu[4];
struct student *p;
int i;
int temp = 0;
float max;
for(p = stu,i = 1; p < stu + 4; p++,i++)
{
printf("please input %d student's id\tname\tscore:\n", i);
scanf("%d %s %f",&p->num, p->name, &p->score);
}
for(max = stu[0].score,i = 1; i < 4; i++)
{
if(stu[i].score > max)
{
max = stu[i].score;
temp = i;
}
}
p = stu + temp;
printf("\nhighest score:\n");
printf("NO.%d\tname:%s\tscore:%f\n",p->num, p->name, p->score);
}
*/