您的位置:首页 > 理论基础 > 计算机网络

读入一个c程序,并按字母表顺序分组打印变量名,每组前N个字符相同(TCPL 练习6-2)

2013-09-17 16:49 573 查看
在建立结构tnode的过程中,我们没有预设门槛。这道题目就设置了门槛,必须根据前N个字符来进行分组,于是排除了长度小于N的变量,以便减轻负担。

因为要求对变量名分组打印,组别理所应当地应该按照至少是升序来排列,在每一组内,也至少应该是升序排列(以字母顺序),于是构造一棵二叉树,它的每一个节点都指向另一棵二叉树,即一棵由二叉树构成的二叉树。这样,在我们对每一个母二叉树的节点进行访问的时候,也可以遍历它所指向的子二叉树,也就是每一个组别内的升序排列实现了。遍历母二叉树的过程也实现了各组升序排列。

代码如下:

#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<stdlib.h>

#define MAXWORD 100

extern int getword(char *word, int lim);
struct tnode *addtree(struct tnode *, char *);        /* 二叉树tnode,每个节点存放一个word以及其出现的次数 */
struct maketree *mtree(struct maketree *, char *);/*  二叉树maketree,每个节点指向一棵二叉树tnode */
void trreprint(struct tnode *);            /*  打印二叉树tnode */
void actreeprint(struct maketree *);    /*  打印二叉树maketree */
struct tnode *talloc(void);            /*  为树tnode申请储存空间 */
struct maketree *atalloc(void);        /*  为树maketree申请储存空间 */
char *strduper(char *s);            /*  word存放在一个安全的地方 */
int num;
struct tnode {
char *word;
int count;
struct tnode *left;
struct tnode *right;
};

struct maketree {
int count;                /*    每个节点出现的次数 */
struct tnode *nroot;    /*    指向一棵tnode二叉树 */
struct maketree *left;    /*    左子树 */
struct maketree *right;/*    右子树 */
};
/*  编写程序,按字母顺序打印文本中的变量名,每一组内各变量前6个字符相同,其余字符不同,不考虑注释、字符常量等,6可在命令行中更改 */
main(int argc, char *argv[]) {
struct maketree *root;
char word[MAXWORD];

root=NULL;
num=(--argc>0&&(**(++argv)=='-'))?atoi(++(*argv)):6;
while(getword(word,MAXWORD)!=EOF &&(sizeof word/sizeof(char)) >=num)
if(isalpha(word[0])||word[0]=='_')
root=mtree(root,word);
actreeprint(root);
return 0;
}
struct tnode *addtree(struct tnode *p, char *w) {
int cond;

if(p==NULL) {
p=talloc();
p->word=strduper(w);
p->count=1;
p->left=p->right=NULL;

} else if ((cond= strcmp(w,p->word))==0)
p->count++;
else if (cond <0)
p->left=addtree(p->left,w);
else
p->right=addtree(p->right,w);
return p;
}

struct maketree *mtree(struct maketree *aroot, char *w) {

int cond;

if(aroot==NULL) {
aroot=atalloc();
aroot->nroot=NULL;
aroot->nroot=addtree(aroot->nroot,w);            /*  将word传递给二叉树tnode */
aroot->left=aroot->right=NULL;
aroot->count=1;
} else if ((cond=strncmp(w,aroot->nroot->word,num))==0) {
addtree(aroot->nroot,w);
aroot->count++;
} else if (cond <0)
aroot->left=mtree(aroot->left,w);
else
aroot->right=mtree(aroot->right,w);
return aroot;

}

struct tnode *talloc(void) {
return (struct tnode *) malloc(sizeof(struct tnode));

}
struct maketree *atalloc(void) {
return (struct maketree *) malloc(sizeof(struct maketree));
}
char *strduper(char *s) {
char *p;

p=(char *)malloc(strlen(s)+1);
if(p!=NULL)
strcpy(p,s);
return p;
}

void treeprint(struct tnode *p) {
if(p!=NULL) {
treeprint(p->left);
printf("%4d %s\n", p->count, p->word);
treeprint(p->right);
}
}
void actreeprint(struct maketree *p) {
if(p!=NULL)
{
actreeprint(p->left);
treeprint(p->nroot);
printf("\n");
actreeprint(p->right);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐