Skip to content

Commit

Permalink
fix code format and indentation
Browse files Browse the repository at this point in the history
  • Loading branch information
GIC-de committed Nov 28, 2024
1 parent 568489e commit ff0cc88
Showing 1 changed file with 45 additions and 44 deletions.
89 changes: 45 additions & 44 deletions anagram.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit ff0cc88

Please sign in to comment.