統計檔案中各單詞出現的頻率(二叉排序樹實現)

來源:互聯網
上載者:User

統計檔案中各單詞出現的頻率(二叉排序樹實現)

#include<stdio.h>#include<stdlib.h>#include<string.h>#include<ctype.h>#define MAX 50typedef struct BTNode{char *word;unsigned long count;struct BTNode *lchild;struct BTNode *rchild;}BTNode;void GetWord(FILE *fp,int lim,char word[])//擷取一個單詞{char *w=word;int c;while(isspace(c=getc(fp)))//跳過空格;if(c!=EOF)*word++=c;if(!isalpha(c))//單詞第一個不是字母,退出{*word='\0';return ;}for( ;--lim>0;word++)if(!isalnum(*word=getc(fp)))//不是字母或數字,退出break;*word='\0';word=w;}BTNode *AddTree(BTNode *root,char *w){if(root==NULL){root=(BTNode *)malloc(sizeof(BTNode)*1);if(!root){printf("No enough memory!\n");exit(-1);}else{root->word=(char *)malloc(sizeof(char)*MAX);if(!root->word){printf("No enough memory!\n");    exit(-1);}}strcpy(root->word,w);root->count=1;root->lchild=root->rchild=NULL;}else if(strcmp(w,root->word)==0)root->count++;else if(strcmp(w,root->word)>0)root->rchild=AddTree(root->rchild,w);elseroot->lchild=AddTree(root->lchild,w);return root;}void TreePrint(BTNode *root,FILE *fp){if(root){TreePrint(root->lchild,fp);fprintf(fp,"%4ld %s\n",root->count,root->word);TreePrint(root->rchild,fp);}}void DTree(BTNode *root)//銷毀二叉樹{BTNode *p=root;if(!p)return;DTree(p->lchild);DTree(p->rchild);free(p);p=NULL;}int main(int argc,char *argv[]){BTNode *root=NULL;FILE *fp,*out;char word[MAX];int sumwords=0;fp=fopen("text.txt","r");out=fopen("out.txt","w");if(fp==NULL){printf("Can not open the file!\n");exit(-1);}if(out==NULL){printf("Can not open the file!\n");exit(-1);}while(!feof(fp)){GetWord(fp,MAX,word);if(isalpha(word[0]))//如果Word第一個是字母,則加入到二叉樹中{root=AddTree(root,word);sumwords++;}}TreePrint(root,out);    printf("The all words are: %d\n",sumwords);fclose(fp);fclose(out);DTree(root);return 0;}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.