数组排序随机 [英] array sorting in randomly

查看:128
本文介绍了数组排序随机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下code,我想到应该读取文件中的文本,存储词语的数组,然后打印出来以随机顺序。最后一个数组是int,但是应该是char,它并没有给我正确的答案。

 #包括LT&;&stdio.h中GT;
#包括LT&;&string.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&time.h中GT;诠释的main()
{
    炭消息[10] [150],缓冲器[150];
    INT I = 0;
    INT CNTR = 9;
    炭freeArray [9];    函数srand(时间(NULL));
    freeArray [I] =兰特()%CNTR;
    FILE * file_in;    file_in = FOPEN(test.txt的,R);    而(与fgets(缓冲器,150,file_in))
    {
        I =兰特()%CNTR;
        的strcpy(消息[freeArray [I],缓冲区);
    }    而(CNTR> = 0)
    {
        I =兰特()%CNTR;
        的strcpy(消息[freeArray [I],缓冲区);
        freeArray [I] = freeArray [cntr--]
        的printf(%S,freeArray [I]);
    }    返回0;
}

我有替代code,但是这一次让我的文字没有洗牌。

 #包括LT&;&stdio.h中GT;
#包括LT&;&string.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&time.h中GT;
诠释的main()
{
    / *声明并初始化变量* /
    炭消息[10] [150],缓冲器[150];
    INT I = 0;
    诠释J;
    函数srand(时间(NULL));
    FILE * file_in;
    file_in = FOPEN(test.txt的,R);
    / *存储并从字符串打印数据* /
    而(与fgets(缓冲器,150,file_in)){
        的strcpy(消息[I],缓冲液);    }
    而(ⅰ小于10)
{
  J =兰特()%10;
  的printf(%S \\ n,邮件[J]);
  我++;
}    返回0;


解决方案

 以下code:
- 完全编译
- 包含贴code需要OP的所有更改#包括LT&;&stdio.h中GT;
#包括LT&;&string.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&time.h中GT;的#define MAX_MESSAGES(10)
的#define MAX_MESSAGE_LEN(150)静态字符信息[MAX_MESSAGES] [MAX_MESSAGE_LEN] = {{'\\ 0'}};
静态字符缓冲区[MAX_MESSAGE_LEN] = {'\\ 0'};诠释的main()
{
    / *声明并初始化变量* /
    INT I = 0;
    诠释J;    FILE * file_in;
    如果(NULL ==(file_in = FOPEN(test.txt的,R)))
    {//那么,失败的fopen
        PERROR(FOPEN失败的test.txt);
        出口(EXIT_FAILURE);
    }    //暗示一样,成功的fopen    函数srand(时间(NULL));    / *存储并从字符串打印数据* /
    而((ⅰ&下; MAX_MESSAGES)及&放大器;与fgets(缓冲器,150,file_in))
    {
        的strcpy(消息[I],缓冲液);
        我++;
    } //结束时    的printf(\\ n显示按随机顺序\\ n%d封邮件,MAX_MESSAGES);
    的printf(有可能重复的消息,并跳过消息的\\ n);
    对于(i = 0; I< MAX_MESSAGES;我++)
    {
        J =兰特()%MAX_MESSAGES;
        的printf(消息:%D:%S \\ n,J,消息[J]);
    } //结束了    返回0;
} //结束功能:主

i have below code, that i expect should read text from file, store words in array and then print it out in random order. Final array is int, but should be char and it does not give me proper answer.

#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;
}

I have alternative code, but this one gives me text without shuffle.

#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;

解决方案

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

这篇关于数组排序随机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆