aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorzlg <zlg@zlg.space>2015-11-07 02:41:42 -0800
committerzlg <zlg@zlg.space>2015-11-07 02:41:42 -0800
commit87761a45eb70cd22cf0b64fdcc0545006e92a8f9 (patch)
treea52e991021cdbee938d40e2446ee1177b6873b28
parentSolve Exercise 6-03: Word cross-referencing (diff)
downloadknr-87761a45eb70cd22cf0b64fdcc0545006e92a8f9.tar.gz
knr-87761a45eb70cd22cf0b64fdcc0545006e92a8f9.tar.bz2
knr-87761a45eb70cd22cf0b64fdcc0545006e92a8f9.tar.xz
knr-87761a45eb70cd22cf0b64fdcc0545006e92a8f9.zip
Solve Exercise 6-4: Highest Word Frequency
This exercise was good practice to get back into the mindset of programming. It's not a pretty solution, but it works. I may end up coming back to this solution, because I feel like I've missed something that would make this exercise simpler.
-rw-r--r--ch6/6-04_top-words.c177
1 files changed, 177 insertions, 0 deletions
diff --git a/ch6/6-04_top-words.c b/ch6/6-04_top-words.c
new file mode 100644
index 0000000..796dac1
--- /dev/null
+++ b/ch6/6-04_top-words.c
@@ -0,0 +1,177 @@
+#include <stdio.h>
+#include <string.h>
+#include <ctype.h>
+#include <stdlib.h>
+
+/* The C Programming Language: 2nd Edition
+ *
+ * Exercise 6-4: Write a program that prints the distinct words in its input
+ * sorted into decreasing order of frequency of occurrence. Precede each word
+ * by its count.
+ *
+ * Notes: The approach I took builds a separate sorted tree instead of
+ * modifying the tree in-place, like a lot of sorting functions tend to do.
+ * This meant doubling the amount of RAM needed to solve the exercise. There
+ * is probably a more elegant way to do this -- even within Category-0
+ * standards -- but I couldn't seem to figure it out. I'm disappointed that
+ * freq_sort and addsorted couldn't seem to be combined.
+ */
+
+#define BUFSIZE 1000
+#define MAXWORD 100
+#define DEF_PRE_LEN 4
+
+struct tnode {
+ char *word;
+ int count;
+ struct tnode *left;
+ struct tnode *right;
+};
+
+char buf[BUFSIZE];
+int bufp = 0;
+int minlen;
+
+int getch(void);
+void ungetch(int);
+int getword(char *, int);
+struct tnode *addtree(struct tnode *, char *);
+struct tnode *freq_sort(struct tnode *, struct tnode *);
+struct tnode *addsorted(struct tnode *, struct tnode *);
+void treeprint(struct tnode *);
+struct tnode *talloc(void);
+char *mystrdup(char *);
+
+int main(int argc, char *argv[]) {
+ switch (argc) {
+ case 1:
+ minlen = DEF_PRE_LEN;
+ break;
+ case 2:
+ minlen = strtol(argv[1], NULL, 10);
+ break;
+ default:
+ printf("Too many arguments.\n");
+ return 1;
+ }
+ struct tnode *root;
+ struct tnode *sorted;
+ char word[MAXWORD];
+ root = NULL;
+ sorted = NULL;
+ while (getword(word, MAXWORD) != EOF) {
+ /* Only add the words we need to the tree */
+ if (isalpha(word[0]) && strlen(word) > minlen) {
+ root = addtree(root, word);
+ }
+ }
+ /* Sort them in order of occurrence */
+ if (root == NULL) {
+ printf("No words at that length or greater were found.\n");
+ return 1;
+ } else {
+ sorted = freq_sort(root, sorted);
+ treeprint(sorted);
+ return 0;
+ }
+}
+
+/* Iterate over the "tree" and create a new, sorted tree */
+struct tnode *freq_sort(struct tnode *p, struct tnode *s) {
+ for (; p != NULL; p = p->right) {
+ s = addsorted(p, s);
+ }
+ return s;
+}
+
+/* Add entries to the new tree, ordered by word frequency. I feel
+ * that this function and freq_sort() could somehow be consolidated
+ * and that this could be done better. Patches or PRs welcome.
+ */
+struct tnode *addsorted(struct tnode *p, struct tnode *s) {
+ if (s != NULL) {
+ if (p->count > s->count) {
+ s->left = addsorted(p, s->left);
+ } else {
+ s->right = addsorted(p, s->right);
+ }
+ } else {
+ s = talloc();
+ s->word = mystrdup(p->word);
+ s->count = p->count;
+ }
+ return s;
+}
+
+int getword(char *word, int lim) {
+ int c;
+ char *w = word;
+ while (isspace(c = getch())) {
+ }
+ if (c == EOF) {
+ return EOF;
+ }
+ if (c != EOF) {
+ *w++ = c;
+ }
+ for ( ; --lim > 0; w++) {
+ if (!isalpha(*w = getch())) {
+ ungetch(*w);
+ break;
+ }
+ }
+ *w = '\0';
+ return word[0];
+}
+
+int getch(void) {
+ return (bufp > 0) ? buf[--bufp] : getchar();
+}
+
+void ungetch(int c) {
+ if (bufp >= BUFSIZE) {
+ printf("ungetch: Too many characters.\n");
+ } else {
+ buf[bufp++] = c;
+ }
+}
+
+struct tnode *addtree(struct tnode *p, char *w) {
+ if (p != NULL) {
+ /* We're putting all new things to the right side to dumb our tree struct
+ * down to a list. It's technically one address larger than a linked list,
+ * but there's no harm in re-purposing a structure.
+ */
+ if (strcasecmp(p->word, w) == 0) {
+ p->count++;
+ } else {
+ p->right = addtree(p->right, w);
+ }
+ } else {
+ p = talloc();
+ p->word = mystrdup(w);
+ p->count = 1;
+ }
+ return p;
+}
+
+void treeprint(struct tnode *p) {
+ if (p != NULL) {
+ treeprint(p->left);
+ printf("%4d %-20s\n", p->count, p->word);
+ treeprint(p->right);
+ }
+}
+
+struct tnode *talloc(void) {
+ return (struct tnode *) malloc(sizeof(struct tnode));
+}
+
+char *mystrdup(char *s) {
+ char *p;
+ p = (char *) malloc(strlen(s)+1);
+ if (p != NULL) {
+ strcpy(p , s);
+ }
+ return p;
+}
mp;follow=1'>Remove ID field from DBzlg3-38/+46 The sqlite database already uses a game's title and system as the primary keys. Row IDs are redundant. 2018-10-06cli: change "Status" heading to "Progress"zlg2-36/+40 2018-09-29Bump to 0.3alpha5 for PyPIzlg1-1/+1 2018-09-29cli: Add pretty printing to 'list' commandzlg3-17/+107 Also add the "--width" option to specify the maximum width of the table. 2018-09-08setup.py: Bump to alpha4 for PyPIzlg1-1/+1 2018-09-08cli: add '--raw' option to list commandzlg2-9/+45 Add '--raw' option to the list command, in addition to proper note expansion. Newline characters in notes are escaped to be friendly to scripting. This option may be shortened to '-r' at the user's convenience. In raw output mode, the information is formatted in plain pipe-delimited strings, one line per row: title|system|ownership|progress|notes ownership and progress are printed in their numeric form, consistent with the OWNERSHIP and PROGRESS dictionaries in the vgstash package. An empty notes field will result in a line ending with a pipe and no whitespace following it. 2018-09-08Add remaining filters to vgstash packagezlg1-2/+11 2018-09-04Update LICENSE to match setup.pyzlg1-80/+67 Whoops. 2018-09-03Branch off from master with pytest, tox, clickzlg16-778/+779 This commit is huge, but contains everything needed for a "proper" build system built on pytest + tox and a CLI built with click. For now, this branch will contain all new vgstash development activity until it reaches feature parity with master. The CLI is installed to pip's PATH. Only the 'init', 'add', and 'list' commands work, with only two filters. This is pre-alpha software, and is therefore not stable yet. 2018-03-18Flesh out filter types and ownership statuszlg3-82/+144 It's time for a refactor to a module; the functionality and interface are clashing. 2018-03-18README.mdown: break line correctlyzlg1-1/+1 2018-03-18add 'playlog' list filterzlg2-2/+9 This filter is used to get an idea of which games you're currently playing through, so you can prioritize games to play when you're bored and detect it when you've beaten a game but haven't marked it as such. 2018-03-13Update helpers a bitzlg1-2/+9 At present, user modification is needed to make these seamless. vgup() may need to be axed in favor of telling the user to make an alias. 2018-03-13Make VGSTASH_DB_LOCATION point to a filezlg2-21/+20 It used to point to a directory, which would then look for .vgstash.db. This behavior was kind of backwards and I don't remember why I did it that way. This change gives users more control over where they put their DB. Be sure to update your environment variable if you have it set! 2016-11-18Remove settings from helpers.shZe Libertine Gamer1-5/+0 Sourcing them in .bash_profile screws up login if they're set. 2016-11-15Correct phrasing in README.Ze Libertine Gamer1-4/+4 2016-11-13DerpZe Libertine Gamer1-0/+1 2016-11-03Improve error handling in shell scriptsZe Libertine Gamer4-3/+23 2016-10-24Correct run_again, add recursionZe Libertine Gamer1-0/+4 Loops and functions -- oh my, what a useful combination. :) 2016-10-21Add quotes to correct behavior for arglistZe Libertine Gamer1-1/+1 2016-10-14updater.sh: add recursion, error handlingZe Libertine Gamer1-43/+101 2016-10-14Correct pipe-handling behaviorZe Libertine Gamer1-1/+9 2016-10-12Clarify a method to move between platformsZe Libertine Gamer1-2/+5 Also correct a typo.