gpt4 book ai didi

c - 获取排序数组中每个单词的计数

转载 作者:行者123 更新时间:2023-11-30 19:04:08 25 4
gpt4 key购买 nike

以下代码段的目标是获取一个排序的字符串数组,并计算每个单词的数量。

然后将该信息放入一个名为“reduceNode”的结构中,该结构保存一个字符串和给定字符串的计数。

reduceNode 结构被放入另一个数组中。

一旦找到所有单词及其计数并将其放入中间数组中,它们就会被插入到reduceNode 结构的全局数组中。

此方法由线程调用,这就是我将结果存储到全局数组中的原因。

每当我运行程序的这一部分时,我都会遇到段错误。

我假设我正在越界访问数组,但我在缩小范围时遇到了困难。

void* reduce(void* num) //Reduce function 
{
int index = *(int*)num;
int curSize = 0; //Size of the current linked list
struct HashNode *head = HashTable[index]; //Get the head of the linked list from the hashtable
struct HashNode *linkedList = head; //Pointer to the head to traverse the linked list
while(linkedList != NULL) //Gets the size of the current linked list
{
curSize++;
linkedList = linkedList->next;
}
linkedList = head;
int linkedListTraverse = 0; //Array index for each linked list node
int numSort[curSize];
char* wordSort[curSize];
while(linkedList != NULL)
{
if(app == 1)
numSort[linkedListTraverse] = linkedList->num; //Copy the data from the linked list into an array
else
{
wordSort[linkedListTraverse] = (char*) malloc(sizeof(linkedList->string));
strcpy(wordSort[linkedListTraverse],linkedList->string); //Copy the data from the linked list into an array
}
linkedList = linkedList->next;
linkedListTraverse++;
}
if(app == 1)
{
qsort(numSort, curSize, sizeof(int), numCmpFunc); //Sort the current node
int i, j = 0;
reduceNode* numSortArray[curSize];
reduceNode* curNum;
for(i = 0; i < curSize; i++)
{
curNum = (reduceNode*) malloc(sizeof(reduceNode));
curNum->num = numSort[i];
numSortArray[i] = curNum;
}
i = 0;
while(sortedArray[i] != NULL)
{
i++;
}
for(j = 0; j < curSize; j++, i++)
{
sortedArray[i] = numSortArray[j];
}
return (void*) 0;
}
else
{
int i = 0;
while(i < curSize) //Convert all of the words to lowercase
{
char* str = wordSort[i];
char *p;
for (p = str; *p != '\0'; p++)
*p = (char)tolower(*p);
i++;
}
qsort(wordSort, curSize, sizeof(char*), stringCmpFunc); //Sort the current node
}
int curWordIndex = 0; //Exclusively for wordcount
int checkWordIndex = 1;
int curArrayIndex = 0;
reduceNode *curWord;
reduceNode* wordCountArray[curSize];
while(curWordIndex < curSize)
{
curWord = malloc(sizeof(reduceNode));
curWord->word = wordSort[curWordIndex]; //Set the word
curWord->count = 1; //Start the count out at 1
while(strcmp(wordSort[curWordIndex], wordSort[checkWordIndex]) == 0) //While the two words are equal
{
checkWordIndex++; //Advance the leading index check
curWord->count++;
if(checkWordIndex >= curSize) //If the leading index goes beyond the array bounds
{
break;
}
}
if(checkWordIndex <= curSize)
{
curWordIndex = checkWordIndex;
checkWordIndex = curWordIndex + 1;
}
else if(checkWordIndex >= curSize) //If the leading index goes beyond the array bounds
{
if(strcmp(curWord->word, wordSort[curWordIndex]) != 0)
{
curWord->word = wordSort[curWordIndex]; //Set the word
curWord->count = 1; //Start the count out at 1
curArrayIndex++;
wordCountArray[curArrayIndex] = curWord;
}
else
{
wordCountArray[curArrayIndex] = curWord;
curArrayIndex++;
}
break;
}
wordCountArray[curArrayIndex] = curWord;
curWord = NULL;
curArrayIndex++;
}
int i,j = 0;
while(sortedArray[i] != NULL)
{
i++;
}
for(j = 0; j < curSize; j++, i++)
{
sortedArray[i] = wordCountArray[j];
}
return (void*) 0;

}

reduceNode 定义为

typedef struct reduceNode
{
int count;
char *word;
int num;
} reduceNode;

sortedArray 全局声明为

reduceNode **sortedArray;

后来初始化为

sortedArray = (reduceNode **)calloc(1,sizeof(reduceNode*)*inputCount);

输入计数是读入程序的单词数

示例输入是一个数组:[alpha, alpha, bravo, charlie, charlie, charlie, delta]。预期结果为 [alpha 2, bravo 1, charlie 3, delta 1]。

最佳答案

1.您 checkWordIndex 恰好达到 curSize 并且 strcmp(wordSort[curWordIndex], wordSort[checkWordIndex] 将超出范围。我建议打印以下索引调试。

if(checkWordIndex < curSize)
{
curWordIndex = checkWordIndex;
checkWordIndex = curWordIndex + 1;
}

这段代码仍然会导致checkWordIndex == curSize

2.你分配了新的内存,记得释放它。

3.用于C中的线程安全查找互斥体。

我建议仅使用一个索引并进行迭代

while(index < cursize-1)
{
...
++index;
}

您的第一个索引是 index,第二个索引是 index+1

关于c - 获取排序数组中每个单词的计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52714604/

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