日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

C Primer+Plus(十七)高级数据表示 编程练习(二)

發布時間:2023/11/29 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C Primer+Plus(十七)高级数据表示 编程练习(二) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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;elsepnode=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.\n",root->word,root->times);PrintTree(root->right);} }//輸入單詞,顯示次數 void showtimes(const Tree *ptree) {Node *pnode;char word[15];printf("input the word.\n");gets(word);pnode=FindWord(word,ptree->root);if(pnode==NULL)printf("the word:%s is not in the tree.\n",word);elseprintf("the word:%s times in tree is:%d.\n",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;elsepnode=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.\n",root->word,root->times);PrintTree(root->right);} }//輸入單詞,顯示次數 void showtimes(const Tree *ptree) {Node *pnode;char word[15];printf("input the word.\n");gets(word);pnode=FindWord(word,ptree->root);if(pnode==NULL)printf("the word:%s is not in the tree.\n",word);elseprintf("the word:%s times in tree is:%d.\n",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.\n");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]='\0';AddWord(newword,wordtree); //加入單詞樹i=0;}}} //針對文本最后字符為字母,則還未設置字符串結束符就跳出while循環了 //增加本段代碼處理if(i!=0){newword[i]='\0';AddWord(newword,wordtree); //加入單詞樹 } //單詞二叉搜索樹創建完成--------------------------------------------------------- printf("-----Please choose--------\n");printf("a:---show all words-------\n");printf("b:---some word------------\n");printf("c:---Quit-----------------\n");while((cc=getch())!='c'){switch(cc){case 'a':PrintTree(wordtree->root);break;case 'b':showtimes(wordtree);break;}printf("-----Please choose--------\n");printf("a:---show all words-------\n");printf("b:---some word------------\n");printf("c:---Quit-----------------\n");}fclose(fp);return 0; }

?

轉載于:https://www.cnblogs.com/tsembrace/p/3194344.html

總結

以上是生活随笔為你收集整理的C Primer+Plus(十七)高级数据表示 编程练习(二)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。