7、编写一个程序,能打开、读入一个文本文件并统计文件中每个单词出现的次数。用改进的二叉搜索树存储单词及其出现的次数。程序读入文件后,会提供一个有三个选项的菜单。第一个选项为列出所有单词连同其出现的次数。第二个选项为让您输入一个单词,程序报告该单词在文件中出现的次数。第三个选项为退出。
断断续续编写代码、测试,花了差不多两天时间,总算告成。
*建立该问题的二叉树节点模型
//BST Model--START------------------ typedef struct node { char word[15]; int times; //出现的次数 struct node *left; struct node *right; }Node; typedef struct tree { Node *root; int num; //树中单词数 }Tree; //BST Model--FINISH--------------------
*研究本题需求,实现接口代码
//BST function START----------------------------- //初始化树 void IniTree(Tree *ptree) { ptree->root=NULL; ptree->num=0; } //查找树中有无指定单词 //若未找到,返回NULL;若找到返回指定项目所在节点地址 Node *FindWord(char word[15],const Node *root) { Node *pnode; pnode=root; if(root==NULL) { return NULL; } while(strcmp(word,pnode->word)!=0 && (pnode!=NULL)) { if(strcmp(word,pnode->word)<0) pnode=pnode->left; else pnode=pnode->right; } return pnode; } //添加单词节点 //通过FindWord做支撑,若存在,则相应节点次数+1 //若不存在,则找到合适的空位,同时定位该空位的父节点 //创建该单词的节点,并判断在空位父节点的左还是右,然后添加 void AddWord(char word[15],Tree *ptree) { Node *pnode,*newnode,*fathernode; pnode=FindWord(word,ptree->root); if(pnode!=NULL) { pnode->times++; } else { newnode=ptree->root; while(newnode!=NULL) //找空位,并且定位空位的父节点 { if(strcmp(word,newnode->word)<0) { fathernode=newnode; newnode=newnode->left; } else { fathernode=newnode; newnode=newnode->right; } } newnode=(Node*)malloc(sizeof(Node)); newnode->left=NULL; newnode->right=NULL; newnode->times=1; strcpy(newnode->word,word); //设置节点信息 if(ptree->root==NULL) //如果树为空 { ptree->root=newnode; ptree->num++; } else { if(strcmp(word,fathernode->word)<0) //说明父节点左子节点为空 { fathernode->left=newnode; ptree->num++; } else { fathernode->right=newnode; ptree->num++; } } } } //打印树中单词信息 void PrintTree(const Node *root) { if(root!=NULL) { PrintTree(root->left); printf("the word is %s,its times is %d. ",root->word,root->times); PrintTree(root->right); } } //输入单词,显示次数 void showtimes(const Tree *ptree) { Node *pnode; char word[15]; printf("input the word. "); gets(word); pnode=FindWord(word,ptree->root); if(pnode==NULL) printf("the word:%s is not in the tree. ",word); else printf("the word:%s times in tree is:%d. ",word,pnode->times); } //BST function FINISH---------------
*完整代码:
#include<stdio.h> #include<string.h> #include<stdlib.h> //BST Model--START------------------ typedef struct node { char word[15]; int times;
struct node *left; struct node *right; }Node; typedef struct tree { Node *root; int num;
}Tree; //BST Model--FINISH-------------------- //BST function START----------------------------- //初始化树 void IniTree(Tree *ptree) { ptree->root=NULL; ptree->num=0; } //查找树中有无指定单词 Node *FindWord(char word[15],const Node *root) { Node *pnode; pnode=root; if(root==NULL) { return NULL; } while(strcmp(word,pnode->word)!=0 && (pnode!=NULL)) { if(strcmp(word,pnode->word)<0) pnode=pnode->left; else pnode=pnode->right; } return pnode; } //添加单词节点 void AddWord(char word[15],Tree *ptree) { Node *pnode,*newnode,*fathernode; pnode=FindWord(word,ptree->root); if(pnode!=NULL) { pnode->times++; } else { newnode=ptree->root; while(newnode!=NULL)
{ if(strcmp(word,newnode->word)<0) { fathernode=newnode; newnode=newnode->left; } else { fathernode=newnode; newnode=newnode->right; } } newnode=(Node*)malloc(sizeof(Node)); newnode->left=NULL; newnode->right=NULL; newnode->times=1; strcpy(newnode->word,word);
if(ptree->root==NULL)
{ ptree->root=newnode; ptree->num++; } else { if(strcmp(word,fathernode->word)<0)
{ fathernode->left=newnode; ptree->num++; } else { fathernode->right=newnode; ptree->num++; } } } } //打印树中单词信息 void PrintTree(const Node *root) { if(root!=NULL) { PrintTree(root->left); printf("the word is %s,its times is %d. ",root->word,root->times); PrintTree(root->right); } } //输入单词,显示次数 void showtimes(const Tree *ptree) { Node *pnode; char word[15]; printf("input the word. "); gets(word); pnode=FindWord(word,ptree->root); if(pnode==NULL) printf("the word:%s is not in the tree. ",word); else printf("the word:%s times in tree is:%d. ",word,pnode->times); } //BST function FINISH--------------- int main() { FILE *fp; Tree *wordtree; Node *new_node; char newword[15]; char ch,cc; int i=0; //读入文件,创建单词的二叉搜索树----------------------- fp=fopen("ab.txt","r"); if(fp==NULL) { fprintf(stdout,"can't open the file. "); getch(); exit(1); } //初始化树 IniTree(wordtree); //读入文件字符 while((ch=getc(fp))!=EOF) { if((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z')) { newword[i]=ch; i++; } else { if(i==0)
continue; else
{ newword[i]='