用字符串计数排序[core dumped]

时间:2016-11-22 00:57:42

标签: c sorting

我需要编写一个计数排序算法,它按首字母对字符串进行排序(如果它是大写或小则无关紧要),但在写完所有字符串后会出现Segmentation fault(core dumped)错误。还有一些警告(下面​​)

aaa.c: In function ‘Count’:
aaa.c:20:23: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
           C[i]=(char)((int)C[i]+(int)C[i-1]);
                       ^
aaa.c:20:33: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
           C[i]=(char)((int)C[i]+(int)C[i-1]);
                                 ^
aaa.c:20:15: warning: assignment makes pointer from integer without a cast [-Wint-conversion]
           C[i]=(char)((int)C[i]+(int)C[i-1]);
               ^
aaa.c:22:25: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
                B[(int)C[(int)A[j]]]=A[j];
                         ^
aaa.c:22:18: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
                B[(int)C[(int)A[j]]]=A[j];
                  ^
aaa.c:23:18: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
                C[(int)A[j]]=C[(int)A[j]]-1;
                  ^
aaa.c:23:31: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
                C[(int)A[j]]=C[(int)A[j]]-1;


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

#define MaxStringLength 30
#define MaxStringNumber 5
#define NumberOfLetters 26

char **A;
char **B;
char **C;

void Count(char **A, char **B, int quantity){
        int i,j;

        for (i=1;i<=NumberOfLetters;i++) C[i]=0;
        for (j=1;j<=quantity;j++)
             C[A[j][1]-96] +=1;
        for (i=2;i<=NumberOfLetters;i++)
          C[i]=(char)((int)C[i]+(int)C[i-1]);     
        for (j=MaxStringNumber;j>0;j--){
               B[(int)C[(int)A[j]]]=A[j]; 
               C[(int)A[j]]=C[(int)A[j]]-1;

        }
}

void printing(char **array, int quantity){
        int i;
        for (i=1;i<=quantity;i++)
                printf("%s \n", array[i]);
}

void reading(char **array, int quantity){
        char word[MaxStringLength];
        int i;
        for (i=1;i<=quantity; i++){
                scanf("%s",word);
                array[i] = (char*) malloc(sizeof(char)*MaxStringLength);
                strcpy(array[i],word);
        }
}

int main(){
        A=(char**) malloc((MaxStringNumber+1)*sizeof(char*));
        B=(char**) malloc((MaxStringNumber+1)*sizeof(char*));
        C=(char**) malloc((MaxStringNumber+1)*sizeof(char*));

        printf("Array A:\n");
        reading(A,MaxStringNumber);
        Count(A,B,MaxStringNumber);
        printf("Sorted:\n");
        printing(B,MaxStringNumber);

        return 0;
}

1 个答案:

答案 0 :(得分:0)

如评论所述,原始代码存在一些问题。在C指数中,从0到大小为1,而不是从1到大小。 C []需要是整数数组(或size_t)。循环的初始值和结束值以及循环结束检查,例如&lt;或&lt; =需要修复。

由于来回纠正一个错误的时间太长,我创建了一个工作示例。

此示例正在运行,但假设输入正好是5行,所有字符都是小写。 -96转换&#39; a&#39;进入1,&#39; b&#39;进入2,.......因此,在生成计数后,C [0] = 0,C [1] =&#39; a&#39;的计数,C [2] =&#39; b&#39;的计数,...

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

#define MaxStringLength 30
#define MaxStringNumber 5
#define NumberOfLetters 26

void Count(char **A, char **B, int *C, int quantity){
int i,j;
    for (i=0;i<NumberOfLetters;i++) C[i]=0;
    for (j=0;j<quantity;j++)
         C[A[j][0]-96]++;
    for (i=1;i<=NumberOfLetters;i++)
        C[i] += C[i-1];
    for (j=MaxStringNumber-1;j>=0;j--)
        B[--C[A[j][0]-96]]=A[j]; 
}

void printing(char **array, int quantity){
int i;
    for (i=0;i<quantity;i++)
    printf("%s \n", array[i]);
}

void reading(char **array, int quantity){
char word[MaxStringLength+1];
int i;
    for (i=0;i<quantity; i++){
        scanf("%s",word);
        array[i] = (char*) malloc(MaxStringLength+1);
        strcpy(array[i],word);
    }
}

int main(){
char **A;
char **B;
int   *C;
    A=(char**) malloc((MaxStringNumber)*sizeof(char**));
    B=(char**) malloc((MaxStringNumber)*sizeof(char**));
    C=(int *)  malloc((NumberOfLetters+1)*sizeof(int *));

    reading(A,MaxStringNumber);
    printf("Array A:\n");
    printing(A,MaxStringNumber);
    Count(A,B,C,MaxStringNumber);
    printf("Sorted:\n");
    printing(B,MaxStringNumber);
    /* clean up */
    for(i = 0; i < MaxStringNumber; i++)
        free(B[i]);     /* or A[i] */
    free(C);
    free(B);
    free(A);
    return 0;
}