1
0
mirror of https://github.com/cc65/cc65.git synced 2024-11-15 11:05:56 +00:00
cc65/test/ref/wf1.c

120 lines
2.9 KiB
C
Raw Normal View History

2014-09-24 14:45:10 +00:00
/*
!!DESCRIPTION!! print word frequencies; uses structures
!!ORIGIN!! LCC 4.1 Testsuite
!!LICENCE!! own, freely distributeable for non-profit. read CPYRIGHT.LCC
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define MAXWORDS 250
FILE *in;
#define getchar() fgetc(in)
2014-09-24 14:45:10 +00:00
struct node
{
2019-02-12 21:50:49 +00:00
int count; /* frequency count */
struct node *left; /* left subtree */
struct node *right; /* right subtree */
char *word; /* word itself */
2014-09-24 14:45:10 +00:00
} words[MAXWORDS];
2019-02-12 21:50:49 +00:00
int next; /* index of next free entry in words */
2014-09-24 14:45:10 +00:00
/*struct node *lookup();*/
int err(char *s);
int getword(char *buf);
void tprint(struct node *tree);
struct node *lookup(char *word, struct node **p);
int isletter(char c);
2019-02-12 21:50:49 +00:00
/* err - print error message s and die */
2014-09-24 14:45:10 +00:00
err(s) char *s; {
2019-02-12 21:50:49 +00:00
printf("? %s\n", s);
exit(EXIT_FAILURE);
2014-09-24 14:45:10 +00:00
}
/* getword - get next input word into buf, return 0 on EOF */
int getword(buf) char *buf;
{
2019-02-12 21:50:49 +00:00
char *s;
int c;
2014-09-24 14:45:10 +00:00
while (((c = getchar()) != -1) && (isletter(c) == 0))
2019-02-12 21:50:49 +00:00
;
2014-09-24 14:45:10 +00:00
for (s = buf; (c = isletter(c)); c = getchar())
2019-02-12 21:50:49 +00:00
*s++ = c;
*s = 0;
if (s > buf)
return 1;
return 0;
2014-09-24 14:45:10 +00:00
}
/* isletter - return folded version of c if it is a letter, 0 otherwise */
int isletter(char c)
{
2019-02-12 21:50:49 +00:00
if ((c >= 'A') && (c <= 'Z')) c += 'a' - 'A';
if ((c >= 'a') && (c <= 'z')) return c;
return 0;
2014-09-24 14:45:10 +00:00
}
/* lookup - lookup word in tree; install if necessary */
struct node *lookup(word, p)
char *word; struct node **p;
{
2019-02-12 21:50:49 +00:00
int cond;
/* char *malloc(); */
if (*p) {
cond = strcmp(word, (*p)->word);
if (cond < 0)
return lookup(word, &(*p)->left);
else if (cond > 0)
return lookup(word, &(*p)->right);
else
return *p;
}
if (next >= MAXWORDS)
err("out of node storage");
words[next].count = 0;
words[next].left = words[next].right = 0;
words[next].word = malloc(strlen(word) + 1);
if (words[next].word == 0)
err("out of word storage");
strcpy(words[next].word, word);
return *p = &words[next++];
2014-09-24 14:45:10 +00:00
}
/* tprint - print tree */
void tprint(tree) struct node *tree; {
2019-02-12 21:50:49 +00:00
if (tree) {
tprint(tree->left);
printf("%d:%s\n", tree->count, tree->word);
tprint(tree->right);
}
2014-09-24 14:45:10 +00:00
}
int main(void)
{
2019-02-12 21:50:49 +00:00
struct node *root;
char word[20];
2014-09-24 14:45:10 +00:00
in = fopen("wf1.in","rb");
if (in == NULL) {
return EXIT_FAILURE;
}
2019-02-12 21:50:49 +00:00
root = 0;
next = 0;
while (getword(word))
lookup(word, &root)->count++;
tprint(root);
2014-09-24 14:45:10 +00:00
fclose(in);
2014-09-24 14:45:10 +00:00
return 0;
}