词频统计
SSH:git@git.coding.net:jlspduqiao/word-frequency-count.git
#include <stdlib.h> #include <stdio.h> #include <string.h> typedef struct LNode { char c[20]; int count; struct LNode *next; }LNode,*LinkList; struct LNode *Create() { LinkList L; L=(LinkList)malloc(sizeof(LNode)); L->next=NULL; return L; } void Print(LinkList L) { struct LNode *p; p = L->next; if(p!= NULL) { do { printf ("%s %d \n", p->c, p->count); p = p->next; } while (p != NULL); } } bool Find(LinkList &L,char a[20])//查找单词 { LNode *p; p=L->next; while(p!=NULL) { if(strcmp(a,p->c)==0){//找到单词 p->count++;//计数加一 return true; } else p=p->next; } return false; } void Insert(LinkList &L,char a[20]) { LNode *p; p=(LinkList)malloc(sizeof(LNode)); strcpy(p->c,a); p->count=1; p->next=L->next; L->next=p; } int main(){ struct LNode *L=Create(); char a[20]; char c; int i=0; printf("请输入:\n"); while ((c = getchar()) != '\n'){//读入并判断是否输入了回车(回车结束) if(c!=' '&&c!='.'&&c!=','&&c!='?'&&c!=':'){ a[i]=c; a[i+1]='\0'; i++; } else{ if(!Find(L,a))//如果不存在该单词 Insert(L,a); i=0; memset(a,NULL,sizeof(a)); } } Print(L); return 0; }
运行结果:
不太会java和c++,试着用链表实现词频统计,看了别的同学的从文件中调用,我感觉自己做错了,需要手动输入,过后会仔细修改一下。