gpt4 book ai didi

比较文件内容以进行单词分组

转载 作者:行者123 更新时间:2023-11-30 15:06:02 25 4
gpt4 key购买 nike

背景:我目前正在做一个项目。主要目标是读取文件并比较单词分组。只有用户交互才能指定组长度。我的程序被放置在一个目录中。在该目录中,将有多个文本文件(最多 30 个)。我用系统(“ls/home/..... > inputfile.txt”);

    system("ls /home/..... > inputfile.txt"); 

从那里,我打开 inputfile.txt 中的文件以读取其内容。现在到实际的问题/问题部分。我为此使用的方法是队列,因为 FIFO。 (代码“link.c”:http://pastebin.com/rLpVGC00

link.c

#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include "linkedlist.h"

struct linkedList
{
char *data;
int key;
int left;
int right;
int size;
};

LinkedList createLinkedList(int size)
{
LinkedList newLL = malloc(sizeof *newLL);
newLL->data = malloc(sizeof(int) * (size+1));
newLL->size = size;
newLL->left = 0;
newLL->right = 0;
return newLL;
}

bool isFull(LinkedList LL)
{
return abs(abs(LL->left)- abs(LL->right)) == LL->size;
}

void insertFront(LinkedList LL, char *newInfo)
{
if(isFull(LL))
{
printf("FULL");
exit(1);
}

LL->data[((--(LL->left) % LL->size) + LL->size) % LL->size] = newInfo;
}

bool isEmpty(LinkedList LL)
{
return LL->left == LL->right;
}

const char * removeEnd(LinkedList LL)
{
if(isEmpty(LL))
{
return "EMPTY";
//exit(1);
}
return LL->data[((--(LL->right) % LL->size) + LL->size) % LL->size];
}

当我使用 link.c 和我的 main (Start11.c) 进行编译时,我收到两个警告

    link.c: In function ‘insertFront’:
link.c:39:64: warning: assignment makes integer from pointer without a cast [enabled by default]
LL->data[((--(LL->left) % LL->size) + LL->size) % LL->size] = newInfo;
^
link.c: In function ‘removeEnd’:
link.c:54:5: warning: return makes pointer from integer without a cast [enabled by default]
return LL->data[((--(LL->right) % LL->size) + LL->size) % LL->size];
^

完整的start11.c代码:http://pastebin.com/eskn5yxm 。从我有疑问的大部分 read() 函数中:

  fp = fopen(filename, "r");

//We want two word or three word or four word PHRASES

for (i = 0; fgets(name, 100, fp) != NULL && i < 31; i++)
{
char *token = NULL; //setting to null before using it to strtok
token = strtok(name, ":");
strtok(token, "\n");//Getting rid of that dirty \n that I hate
strcat(fnames[i], token);
char location[350];
//Copying it back to a static array to avoid erros with fopen()
strcpy(location, fnames[i]);
//Opening the files for their contents
fpp = fopen(location, "r");
printf("\nFile %d:[%s] \n", i+1, fnames[i]);

char* stringArray[400];
//Reading the actual contents
int y;
for(j = 0; fgets(info,1600,fpp) != NULL && j < 1600; j++)
{
for( char *token2 = strtok(info," "); token2 != NULL; token2 = strtok(NULL, " ") )
{
puts(token2);
++y;
stringArray[y] = strdup(token2);
insertFront(index[i],stringArray[y]);
}
}
}
//Comparisons

char take[20010],take2[200100], take3[200100],take4[200100];
int x,z;
int count, count2;
int groupV,groupV2;
for(x = 0; x < 10000; ++x)
{
if(removeEnd(index[0])!= "EMPTY")
{
take[x] = removeEnd(index[0]);
}
if(removeEnd(index[1])!= "EMPTY")
{
take2[x] = removeEnd(index[1]);
}
if(removeEnd(index[2])!= "EMPTY")
{
take3[x] = removeEnd(index[2]);
}
}
for(z = 0; z < 10; z++)
{
if(take[z] == take2[z])
{
printf("File 1 and File 2 are similar\n");
++count;
if(count == groupL)
{
++groupV;
}
}
if(take[z] == take3[z])
{
printf("File 1 and File 3 are similar\n");
++count2;
if(count == groupL)
{
++groupV2;
}
}
}

这两个警告是我尝试比较文件时结果不正确的原因吗? (是的,我意识到我“硬编码”了比较。这只是暂时的,直到我记下其中一些......)我将发布头文件作为评论。不允许我发布两个以上的链接。补充笔记:如果没有要删除的内容,removeEnd() 将返回“EMPTY”。insertFront() 是一个 void 函数。在我创建这个帐户以便我可以发帖之前,我阅读了之前关于 strttok 的问题以及如果我想插入一些内容我必须如何 strdup() 它。我还没有将我的免费函数添加到我的 read() 函数中。我也会最后这样做。

start.h ( pastebin.com/NTnEAPYE )

#ifndef START_H
#define START_H
#include "linkedlist.h"

void read(LinkedList LL,char* filename, int lineL);
#endif

linkedlist.h (pastebin.com/ykzbnCTV)

#include <stdlib.h>
#include <stdio.h>
#ifndef LINKEDLIST_H
#define LINKEDLIST_H

typedef int bool;
typedef struct linkedList *LinkedList;

LinkedList createLinkedList(int size);

bool isFull(LinkedList LL);

void insertFront(LinkedList LL, char *newInfo);

const char * removeEnd(LinkedList LL);

bool isEmpty(LinkedList LL);

#endif

最佳答案

主要问题是围绕 removeEnd (分别是 insertFrom)函数:

const char * removeEnd(LinkedList LL)
{
if (...)
return "EMPTY";
else
return LL->data[xxx];

您在第一个返回中返回一个 const char *,但在第二个返回中返回一个 char,因此会出现警告,这是一个严重的警告。

当您将返回值与调用者中的 "EMPTY" 进行比较时,这是错误的:您应该使用 strcmp 而不是比较可能相同的数组,具体取决于编译器将相同的字符串分组在同一位置,但只是偶然(并且不可移植!)

关于比较文件内容以进行单词分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39440846/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com