日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) >

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

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

7、編寫一個(gè)程序,能打開(kāi)、讀入一個(gè)文本文件并統(tǒng)計(jì)文件中每個(gè)單詞出現(xiàn)的次數(shù)。用改進(jìn)的二叉搜索樹(shù)存儲(chǔ)單詞及其出現(xiàn)的次數(shù)。程序讀入文件后,會(huì)提供一個(gè)有三個(gè)選項(xiàng)的菜單。第一個(gè)選項(xiàng)為列出所有單詞連同其出現(xiàn)的次數(shù)。第二個(gè)選項(xiàng)為讓您輸入一個(gè)單詞,程序報(bào)告該單詞在文件中出現(xiàn)的次數(shù)。第三個(gè)選項(xiàng)為退出。

斷斷續(xù)續(xù)編寫代碼、測(cè)試,花了差不多兩天時(shí)間,總算告成。

*建立該問(wèn)題的二叉樹(shù)節(jié)點(diǎn)模型

//BST Model--START------------------ typedef struct node {char word[15];int times; //出現(xiàn)的次數(shù)struct node *left;struct node *right; }Node;typedef struct tree {Node *root;int num; //樹(shù)中單詞數(shù) }Tree; //BST Model--FINISH--------------------

*研究本題需求,實(shí)現(xiàn)接口代碼

//BST function START----------------------------- //初始化樹(shù) void IniTree(Tree *ptree) {ptree->root=NULL;ptree->num=0; }//查找樹(shù)中有無(wú)指定單詞 //若未找到,返回NULL;若找到返回指定項(xiàng)目所在節(jié)點(diǎn)地址 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; }//添加單詞節(jié)點(diǎn) //通過(guò)FindWord做支撐,若存在,則相應(yīng)節(jié)點(diǎn)次數(shù)+1 //若不存在,則找到合適的空位,同時(shí)定位該空位的父節(jié)點(diǎn) //創(chuàng)建該單詞的節(jié)點(diǎn),并判斷在空位父節(jié)點(diǎn)的左還是右,然后添加 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) //找空位,并且定位空位的父節(jié)點(diǎn) {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); //設(shè)置節(jié)點(diǎn)信息if(ptree->root==NULL) //如果樹(shù)為空 {ptree->root=newnode;ptree->num++; }else{if(strcmp(word,fathernode->word)<0) //說(shuō)明父節(jié)點(diǎn)左子節(jié)點(diǎn)為空 { fathernode->left=newnode;ptree->num++;}else{ fathernode->right=newnode;ptree->num++;}}} }//打印樹(shù)中單詞信息 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);} }//輸入單詞,顯示次數(shù) 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----------------------------- //初始化樹(shù) void IniTree(Tree *ptree) {ptree->root=NULL;ptree->num=0; }//查找樹(shù)中有無(wú)指定單詞 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; }//添加單詞節(jié)點(diǎn) 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++;}}} }//打印樹(shù)中單詞信息 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);} }//輸入單詞,顯示次數(shù) 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; //讀入文件,創(chuàng)建單詞的二叉搜索樹(shù)-----------------------fp=fopen("ab.txt","r");if(fp==NULL){fprintf(stdout,"can't open the file.\n");getch();exit(1);} //初始化樹(shù) 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); //加入單詞樹(shù)i=0;}}} //針對(duì)文本最后字符為字母,則還未設(shè)置字符串結(jié)束符就跳出while循環(huán)了 //增加本段代碼處理if(i!=0){newword[i]='\0';AddWord(newword,wordtree); //加入單詞樹(shù) } //單詞二叉搜索樹(shù)創(chuàng)建完成--------------------------------------------------------- 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; }

?

轉(zhuǎn)載于:https://www.cnblogs.com/tsembrace/p/3194344.html

總結(jié)

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

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。