编辑:修复了代码中的重复错误。我试图创建一个从标准输入读取的词频分析程序。我有两个问题。
- 目前我正在使用 '\n' 来指示我的程序何时应该停止读取输入,我需要它读取直到用户完成输入。使用 EOF 或空终止符 '\0' 会更好吗
- 这可能是一个愚蠢的问题,但我无法弄清楚我的输出有什么问题,它每次都会将字母加倍。
示例输入:“This is a test test of the program for frequency is a this for for”
输出:
thhiiss 1
iiss 2
aa 2
tteesstt 2
ooff 1
tthhee 1
pprrooggrraamm 1
ffoorr 3
ffrreeqquueennccyy 1
tthhiiss 1
如您所见,每个单词的计数接近正确,但无法弄清楚字母重复的原因。这是我使用的代码:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "list.h"
#define MAXWORD 100
//===========================================================================
struct lnode {
struct lnode *next;
struct lnode *counter;
struct lnode *pLast;
struct lnode *prev;
struct lnode *head;
char *word;
int line;
int count;
int freq;
};
struct lnode *start = NULL;
//===========================================================================
struct lnode *createWordCounter(char *str)
{
struct lnode *pCounter = NULL;
pCounter = (struct lnode*)malloc(sizeof(struct lnode));
pCounter->word = (char*)malloc(strlen(str)+1);
strcpy(pCounter->word, str);
pCounter->freq = 1;
pCounter->next = NULL;
return pCounter;
}
//===========================================================================
void addWord(char *str)
{
struct lnode *pCounter = NULL;
struct lnode *pLast = NULL;
if(start == NULL)
{
start = createWordCounter(str);
return;
}
// If the word is in the list, increment its count
pCounter = start;
int temp = pCounter->freq;
while(pCounter != NULL)
{
if(strcmp(str, pCounter->word) == 0)
{
pCounter->freq++;
return;
}
pLast = pCounter;
pCounter = pCounter->next;
}
// Word is not in the list, add it
pLast->next = createWordCounter(str);
}
//===========================================================================
int getNextWord(char *buf, int bufsize) {
char *p = buf;
char ch;
do {
ch = getchar();
if (ch == '\n')
return 0;
} while (!((ch >= 'A' && ch <= 'Z')||( ch >= 'a' && ch <= 'z')));
do {
if (p - buf < bufsize - 1){
if( ch >= 97 && ch <= 122)//making the ch lowercase if needed
*p++ = ch;
else{ch += 32;
*p++ = ch;}
}//End of if
ch = getchar();
} while (((ch >= 'A' && ch <= 'Z')||( ch >= 'a' && ch <= 'z')));
*p = '\0';
return 1;
}
//===========================================================================
void show(struct lnode *pWord)
{
printf("%s %i\n", pWord->word, pWord->freq);
}
//===========================================================================
int main(){
struct lnode *counter = NULL;
int size = 1000;
char buf[MAXWORD];
while(getNextWord(buf, size) != 0 ){
addWord(buf);
}
counter = start;
while(counter != NULL)
{
show(counter);
counter = counter->next;
}
counter = start;
while(counter != NULL)
{
free(counter->word);
start = counter;
counter = counter->next;
free(start);
}
return 0;
}
这是我第一次发帖,如果我做错了什么请告诉我。任何帮助表示赞赏。
谢谢。