5-37 模拟EXCEL排序

来源:互联网 发布:hbase数据库设计原则 编辑:程序博客网 时间:2024/06/05 06:09

这里写图片描述

#include<stdio.h>#include<stdlib.h>#include<string.h>#define MAXID 6#define MAXNAME 8#define MAX 100000typedef 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);}
0 0