// APS105 Labi8 12/26/06 // Geoffrey Lang // 995155772 // langgeof@ecf.utoronto.ca #include #include #include using namespace std; const int MAX_WORDS = 10000; const int MAX_WORD_LENGTH = 25; struct WordListNode { char *word; int count; WordListNode *next; }; void readWords(WordListNode *head, ifstream &inFile, int& n); void removePunctuation(char word[]); bool goodLetter(char i); void printWords(WordListNode *head); void printInfo(int n, int u); int main(int argc, char *argv[]) { int n = 0, u = 0; // n - total words WordListNode head; // u - unique words if(argc == 2) { ifstream inFile; inFile.open(argv[1]); if(!inFile.fail()) { readWords(&head, inFile, n); printWords(&head); printInfo(n, u); } } else { cout << "Correct usage: " << argv[0] << " filename" << endl; } return 0; } void readWords(WordListNode *head, ifstream &inFile,int &n) { WordListNode *i = head; while(!inFile.eof()) { char current[MAX_WORD_LENGTH + 1]; inFile >> current; removePunctuation(current); if(strlen(current) > 0 && !inFile.eof()) { i -> word = new char[strlen(current) + 1]; strcpy(i -> word, current); i -> next = new WordListNode; i = i -> next; n++; } } i->next = NULL; } void removePunctuation(char word[]) { char temp[MAX_WORD_LENGTH]; int i = 0, j = 0, k; strncpy(temp, word, MAX_WORD_LENGTH); while(temp[i] != '\0' && !goodLetter(temp[i])) { i++; } k = i; word[0] = '\0'; while(temp[i] != '\0') { word[j] = tolower(temp[i]); if(goodLetter(temp[i])) { k = j; } i++; j++; } word[k+1] = '\0'; } bool goodLetter(char i) { if((i >= 'a' && i <= 'z') || (i >= 'A' && i <= 'Z') || (i >= '0' && i <= '9')) { return 1; } else { return 0; } } void printWords(WordListNode *head) { WordListNode *i; for(i = head; i != NULL; i = i -> next) { cout << i -> word << " (" << i -> count << ")" << endl; } } void printInfo(int n,int u) { cout << "Words processed:\t" << n << "." << endl; cout << "Unique words:\t" << u << "." << endl; }