您的位置:首页 > 编程语言 > C语言/C++

5-37 模拟EXCEL排序

2016-07-29 20:43 507 查看


#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAXID 6
#define MAXNAME 8
#define MAX 100000
typedef struct node{
char id[MAXID+1];
char name[MAXNAME+1];
int grade;
}*student;
int CompareId(const void *a,const void *b)
{
return strcmp(((student)a)->id,((student)b)->id);
}
int CompareName(const void *a,const void *b)
{
int k;
k=strcmp(((student)a)->name,((student)b)->name);
if(!k)
k=strcmp(((student)a)->id,((student)b)->id);
return k;
}
int CompareGrade(const void *a,const void *b)
{
int k;
k=((student)a)->grade-((student)b)->grade;
if(!k)
k=strcmp(((student)a)->id,((student)b)->id);
return k;
}
main()
{
int N,C,i;
student stu;
scanf("%d %d",&N,&C);
stu=malloc(sizeof(struct node)*N);
for(i=0;i<N;i++)
scanf("%s %s %d",stu[i].id,stu[i].name,&stu[i].grade);
switch(C)
{
case 1:qsort(stu,N,sizeof(struct node),CompareId);break;
case 2:qsort(stu,N,sizeof(struct node),CompareName);break;
case 3:qsort(stu,N,sizeof(struct node),CompareGrade);break;
}
for(i=0;i<N;i++)
printf("%s %s %d\n",stu[i].id,stu[i].name,stu[i].grade);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c语言 排序算法