- -
代码有很多,我还是第一次打出这么多的代码。。。
好累啊
#include"stdio.h"
#include"stdlib.h"
typedef struct
{
char id[8];
char name[10];
char sex[2];
float score[3];
}student;
void cptcourseavg(student *ps,int n)
{
int i,j;
float sum;
for(i=0;i<3;i++)
{
sum=0;
for(j=0;j<n;j++)
sum+=ps[j].score[i];
printf("第%d门课程的平均成绩为:%.2f\n",i+1,sum/n);
}
}
void output(student *ps,int n);
void cptmaxscore(student *ps,int n)
{
student *ps2=ps;
float sum,max=0;
int i;
float *pn=(float *)malloc(n*sizeof(int));
float *pn2=pn;
student *pos;
while(ps<ps2+n)
{
sum=0;
for(i=0;i<3;i++)
sum+=ps->score[i];
*pn=sum;
if(sum>max)
{
max=sum;
pos=ps;
}
ps++;
pn++;
}
printf("总成绩最高的学生信息:\n");
output(pos,1);
}
void output(student * ps,int n)
{
student *ps2=ps;
while(ps<ps2+n)
{
printf("学号:%s\t姓名:%s\t性别:%s\n",ps->id,ps->name,ps->sex);
printf("各科成绩为:c语言=%.2f\t高数=%.2f\t英语=%.2f\n",ps->score[0],ps->score[1],ps->score[2]);
putchar('\n');
ps++;
}
}
void sort(student *ps,int n)
{
int i,j,k;
student t;
for(i=0;i<n-1;i++)
{
k=i;
for(j=i+1;j<n;j++)
{
if(ps[j].score[0]>ps[k].score[0])
k=j;
}
if(k!=j)
{
t=ps[i];
ps[i]=ps[k];
ps[k]=t;
}
}
}
int mian()
{
student stu[5]={
{"001","jack","f",50,50,50},
{"002","rose","m",60,60,60},
{"003","lucy","m",90,90,90},
{"004","lily","m",30,30,30},
{"005","jime","f",80,80,80}
};
printf("课程的平均成绩如下:\n");
cptcourseavg(stu,5);
printf("\n总成绩最高的学生信息如下:");
cptmaxscore(stu,5);
printf("\n按c语言从高到低的顺序为:\n");
sort(stu,5);
output(stu,5);
return 0;
}