结构体成员排序问题
程序代码:
#include<stdio.h> #include<string.h> struct Student { char num[6]; char name[10]; int math; int english; int general; }; struct Student stu[100]={{"05001","Jam",78,68,146},{"05002","Bill",80,98,178},{"05003","Bob",82,83,165},{"05004","Sam",69,82,151},{"05005","Steven",94,83,177}}; void exchange(struct Student stu[],struct Student *p1,struct Student *p2) //交换函数 { int matht,englisht,generalt; char namet[10],numt[6]; strcpy(numt,p1->num);strcpy(p1->num,p2->num);strcpy(p2->num,numt); strcpy(namet,p1->name);strcpy(p1->name,p2->name);strcpy(p2->name,namet); matht=p1->math;p1->math=p2->math;p2->math=matht; englisht=p1->english;p1->english=p2->english;p2->english=englisht; generalt=p1->general;p1->general=p2->general;p2->general=generalt; } //功能八,通过总分信息排序 void main() { int i,j,k,m,p; int hign; for(m=0;m<100;m++) if(stu[m].num[0]==NULL) { hign=m-1; break; } printf("%d\n",hign); for(i=hign;i<=0;i--) //我感觉着部分好像没有执行啊! for(j=0;j<i;j++) if(p=stu[j].general>stu[j+1].general) { printf("%d",p); exchange(stu,&stu[j],&stu[j+1]); //冒泡排序 } for(k=0;k<100;k++) if(stu[k].num[0]!=NULL) printf("%s %s %d %d %d\n",stu[k].num,stu[k].name,stu[k].math,stu[k].english,stu[k].general); }我感觉着有一部分好像没有执行啊!