数组排序随机

编程入门 行业动态 更新时间:2024-10-27 12:34:46
本文介绍了数组排序随机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有以下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

更多推荐

数组排序随机

本文发布于:2023-11-30 00:58:02,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1648195.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!