c语言库函数 qsort(),有问题,求解答!

xiazhou88 2012-04-05 10:32:16

#include "stdio.h"
#include "algorithm"
using namespace std;
struct Node
{
double cj;//成绩
int xh;//学号
};
int cmp(const void *a, const void *b)//先按成绩排序,如成绩相同按学号排序(降序)
{
/* struct Node *c = (Node *)a;
struct Node *d = (Node *)b;
if (c->cj >= d->cj)
{
return 1;
}
else
return -1;*/
return (*(Node *)a).cj > (*(Node *)b).cj ? 1:-1;
}
int main()
{
Node node[5];
int i;
int n;
scanf("%d",&n);
for (i = 0; i < n; i++)
{
scanf("%f%d",&node[i].cj,&node[i].xh);
}
qsort(node,n,sizeof(node[0]),cmp);
for(i = 0; i < n; i++)
printf("cj:%f xh:%d\n",node[i].cj,node[i].xh);
return 0;
}

排序函数int cmp();有问题,排序double型 ,我修改后依然不行,求解答!
...全文
162 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq120848369 2012-04-06
  • 打赏
  • 举报
回复
成绩从低到高,学号从高到底的,想改次序自己修一下代码。

#include <stdio.h>
#include <stdlib.h>

struct node_st {
double score;
int snum;
};

int node_compare(const void *left, const void *right) {
const struct node_st *lnode = (const struct node_st*)left;
const struct node_st *rnode = (const struct node_st*)right;

if (lnode->score < rnode->score)
return -1;
else if (lnode->score > rnode->score)
return 1;
else if (lnode->snum > rnode->snum)
return -1;
else if (lnode->snum < rnode->snum)
return 1;
else
return 0;
}

int main(int argc, char* argv[]) {
struct node_st nodes[5];
int i;

for (i = 0; i < 5; ++ i) {
if (scanf("%lf%d", &nodes[i].score, &nodes[i].snum) != 2) {
return -1;
}
}

qsort(nodes, 5, sizeof(struct node_st), node_compare);

for (i = 0; i < 5; ++ i) {
printf("Index %d => (score : %lf, snum : %d)\n", i, nodes[i].score, nodes[i].snum);
}

return 0;
}
xiazhou88 2012-04-06
  • 打赏
  • 举报
回复
6楼是我最想要的,8楼亦行,感谢!
web_wxw 2012-04-05
  • 打赏
  • 举报
回复
同意
[Quote=引用 6 楼 的回复:]

C/C++ code

把%f改成%lf就Ok了
#include "stdio.h"
#include "algorithm"
using namespace std;
struct Node
{
double cj;//成绩
int xh;//学号
};
int cmp(const void *a, const void *b)//先按成绩排序,如成绩相同按学号排序(降序)……
[/Quote]
hen_hao_ji 2012-04-05
  • 打赏
  • 举报
回复

把%f改成%lf就Ok了
#include "stdio.h"
#include "algorithm"
using namespace std;
struct Node
{
double cj;//成绩
int xh;//学号
};
int cmp(const void *a, const void *b)//先按成绩排序,如成绩相同按学号排序(降序)
{
struct Node *c = (Node *)a;
struct Node *d = (Node *)b;
if (c->cj != d->cj)
{
return c->cj - d->cj;
}
else
return d->xh - c->xh;
// return (*(Node *)a).cj - (*(Node *)b).cj;
}
int main()
{
Node node[5];
int i;
int n;
scanf("%d",&n);
for (i = 0; i < n; i++)
{
scanf("%lf %d",&node[i].cj, &node[i].xh); //把%f改成%lf就Ok了
}
qsort(node,n,sizeof(node[0]),cmp);

for(i = 0; i < n; i++)
printf("cj:%f xh:%d\n",node[i].cj,node[i].xh);
return 0;
}


justkk 2012-04-05
  • 打赏
  • 举报
回复
转换为字符串比较呢?
int cmp(const void *a, const void *b)
{
char s1[64], s2[64];

struct Node *c = (Node *)a;
struct Node *d = (Node *)b;

sprintf(s1, "%.2lf", c->cj);
sprintf(s2, "%.2lf", d->cj);

return strcmp(s1, s2);
}
xiazhou88 2012-04-05
  • 打赏
  • 举报
回复
那肯定不行,如果都是 一点几,强制转换了,都是1 ,那咋整!
justkk 2012-04-05
  • 打赏
  • 举报
回复
再强制转换为int,嘿嘿
xiazhou88 2012-04-05
  • 打赏
  • 举报
回复
整型能行,像double之类的实型就不能呀!
justkk 2012-04-05
  • 打赏
  • 举报
回复
试试
return (*(Node *)a).cj - (*(Node *)b).cj;

69,373

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧