lolamontes69

K+R Exercise6_4

Sep 25th, 2014
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.21 KB | None | 0 0
  1. /* Write a program that prints the distinct word in its input sorted into
  2.  * decreasing order of occurrence. prints each word by its count */
  3.  
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <ctype.h>
  7. #include <string.h>
  8.  
  9. #define BUFSIZE 100
  10. #define MAXWORD 100
  11.  
  12. struct tnode {            /* the tree node:       */
  13.     char *word;           /* points to the text   */
  14.     int count;            /* nunber of occurances */
  15.     struct tnode *left;   /* left child           */
  16.     struct tnode *right;  /* right child          */
  17. };
  18.  
  19. struct tnode *addtree(struct tnode *, char *);
  20. void treeindex(struct tnode *);
  21. int getword(char *, int);
  22.  
  23. int getch(void);
  24. void ungetch(int c);
  25.  
  26. struct tnode *structindex[MAXWORD];
  27. int nstructs = 0;
  28.  
  29. void qsort1(struct tnode *v[], int left, int right);
  30. void swap(struct tnode *v[], int i, int j);
  31. void writelines(void);
  32.  
  33.  
  34. char buf[BUFSIZE];
  35. int bufp = 0;
  36.  
  37. /* word frequency count */
  38. main()
  39. {
  40.     struct tnode *root;
  41.     char word[MAXWORD];
  42.  
  43.     root = NULL; /* pointer to NULL */
  44.     while(getword(word, MAXWORD) != EOF)
  45.         if(isalpha(word[0]))
  46.             root = addtree(root, word);
  47.     treeindex(root);
  48.     qsort1(structindex, 0, nstructs-1);
  49.     writelines();
  50.     return 0;
  51. }
  52.  
  53. struct tnode *talloc(void);
  54. char *strdup1(char *);
  55.  
  56.  
  57. /* addtree: add a node with w, at or below p */
  58. struct tnode *addtree(struct tnode *p, char *w)
  59. {
  60.     int cond;
  61.  
  62.     if(p == NULL) {    /* a new word has arrived */
  63.        p=talloc();     /* make a new node        */
  64.        p->word  = strdup1(w);
  65.        p->count = 1;
  66.        p->left  = p->right = NULL;
  67.     } else if ((cond = strcmp(w, p->word)) == 0)
  68.         p->count++;    /* repeated word */
  69.     else if(cond < 0)  /* less than into left subtree */
  70.         p->left  = addtree(p->left, w);
  71.     else               /* greater than into right subtree */
  72.         p->right = addtree(p->right, w);
  73.     return p;
  74. }
  75.  
  76.  
  77. /* getword: get nextword or character from input */
  78. int getword(char *word, int lim)
  79. {
  80.     int c, getch(void);
  81.     void ungetch(int);
  82.     char *w = word;
  83.  
  84.     while(isspace(c=getch()))
  85.         ;
  86.     if(c!=EOF)
  87.         *w++ = c;
  88.     if(!isalpha(c) && c!='-') {
  89.         *w = '\0';
  90.         return c;
  91.     }
  92.     for( ; --lim > 0; w++)
  93.         if(!isalnum(*w=getch()) && *w!='_' && *w!='"') {   /* This adds to word and breaks on non-alnum*/
  94.             ungetch(*w);              /* so continue with underscores */
  95.             break;
  96.         }
  97.     *w = '\0';
  98.     return word[0];
  99. }
  100.  
  101.  
  102. /* get a (possibly pushed-back) character */
  103. int getch(void)
  104. {
  105.     return (bufp > 0) ? buf[--bufp] : getchar();
  106. }
  107.  
  108.  
  109. /* push character back on input */
  110. void ungetch(int c)
  111. {
  112.     if(bufp >= BUFSIZE)
  113.         printf("ungetch: too many characters\n");
  114.     else
  115.         buf[bufp++] = c;
  116. }
  117.  
  118.  
  119. /* treeindex: make an array of pointers to the tree nodes */
  120. void treeindex(struct tnode *p)
  121. {
  122.     if(p!=NULL) {
  123.         treeindex(p->left);
  124.         structindex[nstructs++] = p;
  125.         treeindex(p->right);
  126.     }
  127. }
  128.  
  129.  
  130. /* talloc: make a tnode */
  131. struct tnode *talloc(void)
  132. {
  133.     return (struct tnode *) malloc(sizeof(struct tnode));
  134. }
  135.  
  136.  
  137. char *strdup1(char *s) /* make a duplicate of s */
  138. {
  139.     char *p;
  140.  
  141.     p=(char *) malloc(strlen(s)+1); /* +1 for '\0' */
  142.     if(p!=NULL)
  143.         strcpy(p, s);
  144.     return p;
  145. }
  146.  
  147.  
  148. /*qsort1: sort v[left]...v[right] into increasing order */
  149. void qsort1(struct tnode *v[], int left, int right)
  150. {
  151.     int i, last;
  152.     void swap(struct tnode *v[], int i, int j);    
  153.  
  154.     if(left>=right)
  155.         return;
  156.     swap(v, left, (left + right)/2);
  157.     last = left;
  158.     for(i = left+1; i <= right; i++) {
  159.         if(v[i]->count < v[left]->count)
  160.             swap(v,++last,i);
  161.     }
  162.     swap(v,left,last);
  163.     qsort1(v, left, last-1);
  164.     qsort1(v, left+1, right);
  165. }
  166.  
  167.  
  168. /* swap: interchange v[i] and v[j] */
  169. void swap(struct tnode *v[], int i, int j)
  170. {
  171.     struct tnode *temp;
  172.  
  173.     temp = v[i];
  174.     v[i] = v[j];
  175.     v[j] = temp;
  176. }
  177.  
  178. /* writelines: write output lines */
  179. void writelines(void)
  180. {
  181.     int i;
  182.     puts("\n---- Sorted words ----\n");
  183.    
  184.     while(nstructs-- > 0)
  185.         printf("%4d  %s\n",structindex[nstructs]->count,structindex[nstructs]->word);
  186. }
Advertisement
Add Comment
Please, Sign In to add comment