免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2817 | 回复: 5
打印 上一主题 下一主题

[C] [讨论]选择排序排列字符串有问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-03-29 21:07 |只看该作者 |倒序浏览
5可用积分
各位好,我这里有个字符串选择排序的问题向各位请教。问题是,按字符在英文字母表中的顺序排序出错。代码如下:

/* Test with sorting arrays in a array*/

#include <stdio.h>

#define N 10
#define M 81

void shwstr(char * str[], int n);
char ** selectsort(char * str[],int n);

int main(void){
   
    char arr[N][M];
    char *arp[M], ** app;
    int cnt = 0;
   
    printf("\nEnter a maximum of %d lines.\nPress Enter at the biginning of a "
           "line to quit.\n");
    while(cnt < N && gets(arr[cnt]) != NULL && arr[cnt][0] != '\0'){
        arp[cnt] = arr[cnt];
        cnt++;
    }
   
    printf("\nBefore sorting, the lines entered are:\n");
    shwstr(arp,cnt);
    app = selectsort(arp,cnt);
    printf("\nAfter sorting, the lines entered are:\n");
    shwstr(app,cnt);
   
    puts("\nDone!");
   
    getch();
    return 0;
}

char ** selectsort(char * str[], int n){
     
     int k,j,max;
     char * temp;
     
     for(k = 0; k < n - 1; k++){
        max = k;
        for(j = k+1; j < n; j++)
            if(strcmp(str[k],str[j]) > 0)
                max = j;
     
        temp = str[k];
        str[k] = str[max];
        str[max] = temp;}
        
    return str;
}

void shwstr(char * str[],int n){
     
    int k;
     
    for(i = 0; i < n; i++)
        puts(str[k]);
}

运行程序情况:

Enter a maximum of 10 lines.
Press Enter at the biginning of a line to quit.
A man was sitting in the chair.
He was smoking a camel cigarette.
A woman came into the scene.
She was wearing a red blouse.
A child was standing at the center of the room.
He was playing his toys.


Before sorting, the lines entered are:
A man was sitting in the chair.
He was smoking a camel cigarette.
A woman came into the scene.
She was wearing a red blouse.
A child was standing at the center of the room.
He was playing his toys.

After sorting, the lines entered are:
A child was standing at the center of the room.
He was playing his toys.
A man was sitting in the chair.
He was smoking a camel cigarette.
A woman came into the scene.
She was wearing a red blouse.

Done!

[ 本帖最后由 mcmay 于 2008-3-29 21:10 编辑 ]

最佳答案

查看完整内容

好! 我看错了.

论坛徽章:
0
2 [报告]
发表于 2008-03-29 21:07 |只看该作者
好!

我看错了.

论坛徽章:
0
3 [报告]
发表于 2008-03-29 22:01 |只看该作者
这里应该用strcpy吧.
        
  temp = str[k];
        str[k] = str[max];
        str[max] = temp;

论坛徽章:
0
4 [报告]
发表于 2008-03-29 22:03 |只看该作者
temp定义成长的一个char temp[MAXLEN];

strcpy(temp, &str[k]);
strcpy(&str[k], &str[max]);
strcpy(str+max, temp);

论坛徽章:
0
5 [报告]
发表于 2008-03-29 22:19 |只看该作者

问题解决了!

/* Test with sorting arrays in a array*/

#include <stdio.h>

#define N 10
#define M 81

void shwstr(char * str[], int n);
char ** selectsort(char * str[],int n);

int main(void){
   
    char arr[N][M];
    char *arp[M], ** app;
    int cnt = 0;
   
    printf("\nEnter a maximum of %d lines.\nPress Enter at the biginning of a "
           "line to quit.\n");
    while(cnt < N && gets(arr[cnt]) != NULL && arr[cnt][0] != '\0'){
        arp[cnt] = arr[cnt];
        cnt++;
    }
   
    printf("\nBefore sorting, the lines entered are:\n");
    shwstr(arp,cnt);
    app = selectsort(arp,cnt);
    printf("\nAfter sorting, the lines entered are:\n");
    shwstr(app,cnt);
   
    puts("\nDone!");
   
    getch();
    return 0;
}

char ** selectsort(char * str[], int n){
     
     int k,j,max;
     char * temp;
     
     for(k = 0; k < n - 1; k++){
        max = k;
        for(j = k+1; j < n; j++)
            if(strcmp(str[k],str[j]) > 0)   //问题在这里,str[k]应为 str[max]
                max = j;
     
        temp = str[k];
        str[k] = str[max];
        str[max] = temp;}
        
    return str;
}

void shwstr(char * str[],int n){
     
    int k;
     
    for(i = 0; i < n; i++)
        puts(str[k]);
}

另外,这里用不着strcpy()那么复杂,交换指针足矣。呵呵,谢谢大家了!

论坛徽章:
0
6 [报告]
发表于 2008-03-29 23:08 |只看该作者
原帖由 思一克 于 2008-3-29 22:32 发表
好!

我看错了.

谢谢!
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP