你会如何使用qsort对字符串进行排序 [英] How would you use qsort to sort on a string

查看:62
本文介绍了你会如何使用qsort对字符串进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很好奇你是否可以轻松地使用qsort对null

终止字符串中的字母进行排序,而不使用任何条件语句?


char str [] =" bdace";


变为abcde

解决方案

< blockquote>Eddy C < ED **** @ gmail.com>写道:

我很好奇你是否可以轻松地使用qsort对空的
终止字符串中的字母进行排序,而不使用任何条件语句?

char str [] =" bdace";

变为abcde




以下内容未编译或校对良好。只有

才能在系统上为你的例子产生所需的输出

按字母顺序排列的字母值增加字符值

(这是正常):


int

compare_chars(const void * a_,const void * b_)

{

const char * a =(const char *)a_;

const char * b =(const char *)b_;


return * a< * b? -1:* a> * b;

}


qsort(str,strlen(str),1,compare_chars);

-

我看到它的方式,一个不同意我的聪明人是

可能是我将与任何给定的最重要的人交换

day。"

- 比利Chambless


Eddy C写道:


我好奇你是否可以轻松地使用qsort对空的
终止字符串中的字母进行排序,而不使用任何条件语句?

char str [] =" bdace";

成为abcde




好​​吧,除了显而易见的你为什么要那样做之外问题(

答案可能是因为那是'家庭作业分配

所说的'),答案是是的,它可以完成,但''很容易''在beholder的眼睛里面。


替换:


if(char1< char2)

compare = -1;

else if(char1> char2)

compare = 1;

其他

比较= 0;


with:


int cmp [ 3] = {-1,0,1};

比较= cmp [signum(char1-char2)+1];


实现signum()没有条件作为练习留给

读者。 :-)


-

+ ------------------------ - + -------------------- + --------------------------- - +

| Kenneth J. Brody | www.hvcomputer.com | |

| kenbrody / at\spamcop.net | www.fptech.com | #include< std_disclaimer.h> |

+ ------------------------- + -------------- ------ + ----------------------------- +

不要给我发电子邮件:< mailto:Th ************* @ gmail.com>


Ben Pfaff写道:

" Eddy C" < ED **** @ gmail.com>写道:

我很好奇你是否可以轻松地使用qsort对空的
终止字符串中的字母进行排序,而不使用任何条件语句?

char str [] =" bdace" ;;

变为abcde

以下内容未编译或校对得很好。它只会在系统上为你的例子产生所需的输出,其中字母按字母顺序增加字符值
(即正常):

int
compare_chars(const void * a_,const void * b_)
{char /> const char * a =(const char *)a _;




为什么演员?

const char * b =(const char *)b_;


同样......
返回* a< * b? -1:* a> * B;


....并且这不是一个条件表达式吗?

}

qsort(str, strlen(str),1,compare_chars);




我想有人可以得到*真正*狡猾的东西,如:


#include< string.h>

int

compare_chars(const void * a_,const void * b_)

{

静态字符a [2],b [2];

a [0] = *(字符*)a_;

b [0] = *( char *)b_;

返回strcoll(a,b);

}


...但那将是*错误*不会吗?


- g


-

Artie Gold - 奥斯汀,德克萨斯州
http://goldsays.blogspot.com
http://www.cafepress.com/goldsays

" ;如果你没有什么可隐瞒的,你就不会尝试!


I''m curious if you can easily use qsort to sort the letters in a null
terminated string, without using any conditional statements?

char str[ ] = "bdace";

becomes "abcde"

解决方案

"Eddy C" <ed****@gmail.com> writes:

I''m curious if you can easily use qsort to sort the letters in a null
terminated string, without using any conditional statements?

char str[ ] = "bdace";

becomes "abcde"



The following is not compiled or well proofread. It will only
produce the desired output for your example on systems where
letters in alphabetical order have increasing character values
(which is "normal"):

int
compare_chars (const void *a_, const void *b_)
{
const char *a = (const char *) a_;
const char *b = (const char *) b_;

return *a < *b ? -1 : *a > *b;
}

qsort (str, strlen (str), 1, compare_chars);
--
"The way I see it, an intelligent person who disagrees with me is
probably the most important person I''ll interact with on any given
day."
--Billy Chambless


Eddy C wrote:


I''m curious if you can easily use qsort to sort the letters in a null
terminated string, without using any conditional statements?

char str[ ] = "bdace";

becomes "abcde"



Well, aside from the obvious "why would you want to do that" question (the
answer of which is probably "because that''s what the homework assignment
said to do"), the answer is "yes, it can be done, but ''easily'' is in the
eye of the beholder".

Replace:

if (char1 < char2)
compare = -1;
else if ( char1 > char2 )
compare = 1;
else
compare = 0;

with:

int cmp[3] = { -1,0,1 };
compare = cmp[signum(char1-char2)+1];

Implementing signum() without conditionals is left as an exercise to the
reader. :-)

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Don''t e-mail me at: <mailto:Th*************@gmail.com>


Ben Pfaff wrote:

"Eddy C" <ed****@gmail.com> writes:

I''m curious if you can easily use qsort to sort the letters in a null
terminated string, without using any conditional statements?

char str[ ] = "bdace";

becomes "abcde"

The following is not compiled or well proofread. It will only
produce the desired output for your example on systems where
letters in alphabetical order have increasing character values
(which is "normal"):

int
compare_chars (const void *a_, const void *b_)
{
const char *a = (const char *) a_;



Why the cast?
const char *b = (const char *) b_;
Similarly...
return *a < *b ? -1 : *a > *b;
....and isn''t this a `conditional expression''?
}

qsort (str, strlen (str), 1, compare_chars);



I guess one could get *really* devious with something like:

#include <string.h>
int
compare_chars(const void *a_, const void *b_)
{
static char a[2], b[2];
a[0] = *(char *)a_;
b[0] = *(char *)b_;
return strcoll(a, b);
}

...but that would be *wrong* wouldn;t it?

--ag

--
Artie Gold -- Austin, Texas
http://goldsays.blogspot.com
http://www.cafepress.com/goldsays
"If you have nothing to hide, you''re not trying!"


这篇关于你会如何使用qsort对字符串进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆