//Sun Feb 27 21:49:10 EST 2005 //Sun Feb 27 22:23:34 EST 2005 #include #include #include struct word { char str[30]; int count; struct word *left; struct word *right; }; struct word *ptwords[100000]; struct word words[100000]; int wordsum=0; main() { FILE *f; int i=0; int j=0; char key; char tmpword[100]; struct word *tmp; if((f=fopen("bbe.txt","r"))==NULL) { puts("unable to open file"); return 0; } while(!feof(f)) { key=fgetc(f); if (isalpha(key)) { tmpword[i]=tolower(key); i++; } else if(strlen(tmpword)) { tmpword[i]=0; i=0; if(!ptwords[0]) { strcpy(words[wordsum].str,tmpword); words[wordsum].count=1; words[wordsum].left=0; words[wordsum].right=0; ptwords[wordsum]=&words[wordsum]; wordsum++; } else searchaddword(tmpword,ptwords[0]); } } for (i=0;icount>ptwords[j]->count) { tmp=ptwords[i]; ptwords[i]=ptwords[j]; ptwords[j]=tmp; } } fclose(f); for (i=0;istr,ptwords[i]->count); } int searchaddword(char *str, struct word *p) { if(!strcmp(str,p->str)) { p->count++; return 0; } if(strcmp(str,p->str)<0) if(!(p->left)) { strcpy(words[wordsum].str,str); words[wordsum].count=1; words[wordsum].left=0; words[wordsum].right=0; ptwords[wordsum]=&words[wordsum]; p->left=&words[wordsum]; wordsum++; return 0; } else searchaddword(str,p->left); if(strcmp(str,p->str)>0) if(!(p->right)) { strcpy(words[wordsum].str,str); words[wordsum].count=1; words[wordsum].left=0; words[wordsum].right=0; ptwords[wordsum]=&words[wordsum]; p->right=&words[wordsum]; wordsum++; return 0; } else searchaddword(str,p->right); }