#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#define N 20
int my_isspace(char c) {
return (c == ' ' || c == ',' || c == '.' || c == '\n' || c == '\r');
}
int read_word(FILE *fp,char buf[N]){
int i=0;
for(i=0;i<N;i++){
buf[i] = '\0';
}
int c;
for (;;) {
if (c != EOF && my_isspace(c))
continue;
else
break;
}
if (c == EOF)
return 0;
buf[0] = c;
i = 1;
while(((c
= fgetc(fp
)) != EOF
) && i
< N
- 1) { buf[i] = c;
if(my_isspace(c)){
buf[i] = '\0';
break;
}
i++;
}
if (c == EOF) {
buf[i] = '\0';
}
if (i == N - 1) {
buf[N - 1] = '\0';
}
return i;
}
struct word {
char name[N];
int count;
struct word *next;
};
struct word *create_word(char buf[N]) {
struct word *p;
if ((p
= malloc(sizeof(struct word
))) == 0) { printf("memory full, aborted.\n"); }
p->count = 1;
return p;
}
void insertword(struct word **root, char buf[N]) {
struct word *p;
p = create_word(buf);
p->next = *root;
*root = p;
}
void addword(struct word **root, char buf[N]) {
int c;
if (*root == 0) {
insertword(root, buf);
} else {
c
= strcmp((*root
)->name
, buf
); if (c == 0) {
(*root)->count++;
return;
} else if (c > 0) {
insertword(root, buf);
} else {
addword(&((*root)->next), buf);
}
}
}
void release(struct word **root) {
struct word *p;
if (*root == 0)
return;
p = (*root)->next;
(*root)->next = 0;
release(&p);
}
void dump(struct word *p) {
if (p == 0)
return;
printf("%s(%d)\n", p
->name
, p
->count
); dump(p->next);
}
int main() {
char buf[N];
FILE *fp;
fp
= fopen("anne_short.txt","r"); if(fp == NULL){
}
int n;
struct word *root;
root = 0;
for (;;) {
n = read_word(fp, buf);
if (n == 0)
break;
addword(&root, buf);
}
dump(root);
release(&root);
root = 0;
return 0;
}
/* end */
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KI2luY2x1ZGUgPHN0cmluZy5oPgojaW5jbHVkZSA8YXNzZXJ0Lmg+CiAKI2RlZmluZSBOIDIwCiAKaW50IG15X2lzc3BhY2UoY2hhciBjKSB7CiAgcmV0dXJuIChjID09ICcgJyB8fCBjID09ICcsJyB8fCBjID09ICcuJyB8fCBjID09ICdcbicgfHwgYyA9PSAnXHInKTsKfQogCmludCByZWFkX3dvcmQoRklMRSAqZnAsY2hhciBidWZbTl0pewogIGludCBpPTA7CiAgZm9yKGk9MDtpPE47aSsrKXsKICAgIGJ1ZltpXSA9ICdcMCc7CiAgfQogCiAgaW50IGM7CiAgZm9yICg7OykgewogICAgYyA9IGZnZXRjKGZwKTsKICAgIGlmIChjICE9IEVPRiAmJiBteV9pc3NwYWNlKGMpKQogICAgICBjb250aW51ZTsKICAgIGVsc2UKICAgICAgYnJlYWs7CiAgfQoKICBpZiAoYyA9PSBFT0YpCiAgICByZXR1cm4gMDsKCiAgYnVmWzBdID0gYzsKCiAgaSA9IDE7CiAgd2hpbGUoKChjID0gZmdldGMoZnApKSAhPSBFT0YpICYmIGkgPCBOIC0gMSkgewogICAgYnVmW2ldID0gYzsKICAgIGlmKG15X2lzc3BhY2UoYykpewogICAgICBidWZbaV0gPSAnXDAnOwogICAgICBicmVhazsKICAgIH0KICAgIGkrKzsKICB9CiAgaWYgKGMgPT0gRU9GKSB7CiAgICBidWZbaV0gPSAnXDAnOwogIH0KICBpZiAoaSA9PSBOIC0gMSkgewogICAgdW5nZXRjKGMsIGZwKTsKICAgIGJ1ZltOIC0gMV0gPSAnXDAnOwogIH0KICByZXR1cm4gaTsKfQogCnN0cnVjdCB3b3JkIHsKICBjaGFyIG5hbWVbTl07CiAgaW50IGNvdW50OwogIHN0cnVjdCB3b3JkICpuZXh0Owp9OwogCnN0cnVjdCB3b3JkICpjcmVhdGVfd29yZChjaGFyIGJ1ZltOXSkgewogIHN0cnVjdCB3b3JkICpwOwogIGlmICgocCA9IG1hbGxvYyhzaXplb2Yoc3RydWN0IHdvcmQpKSkgPT0gMCkgewogICAgcHJpbnRmKCJtZW1vcnkgZnVsbCwgYWJvcnRlZC5cbiIpOwogICA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