基本信息
源码名称:c++ 模拟excel排序
源码大小:1.50KB
文件格式:.cpp
开发语言:C/C++
更新时间:2019-12-18
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300
本次赞助数额为: 2 元×
微信扫码支付:2 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
图中红框是 排序后的结果
#include<stdio.h>
#include<string.h>
#include <stdlib.h>
#define MAXID 6
#define MAXNAME 8
#define MAXN 100000
struct Student {
char id[MAXID 1];
char name[MAXNAME 1];
int grade;
} Record[MAXN];
int ComparId(const void *a, const void *b)
{
return strcmp(((const struct Student*)a)->id,
((const struct Student*)b)->id);
}
int ComparName(const void *a, const void *b)
{
int k = strcmp(((const struct Student*)a)->name,
((const struct Student*)b)->name);
if (!k)
k = strcmp(((const struct Student*)a)->id,
((const struct Student*)b)->id);
return k;
}
int ComparGrade(const void *a, const void *b)
{
int k = (((const struct Student*)a)->grade >
((const struct Student*)b)->grade)? 1 : 0;
if (!k) {
k = (((const struct Student*)a)->grade <
((const struct Student*)b)->grade)? -1 : 0;
if (!k)
k = strcmp(((const struct Student*)a)->id,
((const struct Student*)b)->id);
}
return k;
}
int main()
{
int N, C, i;
scanf("%d %d", &N, &C);
for (i=0; i<N; i ) {
scanf("%s %s %d",Record[i].id, Record[i].name, &Record[i].grade);
}
switch (C) {
case 1: qsort(Record, N, sizeof(struct Student), ComparId); break;
case 2: qsort(Record, N, sizeof(struct Student), ComparName); break;
case 3: qsort(Record, N, sizeof(struct Student), ComparGrade); break;
}
for (i=0; i<N; i ) {
printf("%s %s %d\n", Record[i].id, Record[i].name, Record[i].grade);
}
return 0;
}