从文本文件中排序数组

时间:2016-02-22 17:23:44

标签: c

我可以'似乎按升序排序我的文本文件。由于某种原因,它打印我的shuffle数组,第一个和第二个条目交换。经过几个小时的尝试,我似乎已经把自己搞糊涂了,可能会犯一些错误。

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

// Accepts: command line input
// Returns: 0 if no error

int main(int num_args, char *arg_strings[])
{
    int x = 0, i, track_count = 0;
    unsigned long Max_Length = 0;
    char line[500], *temp;
    FILE *file = fopen("playlist.txt", "r");
    /* The next line checks if the playlist file exists and if it's not there, "Cannot Open File" is printed to the screen */
    if (file == NULL)
    {
        printf("Cannot open file\n");
    }
    /* The following code identifies each line in the text and lines are shuffled accordingly */

    while (fgets(line, sizeof(line), file) != NULL)
    {
        track_count++;
        if (strlen(line) > Max_Length)
            Max_Length = strlen(line);
    }
    rewind(file);
    char *Array[track_count];
    while (fgets(line, sizeof(line), file) != NULL)
    {
        Array[x] = malloc(strlen(line));
        if (Array[x] == NULL)
        {
            printf("A memory error occurred.\n");
            return(1);
        }
        strcpy(Array[x], line);
        /* change \n to \0 */
        Array[x][strlen(Array[x]) - 1] = '\0';
        x++;
    }

    printf("The original playlist is:\n");
    for (x = 0; x < track_count; x++)
        printf("%2d %s\n", x, Array[x]);
    /*  The array will now be shuffled: */
    srand((unsigned int)time(NULL));
    for (x = track_count - 2; x > 1; x--)
    {
        while (1)
        {
            i = rand() % (track_count - 1) + 1;
            if (Array[x + 1][0] == Array[i][0])
                continue;
            if (Array[x - 1][0] == Array[i][0])
                continue;
            if (Array[i + 1][0] == Array[x][0])
                continue;
            if (Array[i - 1][0] == Array[x][0])
                continue;

            temp = Array[x];
            Array[x] = Array[i];
            Array[i] = temp;
            break;
        }
    }

    printf("\nShuffled Array\n");
    for (x = 0; x < track_count; x++)
        printf("%2d %s\n", x, Array[x]);

    /* Sorting */

    int m = 0;
    int z = 0;
    int k = 0;
    char j = 0;
    char tempArtist[Max_Length][Max_Length];

    for (m = 0; m < track_count; m++)
    {
        for (z = 0; z <track_count - 1 - m; z++)
        {
            if (strcmp(Array[j], Array[j + 1]) > 0)
            {
                strcpy(tempArtist, Array[j]);
                strcpy(Array[j], Array[j + 1]);
                strcpy(Array[j + 1], tempArtist);
            }
        }
    }
    puts("");
    printf("Sorted Playlist:");
    for (k = 0; k <= track_count; k++)
    {
        printf("\n%s", Array[k]);
    }

    return 0;
}

1 个答案:

答案 0 :(得分:0)

您的代码肯定需要一些清理工作。但主要问题是在循环中使用错误的变量名称(你有太多的变量)。现在它有效。

for (m = 0; m < track_count - 1; m++)
{
    for (z = 0; z <track_count - 1 - m; z++)
    {
        if (strcmp(Array[z], Array[z + 1]) > 0)
        {
            char* tmp;
            tmp = Array[z];
            Array[z] = Array[z + 1];
            Array[z + 1] = tmp;
        }
    }
}

puts("");
printf("Sorted Playlist:");
for (k = 0; k < track_count; k++)
{
    printf("\n%s", Array[k]);
}