the c programming language 上统计单词数目源码

#include<stdio.h>
#include <ctype.h> 
#include <string.h> 
#include <stdlib.h> 
#define MAXWORD 100 
#define BUFSIZE 100 
char buf[BUFSIZE]; /* buffer for ungetch */
int bufp = 0; /* next free position in buf */
struct tnode { /* the tree node: */
    char *word; /* points to the text */
    int count; /* number of occurrences */
    struct tnode *left; /* left child */
    struct tnode *right; /* right child */
};
struct tnode *addtree(struct tnode *, char *);//将读入的单词插入到树中
//主函数 main 以参数的方式传递给该函数的一个单词将作为树
//的最顶层(即树的根)。在每一步中,新单词与节点中存储的单词进行比较,随后,通过递归
//调用 addtree 而转向左子树或右子树。该单词最终将与树中的某节点匹配(这种情况下计数
//值加 1),或遇到一个空指针(表明必须创建一个节点并加入到树中)。若生成了新节点,则
//addtree 返回一个指向新节点的指针,该指针保存在父节点中
void treeprint(struct tnode *);//打印树节点
int getword(char *, int);//读入单词
struct tnode *talloc(void);//用molloc函数为节点分配存储空间
char *strdup(char *);//将读取的新单词复制到新节点中,返回的是一个字符指针
int getword(char *, int);//从输入终端读取一个单词
int getch(void);//读取多个字符
void ungetch(int);//把预读取的字符放入缓存数组
/* word frequency count */
/* addtree: add a node with w, at or below p */
int main()
{
    struct tnode *root;//根节点
    char word[MAXWORD];//存放读入的单词
    root = NULL;
    while (getword(word, MAXWORD) != EOF)
        if (isalpha(word[0]))//判断字符ch是否为英文字母,若为英文字母,返回非0(小写字母为2,大写字母为1)。若不是字母,返回0
            root = addtree(root, word);
    treeprint(root);
    getchar();
    return 0;
}
void treeprint(struct tnode *p)
{
    if (p != NULL) {//前序遍历左中右
        treeprint(p->left);//递归左子树
        printf("%4d %s\n", p->count, p->word);
        treeprint(p->right);//递归右子树
    }
}
/* addtree: add a node with w, at or below p */
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)//比较两个字符串设这两个字符串为str1,str2,若str1=str2,则返回零
        p->count++; /* repeated word */
    else if (cond < 0) /* less than into left subtree */
        p->left = addtree(p->left, w);
    else /* greater than into right subtree */
        p->right =  addtree(p->right, w);
    return p;
}
/* talloc: make a tnode */
struct tnode *talloc(void)
{
    return (struct tnode *) malloc(sizeof(struct tnode));
    //分配存储空间,当树中
}
char *strdup(char *s) /* make a duplicate of s */
{
    char *p;
    p = (char *)malloc(strlen(s) + 1); /* +1 for '\0' */
    if (p != NULL)
        strcpy(p, s);
    return p;
}
/* getword: get next word or character from input */
int getword(char *word, int lim)//word数组用来存放读入的单词,lim是能读取的最大字符
{
    int c, getch(void);
    void ungetch(int);
    char *w = word;
    while (isspace(c = getch()))//检查c是否空格,c是空格则跳过
        ;
    if (c != EOF)
        *w++ = c;
    if (!isalpha(c)) {//如果c不是字母的话
        *w = '\0';
        return c;
    }
    for (; --lim > 0; w++)
        if (!isalnum(*w = getch())) {//判断字符变量c是否为字母或数字,若是则返回非零,否则返回零
            ungetch(*w);
            break;
        }
    *w = '\0';
    return word[0];
}
int getch(void) /* get a (possibly pushed-back) character */
{
    return (bufp > 0) ? buf[--bufp] : getchar();
}
void ungetch(int c) /* push character back on input */
{
    if (bufp >= BUFSIZE)
        printf("ungetch: too many characters\n");
    else
        buf[bufp++] = c;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值