C 字符串数组排序(唯一字符串)
我有从文本中打印单词的代码,但它需要洗牌。代码是有效的,但单词是重复的。我应该改变什么以获得唯一的单词C 字符串数组排序(唯一字符串),c,string,sorting,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 i
#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;
}
#定义最大消息数(3)
#定义最大消息长度(150)
静态字符消息[MAX_MESSAGES][MAX_message_LEN]={{{{'\0'};
静态字符缓冲区[最大消息长度]={'\0'};
int main()
{
/*声明并初始化变量*/
int i=0;
int j;
文件*FILE_in;
if(NULL==(文件_in=fopen(“test.txt”,“r”))
{//然后,fopen失败了
perror(“fopen未能通过test.txt”);
退出(退出失败);
}
//否则,fopen成功了
srand(时间(空));
/*存储并打印字符串中的数据*/
而(一){
尺寸i;
对于(i=0;i
重新提交答案。出于这样或那样的原因,我几乎更改了你问题的每一行代码。我建议你逐行检查并比较。特别是,你没有经常使用\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;
}
#包括
#包括
#包括
#包括
#定义最大消息数20
#定义最大消息长度150
char message[MAX_MESSAGES][MAX_message_LEN+1];//允许1个备用
字符缓冲区[最大消息长度+1];
int池[MAX_MESSAGES];
int main()
{
int size=0,n;
文件*FILE_in;
if(NULL==(文件中的文件=fopen(“test.txt”、“rt”)){
perror(“fopen未能通过test.txt”);
退出(退出失败);
}
while(可能重复的int数组[MAX_MESSAGES]={0,1,2};
//按循环设置值;然后调用shuffle(数组,MAX_MESSAGES);
..printf(“%s\n”,message[array[j]”)
各位,也许你们知道如何生成从1到10的随机但唯一的数字?我想这会解决我的问题。从0开始,因为数组的索引在C中从0开始。也许你们可以告诉我如何使用池[n]数组来排序我的字符串数组?我试着这样做,但现在我总是得到条目nr。1@rimasbimas我扩大了我先前的回答。
#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;
}