【问题标题】:array sorting in randomly数组随机排序
【发布时间】:2015-03-04 05:42:18
【问题描述】:

我有以下代码,我希望应该从文件中读取文本,将单词存储在数组中,然后以随机顺序打印出来。最终数组是 int,但应该是 char,它没有给我正确的答案。

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

int main()
{
    char message[10][150], buffer[150];
    int i = 0;
    int cntr = 9;
    char freeArray[9];

    srand(time(NULL));
    freeArray[i] = rand() % cntr;
    FILE *file_in;

    file_in = fopen("test.txt", "r");

    while (fgets(buffer, 150, file_in))
    {
        i = rand() % cntr;
        strcpy(message[freeArray[i]], buffer);
    }

    while (cntr >= 0)
    {
        i = rand() % cntr;
        strcpy(message[freeArray[i]], buffer);
        freeArray[i] = freeArray[cntr--];
        printf("%s", freeArray[i]);
    }

    return 0;
}

我有替代代码,但这个代码给了我没有随机播放的文本。

#include<stdio.h>
#include<string.h>
#include <stdlib.h>
#include <time.h>
int main()
{
    /*declare and initialise variable*/
    char message[10][150],buffer[150];
    int i=0;
    int j;
    srand(time(NULL));
    FILE *file_in;
    file_in=fopen("test.txt", "r");
    /*stores and prints the data from the string*/
    while(fgets(buffer,150,file_in)){
        strcpy(message[i],buffer);

    }
    while(i < 10)
{
  j = rand() % 10;
  printf("%s\n",message[j]);
  i++;
}

    return 0;

【问题讨论】:

  • 它有助于为正确的编程语言添加标签:)
  • “它没有给我正确的答案” 太模糊了。什么正确的答案,你得到了什么?
  • 编译器说:第 23 行:数组下标的类型为 char。第 25 行:格式 %s 需要 char * 类型的参数,但参数 2 的类型为 'int'
  • 最初,freeArray[0] 被分配一个随机数,其余未初始化。然后你读入每一行,将其分配给由freeArray 随机选择的元素给出的message 的索引。你有很多工作要做......
  • @lurker:实际上,任何高于 9 的索引都会有问题。

标签: c arrays string sorting


【解决方案1】:
The following code:
-- compiles cleanly
-- contains all the changes needed to the OPs posted code

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

#define MAX_MESSAGES (10)
#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("message: %d: %s\n",j, message[j]);
    } // end for

    return 0;
} // end function: main

【讨论】:

  • 如何避免重复相同的单词?是否可以阅读一行(一个句子)中的文本。
【解决方案2】:
The following is to answer the question given in an answer block

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

#define MAX_MESSAGES (10)
#define MAX_MESSAGE_LEN (150)

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

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

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

    // implied else, fopen successful

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

    for (i = 1; i < 3; i++)
    {
        for (j = 1; j < 3; j++)
        {
            if (strcmp(message[j - 1], message[j]) > 0)
            {
                strcpy(t, message[j - 1]);
                strcpy(message[j - 1], message[j]);
                strcpy(message[j], t);
            } // end if
        } // end for
    } // end for

    printf("\nStrings in order are : ");

    for (i = 0; i < 3; i++)
    {
        printf("message: %d: %s\n", i+1, message[j]);
    } // end for

    getchar();
    return 0;
} // end function: main

【讨论】:

  • 它仅在文本在列中(1 个单词/行)并且答案给出最短单词(3 次)时才有效
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-12-21
  • 1970-01-01
  • 1970-01-01
  • 2017-09-08
  • 1970-01-01
  • 2013-04-05
  • 1970-01-01
相关资源
最近更新 更多