[英]C: Linked list word frequency - Standard input
Edit: Fixed duplication error in code. 编辑:修复了代码中的重复错误。 I attempted to create a word frequency analysis program that reads in from standard input.
我试图创建一个从标准输入中读取的词频分析程序。 I have two questions.
我有两个问题。
Example input: "This is a test test of the program for frequency is a this for for" 输入示例:“这是针对频率的程序的测试,是针对的”
Output: 输出:
thhiiss 1
iiss 2
aa 2
tteesstt 2
ooff 1
tthhee 1
pprrooggrraamm 1
ffoorr 3
ffrreeqquueennccyy 1
tthhiiss 1
As you can see the count close to correct for each word, but cannot figure out why the letters are duplicating. 如您所见,每个单词的计数接近正确,但无法弄清楚字母为什么重复。 Here is the code I have used:
这是我使用的代码:
#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;
}
This is my first time posting so please let me know if I did anything wrong. 这是我第一次发帖,所以如果我做错了任何事情,请告诉我。 Any help is appreciated.
任何帮助表示赞赏。
Thanks. 谢谢。
Look at this carefully, it is assigning ch
twice to *p
仔细看一下,它将
ch
两次分配给*p
if( ch >= 97 && ch <= 122)//making the ch lowercase if needed
*p++ = ch;
else{ch += 32;}
*p++ = ch;
I think the trailing "}" on the else statement is misplaced. 我认为else语句后的“}”放错了位置。
if( ch >= 97 && ch <= 122) { //making the ch lowercase if needed
*p++ = ch;
} else {
ch += 32;
*p++ = ch;
}
Also, your code will be greatly more readable if you learn about the functions isalpha
, islower
, isupper
, tolower
, toupper
. 另外,如果您了解函数
isalpha
, islower
, isupper
, tolower
和toupper
,则代码的可读性也将大大提高。 man ctype for info. man ctype以获取信息。
Would it be better to use EOF or the null terminator '\0' ?
Use EOF because if you press ctl+D
getchar()
consider input as EOF
. 使用EOF是因为如果您按
ctl+D
getchar()
输入视为EOF
。 You can also use \\n
or Use an OR logic with \\n
, EOF
您还可以使用
\\n
或使用OR逻辑与\\n
, EOF
what is wrong with my output it doubles the letters up every time ?
See this below code in function getNextWord()
请参见以下函数
getNextWord()
中的代码
if( ch >= 97 && ch <= 122)//making the ch lowercase if needed
*p++ = ch;
//this checks if input character is lowercase character, then store it into buffer
else{ch += 32;} // if input character is lowercase character, won't execute else part
*p++ = ch;
// now again you are copying same input character into buffer next location
Modify above part. 修改上面的部分。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.