Chinaunix首页 | 论坛 | 博客
  • 博客访问: 68614
  • 博文数量: 17
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 154
  • 用 户 组: 普通用户
  • 注册时间: 2014-05-12 22:56
个人简介

不卑不亢

文章分类

全部博文(17)

文章存档

2016年(1)

2015年(13)

2014年(3)

分类: C/C++

2015-01-30 15:56:25


点击(此处)折叠或打开

  1. /* 程序功能:统计输入中所有单词的出现次数
  2.  * 创建时间:2015-01-30
  3.  * 作者:elijah
  4.  * 思路:
  5.  * 用二叉树构造:
  6.  * 每个不同的单词在树中都是一个节点,每个节点包含:
  7.  * (1)一个指向该单词内容的指针
  8.  * (2)一个统计出现次数的计数值
  9.  * (3)一个指向左子树的指针
  10.  * (4)一个指向右子树的指针
  11.  * 任何节点最多拥有两个子树,也可能只有一个或一个都没有
  12.  * 对节点的所有操作保证,任何节点的左子树只包含按字典序小于
  13.  * 该节点中单词的那些单词,右子树只包含按字典序大于该节点
  14.  * 中单词的那些单词。*/

  15. #include <stdio.h>
  16. #include <ctype.h>
  17. #include <string.h>
  18. #include <stdlib.h>

  19. #define MAXWORD    100
  20. #define BUFFSIZE 100

  21. struct _tnode_ *talloc(void);
  22. struct _tnode_ *addtree(struct _tnode_ *, char *);
  23. char *mystrdup(char *s);
  24. void treeprint(struct _tnode_ *);
  25. int getword(char *, int);
  26. int comment(void);
  27. int getch(void);
  28. void ungetch(int);

  29. struct _tnode_ {
  30.     char *word;
  31.     int count;
  32.     struct _tnode_ *left;
  33.     struct _tnode_ *right;
  34. };

  35. char buff[BUFFSIZE];
  36. int bufp = 0;

  37. /* word frequency count */
  38. int main()
  39. {
  40.     struct _tnode_ *root;
  41.     char word[MAXWORD];

  42.     while (EOF != getword(word, MAXWORD)) {
  43.         if (isalpha(word[0]))
  44.             root = addtree(root, word);
  45.     }
  46.     treeprint(root);

  47.     return 0;
  48. }

  49. int getch(void)
  50. {
  51.     return (bufp > 0)? buff[--bufp] : getchar();
  52. }

  53. void ungetch(int c)
  54. {
  55.     if (bufp >= BUFFSIZE)
  56.         printf("ungetch: too many characters\n");
  57.     else
  58.         buff[bufp++] = c;
  59. }

  60. int getword(char *word, int lim)
  61. {
  62.     int c, d;
  63.     char *w = word;

  64.     while (isspace(c = getch()));

  65.     if (c != EOF)
  66.         *w++ = c;
  67.     if (isalpha(c) || c == '_' || c == '#') {
  68.         for (; --lim > 0; w++) {
  69.             if (!isalnum(*w = getch()) && *w != '_') {
  70.                 ungetch(*w);
  71.                 break;
  72.             }
  73.         }
  74.     } else if (c == '\'' || c == '"') {
  75.         for (; --lim > 0; w++) {
  76.             if ((*w = getch()) == '\\')
  77.                 *++w = getch();
  78.             else if (*w == c) {
  79.                 w++;
  80.                 break;
  81.             } else if (*w == EOF)
  82.                 break;
  83.         }
  84.     } else if (c == '/') {
  85.         if ((d = getch()) == '*')
  86.             c = comment();
  87.         else
  88.             ungetch(d);
  89.     }
  90.     *w = '\0';
  91.     return c;
  92. }

  93. /* comment: skip over comment and return a character */
  94. int comment(void)
  95. {
  96.     int c;
  97.     
  98.     while (EOF != (c = getch())) {
  99.         if (c == '*') {
  100.             if ((c = getch()) == '/')
  101.                 break;
  102.             else
  103.                 ungetch(c);
  104.         }
  105.     }
  106.     return c;
  107. }

  108. char *mystrdup(char *s)
  109. {
  110.     char *p;

  111.     p = (char *)malloc(strlen(s) + 1);        /* +1 for '\0' */
  112.     if (NULL != p)
  113.         strcpy(p, s);

  114.     return p;
  115. }

  116. /* addtree: add a node with w, at or below p */
  117. struct _tnode_ *addtree(struct _tnode_ *p, char *w)
  118. {
  119.     int cond;

  120.     if (NULL == p) {                /* a new word has arrived */
  121.         p = talloc();                /* make a new node */
  122.         p->word = mystrdup(w);    
  123.         p->count = 1;
  124.         p->left = p->right = NULL;
  125.     } else if ((cond = strcmp(w, p->word)) == 0)
  126.         p->count++;
  127.     else if (cond < 0)                /* less than into left subtree */
  128.         p->left = addtree(p->left, w);
  129.     else /* greater than into right subtree */
  130.         p->right = addtree(p->right, w);

  131.     return p;
  132. }

    点击(此处)折叠或打开

    1. /* treeprint: in-order print of tree p */
    2. void treeprint(struct _tnode_ *p)
    3. {
    4.     if (NULL != p) {
    5.         treeprint(p->left);
    6.         printf("%4d %s\n", p->count, p->word);
    7.         treeprint(p->right);
    8.     }
    9. }

    10. /* make a tnode */
    11. struct _tnode_ *talloc(void)
    12. {
    13.     return (struct _tnode_ *)malloc(sizeof(struct _tnode_));
    14. }



阅读(1181) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~