字符串排序pragram需要帮助 [英] Help needed for a string sorting pragram

查看:65
本文介绍了字符串排序pragram需要帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我是新手。下面的代码对输入的名称进行排序。

----------------------------------- -----------------------------------------

#包括< stdio.h>

#include< conio.h>

#include< string.h>

#include< ; stdlib.h>

#include< ctype.h>

#define SIZE 50

int main()

{

char * a [50];

char hold [50];

char check [50] ;

char * checkPtr;


int i,j,numb = 0,counter = 0;

for(i = 0; i< SIZE; i ++){

a [i] =(char *)malloc(sizeof(char));

}

while(numb == 0){

printf(\ n你想输入多少名字?\ n);

得到(检查);

numb = strtod(check,& checkPtr);

}


printf(" \\ \\ n输入名称。\ n");


for(i = 0; i< numb; i ++){

得到(a [i ]);

counter ++;

}


for(i = 0;我<反击 - 1; i ++){

for(j = 0; j< counter - 1; j ++){


if(strcmp(a [j],a [ j + 1])0){

sscanf(a [j],"%50s",hold);

sscanf(a [j + 1]," ;%s",a [j]);

sscanf(hold,"%s",[j + 1]);

}


}

}


printf(" \ n \ n已排序的名称是:");
= i; i< counter; i ++){

printf(" \ n%s",a [i]); < br $>
}


getch();

返回0;

}


-------------------------------------------- -------------------------------


但是如果你输入一个名字空格,sscanf函数在''for''

循环中,排序发生时会截断遇到

空格的名称:

-------------------------------------------------- -

if(strcmp(a [j],a [j + 1])0){

sscanf(a [j],"%50s" ,持有);

sscanf(a [j + 1],%s,a [j]);

sscanf(hold,"%s" ,a [j + 1]);

}

------------------------ ----------------------------

你能否给我任何建议来阻止这个?


谢谢

Hi,
I''m a newbie. The code below sorts names entered.
----------------------------------------------------------------------------
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#define SIZE 50

int main()
{
char *a[50];
char hold[50];
char check[50];
char *checkPtr;

int i, j, numb = 0, counter = 0;
for ( i = 0; i < SIZE; i++ ) {
a[i] = (char *) malloc(sizeof(char));
}

while ( numb == 0 ) {
printf("\nHow many names would you like to enter?\n");
gets(check);
numb = strtod(check,&checkPtr);
}

printf("\nEnter the names.\n");

for ( i = 0; i < numb; i++ ) {
gets(a[i]);
counter++;
}

for ( i = 0; i < counter - 1; i++ ) {
for (j = 0; j < counter - 1; j++ ) {

if ( strcmp(a[j],a[j+1]) 0 ) {
sscanf(a[j],"%50s",hold);
sscanf(a[j+1],"%s",a[j]);
sscanf(hold,"%s",a[j+1]);
}

}
}

printf("\n\nThe sorted names are:");

for ( i = 0; i < counter; i++ ) {
printf("\n%s", a[i]);
}

getch();
return 0;
}

---------------------------------------------------------------------------

But if you input names with a whitespace, sscanf functions in the ''for''
loop where the sorting occurs truncates the names from where the
whitespace is encountered:
----------------------------------------------------
if ( strcmp(a[j],a[j+1]) 0 ) {
sscanf(a[j],"%50s",hold);
sscanf(a[j+1],"%s",a[j]);
sscanf(hold,"%s",a[j+1]);
}
----------------------------------------------------
Could you give me any suggestions to prevent this?

Thanks

推荐答案

你可以用qsort来排序2D char数组

例如:

char szTable [MAX_COUNT] [MAX_WORD];


int MyStrCmp(const void * arg1,const void * arg2)

{

返回strcmp((char *)arg1,(char *)arg2));

}

qsort(szTable, MAX_COUNT,MAX_WORD,MyStrCmp);


nk ???é?????
you may use qsort to sort 2D char array
eg:
char szTable[MAX_COUNT][MAX_WORD];

int MyStrCmp( const void *arg1, const void *arg2 )
{
return strcmp( (char*)arg1, (char*)arg2) );
}
qsort(szTable, MAX_COUNT, MAX_WORD, MyStrCmp);

nk ???é?????



我是新手。下面的代码对输入的名称进行排序。

----------------------------------- -----------------------------------------

#包括< stdio.h>

#include< conio.h>

#include< string.h>

#include< ; stdlib.h>

#include< ctype.h>

#define SIZE 50

int main()

{

char * a [50];

char hold [50];

char check [50] ;

char * checkPtr;


int i,j,numb = 0,counter = 0;


for(i = 0; i< SIZE; i ++){

a [i] =(char *)malloc(sizeof(char));

}


while(numb == 0){

printf(\ n你想输入多少名字?\ n);

得到(支票);

numb = strtod(check,& checkPtr);

}


printf (\ n输入names.\ n);


for(i = 0;我<麻木; i ++){

得到(a [i]);

counter ++;

}


for(i = 0; i< counter - 1; i ++){

for(j = 0; j< counter - 1; j ++){


if(strcmp(a [j],a [j + 1])0){

sscanf(a [j],"%50s",hold);

sscanf(a [j + 1],%s,a [j]);

sscanf(hold,%s,a [j + 1]);

}


}

}


printf(" \ n \ n排序的名称是:);


for(i = 0; i< counter; i ++){

printf(" \\ \\ n%s",a [i]);

}


getch();

返回0;

}


------------------------------- --------------------------------------------


但是如果你用空格输入名字,sscanf函数在''for''
$ b $发生排序的b循环截断了遇到

空格的名称:

------------------ ----------------------------------

if(strcmp(a [j] ,a [j + 1])0){

sscanf(a [j],"%50s",hold);

sscanf(a [j + 1] ],%s,a [j]);

sscanf(hold,"%s",[j + 1]);

}

------------------------------------------- ---------

你能否给我任何建议来阻止这个?


谢谢
Hi,
I''m a newbie. The code below sorts names entered.
----------------------------------------------------------------------------
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#define SIZE 50

int main()
{
char *a[50];
char hold[50];
char check[50];
char *checkPtr;

int i, j, numb = 0, counter = 0;
for ( i = 0; i < SIZE; i++ ) {
a[i] = (char *) malloc(sizeof(char));
}

while ( numb == 0 ) {
printf("\nHow many names would you like to enter?\n");
gets(check);
numb = strtod(check,&checkPtr);
}

printf("\nEnter the names.\n");

for ( i = 0; i < numb; i++ ) {
gets(a[i]);
counter++;
}

for ( i = 0; i < counter - 1; i++ ) {
for (j = 0; j < counter - 1; j++ ) {

if ( strcmp(a[j],a[j+1]) 0 ) {
sscanf(a[j],"%50s",hold);
sscanf(a[j+1],"%s",a[j]);
sscanf(hold,"%s",a[j+1]);
}

}
}

printf("\n\nThe sorted names are:");

for ( i = 0; i < counter; i++ ) {
printf("\n%s", a[i]);
}

getch();
return 0;
}

---------------------------------------------------------------------------

But if you input names with a whitespace, sscanf functions in the ''for''
loop where the sorting occurs truncates the names from where the
whitespace is encountered:
----------------------------------------------------
if ( strcmp(a[j],a[j+1]) 0 ) {
sscanf(a[j],"%50s",hold);
sscanf(a[j+1],"%s",a[j]);
sscanf(hold,"%s",a[j+1]);
}
----------------------------------------------------
Could you give me any suggestions to prevent this?

Thanks


2006-08-12,nk< wh *************** @ hotmail.comwrote:
On 2006-08-12, nk <wh***************@hotmail.comwrote:



我是新手。下面的代码对输入的名称进行排序。

----------------------------------- -----------------------------------------

#包括< stdio.h>

#include< conio.h>
Hi,
I''m a newbie. The code below sorts names entered.
----------------------------------------------------------------------------
#include <stdio.h>
#include <conio.h>



非标准标题。当

发布到clc时,你应该删除它及其所有依赖项。

Non-standard header. You should remove it and all its dependencies when
posting to clc.


#include< string.h>

#include< stdlib.h>

#include< ctype.h>

#define SIZE 50


int main()

{

char * a [50];

char hold [50];

char check [50];

char * checkPtr;


int i,j,numb = 0,counter = 0;


for(i = 0; i< SIZE; i ++){

a [i] =(char *)malloc(sizeof(char));
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#define SIZE 50

int main()
{
char *a[50];
char hold[50];
char check[50];
char *checkPtr;

int i, j, numb = 0, counter = 0;
for ( i = 0; i < SIZE; i++ ) {
a[i] = (char *) malloc(sizeof(char));



永远不要使用malloc的结果。 sizeof(char)是定义1.所以,你要
应该有

a [i] = malloc(1);


我想知道为什么你不会只使用一系列字符;你怎么从指针中受益?
来自指针?

Never case the result of malloc. sizeof (char) is by definition 1. So, you
should have
a[i] = malloc (1);

I do wonder why you wouldn''t just use an array of chars; how do you benefit
from pointers?


}


while(numb == 0 ){

printf(\ n你想输入多少名字?\ n);

得到(检查);
}

while ( numb == 0 ) {
printf("\nHow many names would you like to enter?\n");
gets(check);



永远,永远/永远/使用gets()。改为使用fgets():

fgets(check,sizeof check,stdin);

Never, ever, /ever/ use gets(). Use fgets() instead:
fgets (check, sizeof check, stdin);


numb = strtod(check,& checkPtr) ;
numb = strtod(check,&checkPtr);



strtod()返回一个double。你想要strtol()吗?

strtod() returns a double. Did you want strtol()?


}


printf(" \\\
输入names。\ n" );


for(i = 0; i< numb; i ++){

得到(a [i]);
}

printf("\nEnter the names.\n");

for ( i = 0; i < numb; i++ ) {
gets(a[i]);



如果用户在这里输入的元数比一个字符多,那么你明白你有未定义的行为,对吗?你真的需要考虑你的

设计。 (如果由于某种原因这就是你想要的,请使用fgets()。)

You understand that you have Undefined Behavior if the user enters more
than one character here, right? You seriously need to consider your
design. (And if for some reason that is what you want, use fgets().)


counter ++;

}
counter++;
}



如果在这里添加`--counter;'',你可以避免那些丑陋的` - 1'。 (或者,

你可以消除上面的计数器++而且只是counter = i,其中
会给你相同的价值。

If you add a `--counter;'' here, you can avoid those ugly `- 1''s. (Or,
you could eliminate the counter++ above and just counter = i, which
will give you the same value.


for(i = 0; i< counter - 1; i ++){

for(j = 0; j< counter - 1; j ++){


if(strcmp(a [j],a [j + 1])0){

sscanf(a [j],"%50s",hold);
for ( i = 0; i < counter - 1; i++ ) {
for (j = 0; j < counter - 1; j++ ) {

if ( strcmp(a[j],a[j+1]) 0 ) {
sscanf(a[j],"%50s",hold);



a [j]只能容纳1个字符;因此一个字符串:"我不确定

为什么你认为你可以得到50个字符。

a[j] can only hold 1 char; and consequently one string: "". I''m not sure
why you think that you can get 50 characters into there.


sscanf(a [j + 1],"%s",a [j]);

sscanf(hold,"%s",a [j + 1]);
sscanf(a[j+1],"%s",a[j]);
sscanf(hold,"%s",a[j+1]);



同上这里;你可以复制的最多是 ;,因为那是[j]和

a [j + 1]所能容纳的。

Ditto here; the most you can copy is "", because that''s all that a[j] and
a[j+1] can hold.


}


}

}


printf(" \ n \ n,排序名称为:");


for(i = 0;我<计数器; i ++){

printf(" \ n%s",a [i]);

}


getch ();
}

}
}

printf("\n\nThe sorted names are:");

for ( i = 0; i < counter; i++ ) {
printf("\n%s", a[i]);
}

getch();



删除此行,并删除#include< conio.habove。没人想要

必须按键来结束程序。

Delete this line, and delete the #include <conio.habove. Nobody want to
have to hit a key to end a program.


返回0;

}
return 0;
}



[解释什么代码理论上应该剪断]


你有很多东西要修理,我的朋友,还有统计上有100%的几率

其他人会发现我错过的更多。


-

Andrew Poelstra< http ://www.wpsoftware.net/projects>

要通过电子邮件与我联系,请在上述域名中使用apoelstra。

" Do BOTH结束了电缆需要插上电源吗? -Anon。

[explanation of what code theorietically should do snipped]

You have a lot to fix, my friend, and there is statistically a 100% chance
that others will find even more that I missed.

--
Andrew Poelstra <http://www.wpsoftware.net/projects>
To reach me by email, use `apoelstra'' at the above domain.
"Do BOTH ends of the cable need to be plugged in?" -Anon.




hankssong,我已经尝试自己实施你的建议但是它

失败:

---------------------------------------- -------------------------------------------------- ------

#include< stdio.h>

#include< stdlib.h>

#include< string.h>


int MyStrCmp(const void * arg1,const void * arg2);


int main()

{

char * a [2] [2] = {{" element4"," element2" },{" element1",

" element3" } $;

int i,j;


qsort(a,4,8,MyStrCmp);


for(i = 0; i< 5; i ++){

for(j = 0; j <5; j ++){

printf("%) s \ n",a [i] [j]);

}

}


返回0;

}


int MyStrCmp(const void * arg1,const void * arg2)

{

return strcmp((char *)arg1,(char *)arg2);

}

------------------ -------------------------------------------------- ----------------------------------------

你能来吗?请告诉我我的错误?

非常感谢!!


hankssong, i''ve tried to implement your suggestions on my own but it
failed:
------------------------------------------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int MyStrCmp( const void *arg1, const void *arg2 );

int main()
{
char *a[2][2] = { { "element4", "element2" }, { "element1",
"element3" } };
int i,j;

qsort(a, 4, 8, MyStrCmp);

for ( i = 0; i < 5; i++ ) {
for ( j = 0; j < 5; j++ ) {
printf("%s\n", a[i][j]);
}
}

return 0;
}

int MyStrCmp( const void *arg1, const void *arg2 )
{
return strcmp( (char*) arg1, (char*) arg2 );
}
------------------------------------------------------------------------------------------------------------
Could you please tell me my mistake?
Thanks very much!!


这篇关于字符串排序pragram需要帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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