c - 字符串数组排序(唯一字符串)

标签 c string sorting

我有从文本中打印单词的代码,但需要对其进行打乱。代码可以运行,但单词会重复。我应该改变什么才能只获得独特的单词?

#define MAX_MESSAGES (3)
#define MAX_MESSAGE_LEN (150)

static char message[MAX_MESSAGES][MAX_MESSAGE_LEN] = {{'\0'}};
static char buffer[MAX_MESSAGE_LEN] = {'\0'};

int main()
{
    /*declare and initialise variable*/
    int i=0;
    int j;

    FILE *file_in;
    if( NULL == (file_in=fopen("test.txt", "r") ) )
    { // then, fopen failed
        perror( "fopen failed for test.txt" );
        exit( EXIT_FAILURE );
    }

    // implied else, fopen successful

    srand(time(NULL));

    /*stores and prints the data from the string*/
    while( (i<MAX_MESSAGES) && fgets(buffer,150,file_in) )
    {
        strcpy(message[i],buffer);
        i++;
    } // end while

    printf("\ndisplay %d messages in random order\n", MAX_MESSAGES);
    printf("with possible repeated messages and skipped messages\n");
    for( i=0; i < MAX_MESSAGES; i++)
    {
        j = rand() % MAX_MESSAGES;
        printf("%s\n",message[j]);
    } // end for

    return 0;
} 

我知道 Fisher-Yates shuffle 方法,我找到了如何描述函数,但我不明白如何在我的代码中调用它。

void shuffle(int *array, size_t n)
{
    if (n > 1) {
        size_t i;
    for (i = 0; i < n - 1; i++) {
      size_t j = i + rand() / (RAND_MAX / (n - i) + 1);
      int t = array[j];
      array[j] = array[i];
      array[i] = t;
    }
    }
}

最佳答案

重新提交答案。由于某种原因,我几乎更改了您问题代码的每一行。我建议你逐行浏览并比较。特别是,您并没有离开使用 #define 值,并且您的循环基于最大容量而不是实际读取的行数。

我添加了之前发布的随机但独特的输出。

请注意,fgets() 会留下尾随的换行符,如输出的双倍行距所示。

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

#define MAX_MESSAGES        20
#define MAX_MESSAGE_LEN     150

char message[MAX_MESSAGES][MAX_MESSAGE_LEN+1];  // allow 1 spare
char buffer [MAX_MESSAGE_LEN+1];
int pool    [MAX_MESSAGES];

int main()
{
    int size=0, n;
    FILE *file_in;
    if (NULL == (file_in=fopen("test.txt", "rt"))) {
        perror ("fopen failed for test.txt");
        exit (EXIT_FAILURE);
    }
    while (size<MAX_MESSAGES && fgets(buffer, MAX_MESSAGE_LEN, file_in))
        strcpy (message[size++],buffer);
    fclose (file_in);       // was omitted

    for (n=0; n<size; n++)  // set up message index pool
        pool [n] = n;
    srand((unsigned)time(NULL));

    printf ("\ndisplay %d messages in random order\n", size);
    printf ("with possible repeated messages and skipped messages\n");

    while (size) {          // print and remove random message
        n = rand() % size;
        printf("%s\n",message [ pool[n] ]);
        pool [n] = pool [--size];            // remove index from pool
    }
    return 0;
}

关于c - 字符串数组排序(唯一字符串),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27815759/

相关文章:

c - fopen 没有打开文件流...我该如何解决这个问题?

c++ - Windows 中最流行的 C++ gui 框架是什么?

python - 如何编写符合 PEP8 的超长字符串并防止 E501

sorting - 帮助使用 Java 代码对 GPA 进行排序

c - 如何解决编译错误: warning "Files using this header must be compiled with _SVID_SOURCE or _XOPEN_SOURCE"

c - 重新分配一个 int 数组

java - Java 中 charAt() 方法的问题

python - Python中黑名单和白名单的字符串处理

sql-server - 使用 in 语句的具有多个值的 SQL 搜索查询

json - LibreOffice Calc 是否支持 JSON 文件导入/排序?