From ff0cc8894e8c4c78e03cc4508763f6df8bb43836 Mon Sep 17 00:00:00 2001 From: Christian Giese Date: Thu, 28 Nov 2024 16:14:14 +0000 Subject: [PATCH] fix code format and indentation --- anagram.c | 89 ++++++++++++++++++++++++++++--------------------------- 1 file changed, 45 insertions(+), 44 deletions(-) diff --git a/anagram.c b/anagram.c index b772edb..aec68ff 100644 --- a/anagram.c +++ b/anagram.c @@ -19,77 +19,78 @@ int main(int argc, char *argv[]) { if (argc != 2) { - printf("Expected filename argument.\n"); - exit(1); + printf("Expected filename argument.\n"); + exit(1); } FILE *fp = fopen(argv[1], "r"); if (!fp) { - printf("Unable to open file '%s'.\n", argv[1]); - exit(1); + printf("Unable to open file '%s'.\n", argv[1]); + exit(1); } rb_tree *tree = rb_tree_new(dict_str_cmp); char buf[512]; while (fgets(buf, sizeof(buf), fp)) { - if (isupper(buf[0])) /* Disregard proper nouns. */ - continue; + if (isupper(buf[0])) /* Disregard proper nouns. */ + continue; - strtok(buf, "\r\n"); - int freq[256] = { 0 }; - memset(freq, 0, sizeof(freq)); + strtok(buf, "\r\n"); + int freq[256] = { 0 }; + memset(freq, 0, sizeof(freq)); - ASSERT(buf[0] != '\0'); + ASSERT(buf[0] != '\0'); - for (char *p = buf; *p; p++) - freq[tolower(*p)]++; + for (char *p = buf; *p; p++) + freq[tolower(*p)]++; - char name[1024]; - char *p = name; - for (int i=1; i<256; i++) { - if (freq[i]) { - ASSERT(freq[i] < 10); + char name[1024]; + char *p = name; + for (int i=1; i<256; i++) { + if (freq[i]) { + ASSERT(freq[i] < 10); - *p++ = (char) i; - *p++ = '0' + (char) freq[i]; + *p++ = (char) i; + *p++ = '0' + (char) freq[i]; + } } - } - *p = 0; + *p = 0; - WordList* word = xmalloc(sizeof(*word)); - word->word = xstrdup(buf); - WordList** wordp = (WordList**) rb_tree_insert(tree, xstrdup(name)).datum_ptr; - word->next = *wordp; - *wordp = word; + WordList* word = xmalloc(sizeof(*word)); + word->word = xstrdup(buf); + WordList** wordp = (WordList**) rb_tree_insert(tree, xstrdup(name)).datum_ptr; + word->next = *wordp; + *wordp = word; } rb_itor *itor = rb_itor_new(tree); for (rb_itor_first(itor); rb_itor_valid(itor); rb_itor_next(itor)) { - WordList *word = *rb_itor_datum(itor); - ASSERT(word != NULL); - if (word->next) { - int count = 1; - for (; word->next; count++) - word = word->next; - printf("%2d:[", count); + WordList *word = *rb_itor_datum(itor); + ASSERT(word != NULL); + if (word->next) { + int count = 1; + for (; word->next; count++) + word = word->next; + printf("%2d:[", count); + word = *rb_itor_datum(itor); + while (word) { + printf("%s%c", word->word, word->next ? ',' : ']'); + word = word->next; + } + printf("\n"); + } word = *rb_itor_datum(itor); while (word) { - printf("%s%c", word->word, word->next ? ',' : ']'); - word = word->next; + WordList *next = word->next; + free(word->word); + free(word); + word = next; } - printf("\n"); - } - word = *rb_itor_datum(itor); - while (word) { - WordList *next = word->next; - free(word->word); - free(word); - word = next; - } } while (rb_itor_next(itor)); rb_itor_free(itor); rb_tree_free(tree, NULL); + fclose(fp); return 0; }