免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
楼主: 无风之谷
打印 上一主题 下一主题

让我欢喜让我忧——我的C++之路 [复制链接]

论坛徽章:
0
41 [报告]
发表于 2012-04-16 18:31 |显示全部楼层
gvim 发表于 2012-04-16 18:04
C版里面0&NULL的争论火过好多次啦 让他自己看看吧。从while strlen, memory.h, 0&NULL,等综合 ...


作者是M$的MVP,还是想听听他的看法

论坛徽章:
0
42 [报告]
发表于 2012-04-17 15:50 |显示全部楼层
陈良乔 发表于 2012-04-17 15:04
后继章节

A.C.E  实现文件排序模块


你程序验证过了吗?
能不能把完整程序,测试用例全部贴出来看看?

论坛徽章:
0
43 [报告]
发表于 2012-04-17 18:03 |显示全部楼层
本帖最后由 0xC1988 于 2012-04-17 18:04 编辑
陈良乔 发表于 2012-04-17 17:34
我想请教一下,在什么时候,什么情况下会出现问题?

The  qsort() function sorts an array with nmemb elements
       of size size.  The base argument points to the start  of
       the array.

       The  contents of the array are sorted in ascending order
       according to a comparison function pointed to by compar,
       which  is  called  with  two arguments that point to the
       objects being compared.

       The comparison function  must  return  an  integer  less
       than,  equal to, or greater than zero if the first argu?
       ment is considered to be respectively less  than,  equal
       to,  or greater than the second.  If two members compare
       as equal, their order in the sorted array is undefined.


你的代码,a>b return 0,a=b return 1,a<b return 1,我还是想问,你代码验证过吗?

论坛徽章:
0
44 [报告]
发表于 2012-04-18 11:38 |显示全部楼层
陈良乔 发表于 2012-04-18 11:03
简单试了一下,并不充分
所以我想请教一下什么情况下会出现问题?


自己想吧,写书的这都分析不出来那还是别写了,提示已经够明确了

论坛徽章:
0
45 [报告]
发表于 2012-04-18 13:17 |显示全部楼层
本帖最后由 0xC1988 于 2012-04-18 13:18 编辑
陈良乔 发表于 2012-04-18 11:55
才疏学浅阿,所以来这里请教

能不能指点一下?给一个测试用例?


我请教你几个问题
a<b ,a>b a=b你比较函数返回什么?
在qsort里面比较函数里面返回值-1,0,1代表什么?
什么是稳定排序和不稳定排序?

论坛徽章:
0
46 [报告]
发表于 2012-04-18 16:21 |显示全部楼层
陈良乔 发表于 2012-04-18 15:20
我只想找一个反例,来说明必须返回=0,否则会出现问题
可是遗憾的是,我现在还没有找到,能举一个例子 ...


你贴出来的代码是有问题的,而且已经给你指出症结所在了,
至少说明两个问题:
1、你对写书的态度不严谨
2、你对不稳定排序没有什么概念

论坛徽章:
0
47 [报告]
发表于 2012-04-18 16:32 |显示全部楼层
陈良乔 发表于 2012-04-18 15:20
我只想找一个反例,来说明必须返回=0,否则会出现问题
可是遗憾的是,我现在还没有找到,能举一个例子 ...

// 比较规则函数
int cmp(const void* a,const void* b)
{
        // 将void*类型的参数转换为实际的txtfile*类型
const txtfile* file1 = a;
        const txtfile* file2 = b;

        // 比较txtfile结构体的词频
return file1->correlation > file2->correlation ? 0 : 1;


}

// 文件排序模块的实现
void sortfiles(txtfile* files,int count)
{
        // 调用qsort()函数对数组进行排序
qsort(files,count,sizeof(txtfile),cmp);        
}


给你代码拍个照

论坛徽章:
0
48 [报告]
发表于 2012-04-18 16:49 |显示全部楼层
gvim 发表于 2012-04-18 16:39
回复 568# 陈良乔


难道他那个代码在VC里面不会出错?

论坛徽章:
0
49 [报告]
发表于 2012-04-18 17:01 |显示全部楼层
gvim 发表于 2012-04-18 16:53
回复 586# 0xC1988


论坛徽章:
0
50 [报告]
发表于 2012-04-19 11:11 |显示全部楼层
AD8018 发表于 2012-04-19 11:04
http://bbs.chinaunix.net/forum.p ... 032116&fromuid= ...


AD你应该习惯,有时候人会选择性失明的
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP