1./结构体的基本用法
//习题
#include
#include
struct student
{
int mumber;
char name[15];
float chinese;
float math;
float english;
}_student[5];
void FuZhi(int i,int num,char *_name,float _chinens,float _math,float _english)
{
_student[i-1].mumber=num;
strcpy(_student[i-1].name,_name);
_student[i-1].chinese=_chinens;
_student[i-1].math=_math;
_student[i-1].english=_english;
}
//按照学号查找学生信息
void FindMsg(int number)
{
int i;
for(i=0;i<5;i++)
{
if(_student[i].mumber==number)
{
printf("学号:%d\n",number);
printf("姓名:%s\n",_student[i].name);
printf("语文分数:%.1f\n",_student[i].chinese);
printf("数学分数:%.1f\n",_student[i].math);
printf("英语分数:%.1f\n",_student[i].english);
}
}
}
//输出每个人的平均分
void PingJunFen()
{
int i;
float pingjunfen;
for(i=0;i<5;i++)
{
pingjunfen=(_student[i].chinese+_student[i].math+_student[i].english)/3;
printf("学号:%d,姓名:%s,平均分:%.2f\n",_student[i].mumber,_student[i].name,pingjunfen);
}
}
//输出成绩最高的学生分数
void FirstStudent()
{
printf("排名第一的是:\n");
int i,max=0;
float arr[5];
for(i=0;i<5;i++)
{
arr[i]=_student[i].chinese+ _student[i].math+_student[i].english;
}
for(i=0;i<5;i++)
{
if(max { max=arr[i]; } } for(i=0;i<5;i++) { if(arr[i]==max) { printf("学生姓名:%s,语文分数:%.1f,数学分数:%.1f,英语分数:%.1f\n", _student[i].name,_student[i].chinese,_student[i].math,_student[i].english); } } } //求出每课的最高分及名字 void SubjectMax() { int i; float Cmax=0,Mmax=0,Emax=0; for(i=0;i<5;i++) { if(Cmax<_student[i].chinese) { Cmax=_student[i].chinese; } if(Mmax<_student[i].math) { Mmax=_student[i].math; } if(Emax<_student[i].english) { Emax=_student[i].english; } } printf("语文成绩第一是:\n"); for(i=0;i<5;i++) { if(Cmax==_student[i].chinese) { printf("学号:%d,姓名:%s,分数:%.1f\n",_student[i].mumber,_student[i].name,_student[i].chinese); } } printf("\n\n数学成绩第一是:\n"); for(i=0;i<5;i++) { if(Mmax==_student[i].math) { printf("学号:%d,姓名:%s,分数:%.1f\n",_student[i].mumber,_student[i].name,_student[i].math); } } printf("\n\n英语成绩第一是:\n"); for(i=0;i<5;i++) { if(Emax==_student[i].english) { printf("学号:%d,姓名:%s,分数:%.1f\n",_student[i].mumber,_student[i].name,_student[i].english); } } } //求出每一科平均分 void SubPingjun() { int i; float Cping=0,Mping=0,Eping=0; for(i=0;i<5;i++) { Cping+=_student[i].chinese; Mping+=_student[i].math; Eping+=_student[i].english; } printf("语文平均分:%.2f\n",Cping/5); printf("数学平均分:%.2f\n",Mping/5); printf("英语平均分:%.2f\n",Eping/5); } void main() { FuZhi(1,1,"赵一",90,88,91); FuZhi(2,2,"孙二",87,85,80); FuZhi(3,3,"张三",95,82,90); FuZhi(4,4,"李四",99,83,95); FuZhi(5,5,"钱五",86,99,94); FindMsg(1); printf("\n\n"); PingJunFen(); printf("\n\n"); FirstStudent(); printf("\n\n"); SubjectMax(); printf("\n\n"); SubPingjun(); }