這是我在互聯網上找到的這個巨型代碼。它是一個程序,可以在文件中找到n個最常用的單詞並將其打印出來。下面的程序讀取給定的文本文件,但我想自己寫輸入文本,所以我可能會將這些單詞存儲在數組中。我該怎麼做才能讓程序讀取隨機長度的文本,下面的程序仍然可以工作?而且,如果輸入文本中有標點符號,我將不得不擺脫它們,因此文本不會僅包含從'a'到'z'的字母。我甚至需要MAX_CHARS
常數嗎?將輸入文字讀入單詞列表中並去除標點符號
#include <stdio.h>
#include <string.h>
#include <ctype.h>
# define MAX_CHARS 26
# define MAX_WORD_SIZE 32000
// A utility function to show results, The min heap
// contains n most frequent words so far, at any time
void displayMinHeap(MinHeap* minHeap)
{
int i;
// print top N word with frequency
for(i = 0; i < minHeap->count; ++i)
{
printf("%s %d\n", minHeap->array[i].word,
minHeap->array[i].frequency);
}
}
// The main funtion that takes a file as input, add words to heap
// and Trie, finally shows result from heap
void printKMostFreq(FILE* fp, int n)
{
// Create a Min Heap of Size n
MinHeap* minHeap = createMinHeap(n);
// Create an empty Trie
TrieNode* root = NULL;
// A buffer to store one word at a time
char buffer[MAX_WORD_SIZE];
// Read words one by one from file. Insert the word in Trie and Min Heap
while(fscanf(fp, "%s", buffer) != EOF)
insertTrieAndHeap(buffer, &root, minHeap);
// The Min Heap will have the n most frequent words, so print Min Heap nodes
displayMinHeap(minHeap);
}
int main()
{
int n;
scanf("%d", &n);
FILE *fp = fopen ("file.txt", "r");
if (fp == NULL)
printf ("File doesn't exist ");
else
printKMostFreq (fp, n);
return 0;
}
TL; DR!請將代碼範圍縮小到出現問題的部分,或者更好,但請創建一個[最小,完整和可驗證示例](http://stackoverflow.com/help/mcve)並向我們顯示。 –
您顯示的代碼缺少一些部分,例如MinHeap的定義。 – fuz