// Chad Williamson #include #include #include #include #include "binarySearchTree.h" #include "linkedList.h" int main() { //inputChar for user input, inputFileName for user file to read from char inputChar = 'y'; std::string inputFileName = "input.txt"; //create binary search tree binarySearchTree BST; //read from .txt file, default is input.txt std::cout << "Use default input.txt file? [Y/N]: "; std::cin >> inputChar; while (!std::cin || (inputChar != 'y' && inputChar != 'Y' && inputChar != 'n' && inputChar != 'N')) { std::cout << "Use default input.txt file? [Y/N]: "; std::cin.clear(); std::cin.ignore(); std::cin >> inputChar; } //user wants to use a different .txt file if (inputChar == 'n' || inputChar == 'N') { std::cout << "Enter name of .txt file (do not include .txt extension): "; std::cin >> inputFileName; inputFileName = inputFileName.append(".txt"); } std::cout << "Cross-reference index for " << inputFileName << std::endl; //input file stream std::ifstream in(inputFileName.c_str()); //variables to store current string, word, end of file boolean, and store line number std::string currentString; std::string currentWord; bool EoFMarker = false; bool newLine = false; int lineNumber = 0; //while loop to read each line from the file while (getline(in, currentString) && !EoFMarker) { if (currentString.compare("#") == 0) { EoFMarker = true; } else { //removes duplicate new line at end of a line if (!newLine) { lineNumber++; } //parse each line to individual words std::stringstream inString(currentString); while (inString >> currentWord) { //detect new line characters if (currentWord.compare("\\n") == 0 || currentWord.compare("\\r") == 0) { lineNumber++; newLine = true; } else { newLine = false; //only accept letters and digits, starts with letter, limit to 10 char total //first clear any punctuation for (int i = 0, l = currentWord.size(); i < l; i++) { if (ispunct(currentWord[i])) { currentWord.erase(i--, 1); l = currentWord.size(); } } //limit to 10 characters if (currentWord.size() > 10) { for (int i = 10; i < currentWord.size(); i++) { currentWord.erase(i--, 1); } } //finally, check that word starts with a letter, and add to binary search tree if (!isdigit(currentWord.at(0))) { BST.insertNode(currentWord, lineNumber); } } } } }//end of getline-while loop //call recursive traversal of the binary search tree BST.recursiveTraversal(BST.getRoot()); //close file input and output, and pause before exiting in.close(); system("PAUSE"); return 0; }