1
void sortArray(char* array[], int count){
int compare;
int index;
 for(index = 0; index < count; index++){ //runs through array, keeping track of index
   for(compare = index; compare < count; compare++){
        if (strcmp(array[compare] , array[index]) <= 0){
             swap(*(array+compare), *(array+index));
        }
   }
 }
}
void swap(char *strA, char *strB){
    char *temp = (char *)malloc((strlen(strA)+1) * sizeof(char));
    assert(temp!=NULL);
    strcpy(temp, strA);
    free(strA);
    strA=(char *)malloc((strlen(strB)+1) * sizeof(char));
    assert(strA!=NULL);
    strA=strcpy(strA, strB);
    free(strB);
    strB= (char *)malloc((strlen(temp)+1) * sizeof(char));
    assert(strB!=NULL);
    strcpy(strB, temp);
    free(temp);
}

出力を与えます:

Array1[0]: Adam Pendleton
Array1[1]: Jeison Ortega
Array1[2]: Theodore Staake
Array1[3]: Patrick Mahoney
Array1[4]: Andrew Silberstein
Array1[5]: Alan Boulais
Array1[6]: Daniel Comeau
Array1[7]: Sean Mikhael
Array1[8]: Sarah Shank
Array1[9]: Daniel Verge
Array1[10]: Dhimitris Natsis
Array1[11]: Kathleen Lent
Array1[12]: Osei Collins
Array1[13]: Jason Hintlian
Array1[14]: Michael Gibson
Array1[15]: Alex Grossi
Array1[16]: Michael Svedberg
Array1[17]: Randall White
Array1[18]: Alvin Cordor
Array1[19]: Rodrigo Roldan
Array1[20]: Matthew McArthur
Array1[21]: Jesse Anaya
    Sorted Array1[0]: Adam Pendleton
    Sorted Array1[1]: Patrick Mahoney
    Sorted Array1[2]: Theodore Staake
    Sorted Array1[3]: Sarah Shank
    Sorted Array1[4]: Dhimitris Natsis
    Sorted Array1[5]: Alan Boulais
    Sorted Array1[6]: Alex Grossi
    Sorted Array1[7]: Alvin Cordor
    Sorted Array1[8]: Sean Mikhael
    Sorted Array1[9]: Osei Collins
    Sorted Array1[10]: Michael Svedberg
    Sorted Array1[11]: Daniel Comeau
    Sorted Array1[12]: Daniel Verge
    Sorted Array1[13]: Jason Hintlian
    Sorted Array1[14]: Jesse Anaya
    Sorted Array1[15]: Michael Gibson
    Sorted Array1[16]: Matthew McArthur
    Sorted Array1[17]: Randall White
    Sorted Array1[18]: Rodrigo Roldan
    Sorted Array1[19]: Kathleen Lent    <-----not sure why this is dupe
    Sorted Array1[20]:                  <-----not sure why this is here
    Sorted Array1[21]: Kathleen Lent

これらの空白がどこから来ているのか、なぜ重複した名前があるのか​​ わかりません。最初と最後の文字列を交換しただけのreverseArrayという別の関数を作成しましたが、問題はswap関数に起因するようです.文字列をコピーしているときに混乱が生じていると思います.

これは起こっていることのほんの一部であるため、ファイルから 1 行ずつテキストを読み込んで、char* 配列 [] に名前の文字列を入力しました。並べ替えました。

4

2 に答える 2

0

正確な問題についてはまだわかりませんが、文字列自体を変更する代わりに、配列内の文字列の場所を交換することで、はるかに簡単で効率的な作業を行うことができます。

void sortArray(char* array[], int count) {
    int compare;
    int index;
    char *tmp;
    for(index = 0; index < count; index++) {
        for(compare = index; compare < count; compare++) {
            if (strcmp(array[compare], array[index]) <= 0) {
                tmp = array[compare];
                array[compare] = array[index];
                array[index] = tmp;
            }
        }
    }
}
于 2013-03-16T01:36:40.300 に答える
0

コードの正確な問題は、swap()関数が渡された 2 つのポインターを解放しており、割り当てた新しいメモリへのポインターをどのような形式でも返さないことです。その結果、並べ替えている配列内のすべてのポインターは、解放されたメモリを指すことになります。まったく機能するのは不思議です。

これを修正するには、 のシグネチャを変更して、swap()参照によって 2 つのポインターを取ることができます。

void swap (char **strA, char **strB)

ポインタを次のように渡します。

swap(array+compare, array+index);

そうは言っても、データをコピーするのではなく、ポインタを交換するだけであるという Claudiu の提案は、長期的にはおそらくはるかに優れたアイデアです。:)

于 2013-03-16T16:03:23.007 に答える