Guest User

Untitled

a guest
Dec 1st, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.83 KB | None | 0 0
  1. Conversation opened. 1 read message.
  2.  
  3. Skip to content
  4. Using Gmail with screen readers
  5. 47 of about 59
  6. Fwd: Slightly modified word_freq program
  7. Inbox
  8. x
  9. Charu Sharma <scharu171193@gmail.com>
  10.  
  11. AttachmentsMon, Oct 9, 2017, 10:22 AM
  12.  
  13. to me
  14.  
  15. ---------- Forwarded message ----------
  16. From: Vineet Chaitanya <vc@iiit.ac.in>
  17. Date: 27 September 2017 at 17:29
  18. Subject: Slightly modified word_freq program
  19. To: shirisha manju <sirimanju@gmail.com>, Charu Sharma <scharu171193@gmail.com>
  20.  
  21.  
  22. Please test it and let me know if there is any problem.
  23.  
  24. Attachments area
  25.  
  26. Thank you!
  27. Looks good to me.
  28. Got it.
  29.  
  30.  
  31.  
  32. /*
  33. * Write a program that prints the distinct words in its input sorted into
  34. * decreasing order of frequency of occurrence. Precede each word by its count.
  35. */
  36.  
  37. /*
  38. * Create a Tree with word and count, just like tnode.
  39. * Parse the tree and create a new tree with count and list of words in the node.
  40. * Print the new tree in-order traversal.
  41. */
  42.  
  43. #include <stdio.h>
  44. #include <stdlib.h>
  45. #include <ctype.h>
  46. #include <string.h>
  47. #include <limits.h>
  48.  
  49. #define MAXWORD 1000 /* longest word that can be read by mgetword */
  50.  
  51. /*
  52. * tnode: Tree node from K&R2 page 140. Words are initially read into
  53. * the tree by getword.
  54. */
  55. struct tnode
  56. {
  57. char *word;
  58. int count;
  59. struct tnode *left;
  60. struct tnode *right;
  61. };
  62.  
  63. struct numwordnode
  64. {
  65. int number;
  66. struct words *wordslist;
  67. struct numwordnode *left;
  68. struct numwordnode *right;
  69. };
  70.  
  71. struct words
  72. {
  73. char *word;
  74. struct words *nextword;
  75. };
  76.  
  77. struct tnode *addtree(struct tnode *, char *);
  78. struct numwordnode *addnumtree(struct numwordnode *, int, char*);
  79. struct words *addwordtolist(struct words*, char *);
  80. void printwords(const struct words*, const int);
  81.  
  82. struct numwordnode *traverse(const struct tnode *, struct numwordnode *);
  83. void treeprint(const struct numwordnode *);
  84. int mgetword(char *, int);
  85.  
  86. int main(int argc, char *argv[])
  87. {
  88. struct tnode *root;
  89. struct numwordnode *numwordroot;
  90.  
  91. char word[MAXWORD];
  92.  
  93. /* get all the words */
  94. root = NULL;
  95. numwordroot = NULL;
  96.  
  97. while(mgetword(word, MAXWORD) != '@')
  98. if(isalpha(word[0]))
  99. root = addtree(root, word);
  100.  
  101. numwordroot = traverse(root, numwordroot);
  102.  
  103. printf("Words and their frequencies:\n");
  104.  
  105. treeprint(numwordroot);
  106.  
  107. return 0;
  108. }
  109.  
  110. struct tnode *talloc(void);
  111. struct numwordnode *numwordalloc(void);
  112. struct words *wordsalloc(void);
  113. char *mstrdup(char *);
  114.  
  115. /* mgetword from Ex6.1 */
  116.  
  117. #define IN 1
  118. #define OUT 0
  119.  
  120.  
  121.  
  122. int mgetword(char *word, int lim)
  123. {
  124. int c, d, getch(void), comment, string, directive;
  125. void ungetch(int);
  126. char *w = word;
  127.  
  128. comment = string = directive = OUT;
  129.  
  130. while (isspace(c = getch()))
  131. ;
  132.  
  133. /* Check if inside a comment */
  134.  
  135. if (c == '/') {
  136. if ((d = getch()) == '*') {
  137. comment = IN;
  138. } else {
  139. comment = OUT;
  140. ungetch(d);
  141. }
  142. }
  143.  
  144. /* Check if inside a quote */
  145.  
  146. if ( c == '\"') {
  147. string = IN;
  148. }
  149.  
  150. /* Check if inside a directive */
  151.  
  152. if (c == '#') {
  153. directive = IN;
  154. }
  155.  
  156. if ( c == '\\') {
  157. c = getch(); /* ignore the \\ character */
  158. }
  159.  
  160. if (comment == OUT && string == OUT && directive == OUT) {
  161.  
  162. if (c != EOF)
  163. *w++ = c;
  164.  
  165. if (!isalnum(c) && c !='_' && c !='-' ) {
  166. *w = '\0';
  167. return c;
  168. }
  169.  
  170. for ( ; --lim > 0; w++) {
  171. *w = getch();
  172. if (!isalnum(*w) && *w != '_' && *w !='-') {
  173. ungetch(*w);
  174. break;
  175. }
  176. }
  177. *w = '\0';
  178. return word[0];
  179. }
  180. else if ( comment == IN) {
  181. *w++ = c;
  182. *w++ = d;
  183.  
  184. while ((*w++ = c = getch())) {
  185. if ( c == '*' ) {
  186. if ( (c = getch()) == '/' ) {
  187. *w++ = c;
  188. comment = OUT;
  189. break;
  190. } else {
  191. ungetch(c);
  192. }
  193. }
  194. }
  195. *w = '\0';
  196.  
  197. }
  198. else if ( string == IN) {
  199. *w++ = c;
  200. while ((*w++ = getch()) != '\"') {
  201. if ( *w == '\\') /* Take care of escaped quotes */
  202. *w++ = getch();
  203. }
  204. string = OUT;
  205. *w = '\0';
  206. }
  207. else if (directive == IN) {
  208. *w++ = c;
  209. while ((*w++ = getch()) != '\n') {
  210. if ( c == '\\') { /* Take care of continuation line escape */
  211. *w++ = getch();
  212. }
  213. }
  214. directive = OUT;
  215. *w = '\0';
  216. }
  217.  
  218. return c;
  219.  
  220. }
  221.  
  222.  
  223.  
  224. /***************************************************************************
  225. * All code below here is from K&R2. *
  226. ***************************************************************************/
  227.  
  228. /*
  229. * addtree: From K&R2 page 141.
  230. * Adds a node containing w, at or below node p.
  231. */
  232. struct tnode *addtree(struct tnode *p, char *w)
  233. {
  234. int cond;
  235.  
  236. if(p == NULL) { /* new word */
  237. p = talloc();
  238. if (p == NULL) {printf ("talloc cannot allocate memory\n"); exit(1);} //VC
  239. p->word = mstrdup(w);
  240. p->count = 1;
  241. p->left = p->right = NULL;
  242. }
  243. else if((cond = strcmp(w, p->word)) == 0)
  244. p->count++;
  245. else if(cond < 0)
  246. p->left = addtree(p->left, w);
  247. else
  248. p->right = addtree(p->right, w);
  249. return p;
  250. }
  251.  
  252. struct numwordnode *addnumtree(struct numwordnode *p, int count, char* w)
  253. {
  254. if (p == NULL) {
  255. p = numwordalloc();
  256. if (p == NULL) {printf ("numwordalloc cannot allocate memory\n"); exit(1);} //VC
  257. p->number = count;
  258. p->wordslist = NULL;
  259. p->wordslist = addwordtolist(p->wordslist, w);
  260. }
  261. else if (count < p->number)
  262. p->left = addnumtree(p->left, count, w);
  263. else
  264. p->right = addnumtree(p->right, count, w);
  265. return p;
  266. }
  267.  
  268. struct words *addwordtolist(struct words* list, char *w)
  269. {
  270. if (list == NULL) {
  271. list = wordsalloc();
  272. if (list == NULL) {printf ("wordsalloc cannot allocate memory\n"); exit(1);} //VC
  273. list->word = mstrdup(w);
  274. list->nextword = NULL;
  275. } else {
  276. list->nextword = addwordtolist(list->nextword, w);
  277. }
  278. return list;
  279. }
  280.  
  281. /* treeprint: From K&R2 page 142. Prints tree p in-order. */
  282. void treeprint(const struct numwordnode *p)
  283. {
  284. if(p != NULL) {
  285. treeprint(p->left);
  286. printwords(p->wordslist, p->number);
  287. treeprint(p->right);
  288. }
  289. }
  290.  
  291. void printwords(const struct words* w, const int count)
  292. {
  293. if (w != NULL) {
  294. printf("%s->%d\n", w->word, count);
  295. w = w->nextword;
  296. }
  297. }
  298.  
  299. struct numwordnode *traverse(const struct tnode *p, struct numwordnode *q)
  300. {
  301.  
  302. if (p != NULL) {
  303. q = traverse(p->left, q);
  304. q = addnumtree(q, p->count, p->word);
  305. q = traverse(p->right, q);
  306. }
  307. return q;
  308. }
  309.  
  310. /* talloc: From K&R2 page 142. Makes a tnode. */
  311. struct tnode *talloc(void)
  312. {
  313. return (struct tnode *) malloc(sizeof(struct tnode));
  314. }
  315.  
  316. struct numwordnode *numwordalloc(void)
  317. {
  318. return (struct numwordnode *) malloc(sizeof(struct numwordnode));
  319. }
  320.  
  321. struct words *wordsalloc(void) {
  322. return (struct words *) malloc(sizeof(struct words));
  323. }
  324.  
  325.  
  326. /* strdup: From K&R2 page 143. Makes a duplicate of s. */
  327. char *mstrdup(char *s)
  328. {
  329. char *p;
  330. p = (char *) malloc(strlen(s) + 1);
  331. if(p != NULL)
  332. strcpy(p, s);
  333. return p;
  334. }
  335.  
  336. /*
  337. * getch and ungetch are from K&R2, page 79
  338. */
  339. #define BUFSIZE 100
  340.  
  341. char buf[BUFSIZE]; /* buffer for ungetch() */
  342. int bufp = 0; /* next free position in buf */
  343.  
  344. int getch(void) { /* get a (possibly pushed back) character */
  345. return (bufp > 0) ? buf[--bufp] : getchar();
  346. }
  347.  
  348. void ungetch(int c) { /* push character back on input */
  349. if(bufp >= BUFSIZE)
  350. printf("ungetch: too many characters\n");
  351. else
  352. buf[bufp++] = c;
  353. return;
  354. }
  355. /*
  356. Run this
  357. Explanation
  358. ab
  359. ab
  360. bc
  361. cd
  362. ef
  363. gh
  364. ab
  365. x
  366. Words and their frequencies:
  367. bc->1
  368. cd->1
  369. ef->1
  370. gh->1
  371. ab->3
  372. */
  373.  
  374. w_f.c
  375. Displaying w_f.c.
Add Comment
Please, Sign In to add comment