/* AUTHOR: John R. Abercrombie DATE: January, 1993 Program produces frequency list of all words in a text. Program has been tested successfully on ccat with texts as large as 1000 pages. Program accepts the following parameters: -a = Latin alphabetic order -z = Reverse Latin alphabetic order -U = force uppercase -l = force lowercase -p = remove punctuation */ #include /* Defines the node in memory */ struct tnode { char listword[40]; int count; struct tnode *left; struct tnode *right; }; /*Global variables*/ char word[40]; char parameters[40]; /*Parameter list*/ int alphabeticorder=0; /*switcher for alphabetic order*/ int cases=0; /*switcher for upper or lower case*/ int punctuation=0; /*switcher for punctuation*/ int StartPoint=32; /*Starting point for ASCII print set*/ int StopPoint=127; /*End point for ASCII print set*/ main(argc,argv) /* Taking arguments from the command line */ int argc; char *argv[]; { struct tnode *root, *tree(); /* Define node */ FILE *fp; int c, x, y=0; int letter; parameters[0] = '\0'; /*Decipher command line*/ if (argc < 2) /*User didn't enter filename*/ { /* Check for two arguments*/ printf("You failed to enter filename! Good-bye!!!\n"); exit(0); /* Quit programs*/ } if (argc == 2) { /*Try to open file*/ if ((fp=fopen(argv[1],"r")) == NULL) { /*Report failure*/ printf("Cannot open your file. \n\n Sorry!\n"); exit(0); } } if (argc == 3) { /*Decipher parameters*/ strcpy(parameters,argv[1]); for (c=0;c<=strlen(parameters);c++) { letter = parameters[c]; /*printf("%d\n",letter);*/ if (letter == 97) alphabeticorder = 0; if (letter == 122) alphabeticorder = 1; if (letter == 85) cases = 1; if (letter == 108) cases = 2; if (letter == 112) { StartPoint = 64; StopPoint = 123; } } /*Try to open file*/ if ((fp=fopen(argv[2],"r")) == NULL) { /*Report failure*/ printf("Cannot open your file. \n\n Sorry!\n"); exit(0); } } /*User entered too many variables*/ if (argc > 3) { /*Report error*/ printf("Error. Please check man pages \n\n"); exit(0); } x = 0; /* Set initial variables*/ word[0] ='\0'; root = NULL; /* Sets start point to null for tree*/ while ((c = getc(fp)) != EOF) /*Loop thru file*/ { /*Build word*/ if ((c > StartPoint) && (c < StopPoint)) { if (cases == 0) word[x++] = c; if (cases == 1) word[x++] = toupper(c); if (cases == 2) word[x++] = tolower(c); } else { word[x++] = '\0'; /* Add eos marker */ if (strlen(word) > 0) root = tree(root);/* Add word to tree */ word[0] = '\0'; /* Set word to null */ x = 0; /* Set x back to zero */ } } /*while*/ close(fp); /* Close the file */ /*Print tree out*/ if (alphabeticorder == 0) treeprint1(root,0); else treeprint2(root,0); }/*main*/ /***********************************************************/ /* Building the tree in memory */ struct tnode *tree(p) struct tnode *p; { struct tnode *talloc(); int cond; int x; if (p==NULL) { p = talloc(); /* Allocation of memory */ strcpy( p->listword,word); /* Copying word to node */ p->count = 1; /* Set counter to one */ p->left = p->right = NULL; /* Set pointer to null */ } else if (( cond = strcmp(word,p->listword)) == 0) p->count++; /* Increment the counter by one */ else if (cond < 0) p->left = tree(p->left); /* Move to the left node for comparison */ else p->right = tree(p->right); /* Move to the right node for comparison */ return (p); } /***********************************************************/ /* Allocation in the heap */ struct tnode *talloc() { return ((struct tnode *) malloc ((char *)(100))); } /***********************************************************/ /* Print out the entire tree in alphabetic order */ treeprint1(p) struct tnode *p; { if (p != NULL) /* Until end*/ { /* Prints results*/ treeprint1(p->left); /*go right*/ printf("%s {%d}\n", p->listword,p->count); treeprint1(p->right); /*go left*/ } } /***********************************************************/ /* Print out the entire tree in reverse alphabetic order*/ treeprint2(p) struct tnode *p; { if (p != NULL) /* Until end*/ { /* Print results*/ treeprint2(p->right); /*go right*/ printf("%s {%d}\n",p->listword,p->count); treeprint2(p->left); /*go left*/ } }