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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/402569.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Mybatis高级-resultMap之collection聚集

From: https://aodeng.cc/archives/mybatisgaoji 简介 聚集元素用来处理“一对多”的关系。需要指定映射的Java实体类的属性&#xff0c;属性的javaType&#xff08;一般为ArrayList&#xff09;&#xff1b;列表中对象的类型ofType&#xff08;Java实体类&#xff09;&#…

Atitit  数据存储的分组聚合 groupby的实现attilax总结

Atitit 数据存储的分组聚合 groupby的实现attilax总结 1. 聚合操作1 1.1. a、标量聚合 流聚合1 1.2. b、哈希聚合2 1.3. 所有的最优计划的选择都是基于现有统计信息来评估3 1.4. 参考资料3 1. 聚合操作 聚合也是我们在写T-SQL语句的时候经常遇到的&#xff0c;我们来分析一下一…

[java理论篇]--java的其他常用API

1、java的正则表达式&#xff1a;常用方法&#xff1a;String matches&#xff08;匹配&#xff09; &#xff1b;String split&#xff08;切割&#xff09;&#xff1b;String replaceAll(替换)&#xff1b;利用正则表达式获取字符创的核心代码&#xff1a;String str“ds…

pojo类无法注入service解决示例

From: https://blog.csdn.net/danielzhou888/article/details/83351913 本示例解决springboot中service无法注入普通jopo的问题。 不啰嗦&#xff0c;直接上代码。 如果该示例帮您解决了问题&#xff0c;请助推哦。 pojo类&#xff1a; package com.scmd.controller;import…

图测试题部分总结.ing

一个无向连通图的生成树是含有该连通图的全部顶点的&#xff08;极小连通子图&#xff09; 在有向图G的拓扑序列中&#xff0c;若顶点Vi在顶点Vj之前&#xff0c;则下列情形不可能出现的是&#xff08;D&#xff09;A&#xff0e;G中有弧<Vi&#xff0c;Vj> B&#xff0e…

mybatis动态sql中的where标签的使用

From: https://blog.csdn.net/wobuaizhi/article/details/81874664 在使用mybatis的动态sql时&#xff0c;有时候遇到根据条件判断添加where后面的筛选条件。 会出现多余的“and”或者“or”&#xff0c;如下&#xff1a; <select id"findBlog" result…

电脑公司 Ghost XP SP3 国庆特别版 v2011.10

电脑公司 Ghost XP SP3 国庆特别版 v2011.10 文件名称:DNGS_GhostXpSp3_v2011.10.iso文件大小:686.59MB &#xff08;719,945,728字节&#xff09;系统格式:NTFSCRC32:DDEBC170MD5:FA264E6241154EE7F80EFED3CFF1C6A2SHA1:FFEB1C13854AA954D9EE384153815FAF52E0747B电脑城系统 …

Linux C 预处理详解

1.预处理程序 按照ANSI标准的定义&#xff0c;预处理程序应该处理以下指令&#xff1a; #if #ifdef #ifndef #else #elif #endif #define #undef #line #error #pragma #include 显然&#xff0c;上述所有的12个预处理指令都以符号#开始&#xff0c;每条预处理指令必须独占一行…

Spring Boot 中使用 @Transactional 注解配置事务管理

From: https://blog.csdn.net/nextyu/article/details/78669997 事务管理是应用系统开发中必不可少的一部分。Spring 为事务管理提供了丰富的功能支持。Spring 事务管理分为编程式和声明式的两种方式。编程式事务指的是通过编码方式实现事务&#xff1b;声明式事务基于 AOP,将…

TCP中间件_Delphi_client

1、界面 1.1、formMain.pas 1.1.1、 object frmMain: TfrmMainLeft 191Top 103Width 542Height 466Caption frmMainColor clBtnFaceFont.Charset DEFAULT_CHARSETFont.Color clWindowTextFont.Height -11Font.Name MS Sans SerifFont.Style []OldCreateOrder False…

python为类定义构造函数

用python进行OO编程时&#xff0c; 经常会用到类的构造函数来初始化一些变量。 class FileData:def __init__(self, data, name, type):self.bits base64.encodestring(data)self.name nameself.type type其中self类似c或者c#的this指针。转载于:https://blog.51cto.com/muz…

Linux C 函数练习

学习函数主要学习的就是函数的声明、定义和调用&#xff0c;下面请看两个例子&#xff0c;来帮助我们学习函数&#xff1a; 题目一&#xff1a; 编写一个函数iswithin()&#xff0c;它接受两个参数&#xff0c;一个是字符&#xff0c;另一个是字符串指针。其功能是如果字符在字…

Java、Mysql、MyBatis 中枚举 enum 的使用

From: https://yulaiz.com/java-mysql-enum/ Java 和 MySql 中都有枚举的概念&#xff0c;合理的使用枚举&#xff0c;可以让代码阅读和数据库数据查询更加直观、高效。那么我们怎么使用呢&#xff0c;什么时候使用&#xff0c;两者之间怎么进行数据关联呢&#xff1f;&#x…

ny12 喷水装置(二)

喷水装置&#xff08;二&#xff09; 时间限制&#xff1a;3000 ms | 内存限制&#xff1a;65535 KB难度&#xff1a;4描述有一块草坪&#xff0c;横向长w,纵向长为h,在它的橫向中心线上不同位置处装有n(n<10000)个点状的喷水装置&#xff0c;每个喷水装置i喷水的效果是让…

http请求头大全

转自http://tools.jb51.net/table/http_header HTTP响应头和请求头信息对照表 HTTP请求头提供了关于请求&#xff0c;响应或者其他的发送实体的信息。HTTP的头信息包括通用头、请求头、响应头和实体头四个部分。每个头域由一个域名&#xff0c;冒号&#xff08;:&#xff09;和…

Linux C 指针练习

题目一、 已知数组内容如下 s[] {1,2,3,4,5,6,7,8,9}&#xff0c;输入一个常数 m(1<m<9)&#xff0c;使得该数组内容顺序后移n个位置。如n 3时&#xff0c;数组后移3个位置后的内容为{7,8,9,1,2,3,4,5,6} 代码如下&#xff1a; [cpp] view plaincopy #include <stdi…

Java重载遇到泛型

今天被问到一个有意思的问题&#xff0c;大家都知道重载的概念吧&#xff1a;一个类中定义同名的方法&#xff0c;参数表不同&#xff08;参数类型&#xff0c;或者参数个数不通&#xff09;&#xff1b; 但是&#xff0c;如果是下面这个两个方法呢 public static int fn(List&…

wordpress插件制作视频教程【资料分享】

2019独角兽企业重金招聘Python工程师标准>>> 一共5集&#xff0c;每一集15分钟左右&#xff0c;适合入门用哦~ 资料地址&#xff1a; http://wordpresshy.com/create-plugin 分集介绍&#xff1a; 1 【教学大纲】 1.介绍什么是插件&#xff1b; 2.插件的文件结…

Java中遍历HashMap的5种方式

From: https://blog.csdn.net/w605283073/article/details/80708943 本教程将为你展示Java中HashMap的几种典型遍历方式。 如果你使用Java8&#xff0c;由于该版本JDK支持lambda表达式&#xff0c;可以采用第5种方式来遍历。 如果你想使用泛型&#xff0c;可以参考方法3。如…

字符串按照单词为单位逆序排列

我们前面已经写过一个简单字符串逆序排序的方法&#xff0c;这里再开一个字符串排序问题&#xff1a; 给定一个字符串“I love China”&#xff0c;编写程序完成以单词为单位的逆序&#xff0c;如"China love I",并要求不使用第三方变量保存数据&#xff0c;但可以使…