使“众神归位”结构体
各位大哥:就是排序之后各个数不能和原来对应,求解决。#include"stdio.h"
#include"string.h"
struct student{
char name[20];
int age;
float chinese,math,s;
};
void sum(struct student a[],int n);
void nam(struct student a[],int n);
main()
{
struct student a[3]={{"tom",21,89,90,0},{"zhu",15,86,78,0},{"fog",24,96,85,0}};
sum(a,3);
nam(a,3);
getch();
}
void sum(struct student a[],int n)
{
int i,j,k;
for(i=0;i<n;i++)
{
a[i].s+=a[i].chinese+a[i].math;
}
for(i=0;i<n-1;i++)
for(j=i;j<n;j++)
{
if(a[i].s<a[j].s)
{
k=a[i].s;
a[i].s=a[j].s;
a[j].s=k;
}
}
for(i=0;i<n;i++)
printf("s= %f %s %d %f %f\n",a[i].s,a[i].name,a[i].age,a[i].math,a[i].chinese);
}
void nam(struct student a[],int n)
{
int i,j;
char tmp[20];
for(i=0;i<n-1;i++)
for(j=i;j<n;j++)
{
if(strcmp(a[i].name,a[j].name)>0)
{ strcpy(tmp,a[i].name);
strcpy(a[i].name,a[j].name);
strcpy(a[j].name,tmp);
}
}
for(i=0;i<n;i++)
printf("%s %d %f %f\n",a[i].name,a[i].age,a[i].chinese,a[i].math);
}