【发布时间】:2015-04-17 12:06:33
【问题描述】:
我在互联网上找到了这个巨大的代码。它是一个程序,可以在文件中找到 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;博士!请将代码缩小到您遇到问题的部分,或者更好的是,请创建一个Minimal, Complete, and Verifiable Example 并向我们展示。
-
您显示的代码缺少某些部分,例如
MinHeap的定义。
标签: c arrays input punctuation