设计一个学生类Stu,包括学生姓名、成绩(char*name;double score),并求出下列一组学生:{Stu(“zhang”,
78),Stu(“wang”,80),Stu("zhao",92),Stu("li",65),Stu("chen",50)}中的最高分和最低分...
78),Stu(“wang”,80),Stu("zhao",92),Stu("li",65),Stu("chen",50)}中的最高分和最低分
展开
展开全部
#include<stdio.h>
struct Stu
{
char name[10];
float score;
}student[5]={{"zhang",78},{"wang",80},{"zhao",92},{"li",65},{"chen",50}};
int highscore(struct Stu *pst)
{
struct Stu stu;
int i,j;
stu=pst[0];
for(i=0;i<5;i++)
{
if(pst[i].score>stu.score)
{
stu=pst[i];j=i;
}
}
return j;
}
int lowscore(struct Stu *pst)
{
struct Stu stu;
int i,j;
stu=pst[0];
for(i=0;i<5;i++)
{
if(pst[i].score<stu.score)
{
stu=pst[i];j=i;
}
}
return j;
}
void main()
{
int lowindex,highindex;
lowindex=lowscore(student);
highindex=highscore(student);
printf("最高分是%s,分数:%.2f\n",student[highindex].name,student[highindex].score);
printf("最低分是%s,分数:%.2f\n",student[lowindex].name,student[lowindex].score);
}
调试过了,可以出结果
struct Stu
{
char name[10];
float score;
}student[5]={{"zhang",78},{"wang",80},{"zhao",92},{"li",65},{"chen",50}};
int highscore(struct Stu *pst)
{
struct Stu stu;
int i,j;
stu=pst[0];
for(i=0;i<5;i++)
{
if(pst[i].score>stu.score)
{
stu=pst[i];j=i;
}
}
return j;
}
int lowscore(struct Stu *pst)
{
struct Stu stu;
int i,j;
stu=pst[0];
for(i=0;i<5;i++)
{
if(pst[i].score<stu.score)
{
stu=pst[i];j=i;
}
}
return j;
}
void main()
{
int lowindex,highindex;
lowindex=lowscore(student);
highindex=highscore(student);
printf("最高分是%s,分数:%.2f\n",student[highindex].name,student[highindex].score);
printf("最低分是%s,分数:%.2f\n",student[lowindex].name,student[lowindex].score);
}
调试过了,可以出结果
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询