C语言用二叉树实现对输入各个单词的个数进行计数

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

#define MAXWORD  100

struct tnode { /*the tree node*/
	char *word;/* points to the text */
	int count; /* number of the occurrences */
	struct tnode *left; /* left child */
	struct tnode *right; /* right child */

};

struct tnode *addtree(struct tnode *, char *);
void treeprint(struct tnode *);
int getword(char *, int);

/************************************************************************/
/* word frequency count
/************************************************************************/
main(){
	struct tnode *root;
	char word[MAXWORD];

	root = NULL;
	while (getword(word, MAXWORD) != EOF) {
		if(isalpha(word[0])){
			root = addtree(root, word);
		}
	}
	treeprint(root);
	return 0;
}

/************************************************************************/
/* getword : get next word or character from input
/************************************************************************/
int getword(char *word, int lim){
	int c, getch(void);
	void ungetch(int);
	char *w = word;
	
	while (isspace(c = getchar()) ) {
		//跳过空格
	}

	if(c != EOF){
		*w++ = c;
	}
	if (!isalpha(c)) {
		*w = '\0';
		return c;
	}
	for (; --lim > 0; w ++) {
		if (!isalnum(*w = getchar())) {
			ungetch(*w);
			break;
		}
	}
	*w = '\0';
	return word[0];
}

char buf[BUFSIZ];// buffer for ungetch   
int bufp = 0; // next free position in buf   
  
int getch(void){  
  
    return (bufp > 0) ? buf[--bufp] : getchar();  
  
}  
  
void ungetch(int c){// push character back on input    
  
    if (bufp >= BUFSIZ) {  
        printf("ungetch : too many characters\n");  
    }  
    else{  
        buf[bufp ++] = c;  
    }  
  
}

struct tnode *talloc(void);
char *strdup(char *);

/************************************************************************/
/* addtree : add a node with w, at or below
/************************************************************************/
struct tnode *addtree(struct tnode *p, char *w){
	int cond;

	if(p == NULL){ /* a new word has arrived*/
		p = talloc(); /* make a new node */
		p->word = strdup(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 if (cond > 0) {
		p->right = addtree(p->right, w);
	}

	return p;
};

/************************************************************************/
/* treeprint : in-order print of tree p
/************************************************************************/
void treeprint(struct tnode *p){
	if ( p != NULL) {
		treeprint(p->left);
		printf("%4d %s\n", p->count, p->word);
		treeprint(p->right);
	}
}

/************************************************************************/
/* talloc : make a tnode 
/************************************************************************/
struct tnode *talloc(void){
	return (struct tnode *)malloc(sizeof(struct tnode));
}

/************************************************************************/
/* 
/************************************************************************/
char *strdup(char *s){
	char *p;

	p = (char *)malloc(strlen(s) + 1); /* +1 for '\0' */
	if (p != NULL) {
		strcpy(p, s);
	}
	return p;
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值