/* main.cpp Program to read in strings from a file and insert them into a dictionary. Date: Feb 8, 2001 Flags: -b binary search tree -r AVL tree (recursive) -i AVL tree (iterative) */ #include #include #include "mystring.h" #include "AvlTree.h" #include "BinarySearchTree.h" void main(int argc, char * argv[]) { bool error = false; // timer variables int total_time = 0; int start_time = 0; int finish_time = 0; int num_insertions = 0; enum algorithm { NOALG = 0, useBST = 1, useAVL_recursive = 2, useAVL_iterative = 3 } ; algorithm which_algorithm = NOALG; ifstream infile_stream; ofstream outfile_stream; const string ITEM_NOT_FOUND (""); AvlTree avltree(ITEM_NOT_FOUND); BinarySearchTree bst(ITEM_NOT_FOUND); string str; // Handle command line arguments. // Usage: argv[0] -[bri] input_filename output_filename // Options: // -b use a standard binary search tree // -r use the recursive implementation of an AVL tree // -i use the iterative implementation of an AVL tree if ((argc < 4) || (argc > 4)) { cout << "Usage: " << argv[0] << " -[bri] infile outfile." << endl; error = true; } else { // figure out which option was chosen if (argv[1][0] == '-') { switch (argv[1][1]) { case 'b': which_algorithm = useBST; break; case 'r': which_algorithm = useAVL_recursive; break; case 'i': which_algorithm = useAVL_iterative; break; default: cout << "Usage: "; cout << "-" << argv[1][1] << " is not a valid option." << endl; error = true; break; } // Get the input filename infile_stream.open(argv[2]); if (!infile_stream) { cout << "Error: Could not open " << argv[2] << "." << endl; error = true; } // Get the output filename outfile_stream.open(argv[3]); if (!outfile_stream) { cout << "Error: Could not open " << argv[3] << "." << endl; error = true; } } else { cout << "Usage: " << argv[0] << " -[bri] filename." << endl; error = true; } } if (!error) { // start the timer start_time = clock(); while (getword(infile_stream, str)) { switch (which_algorithm) { case useBST: bst.insert(str); num_insertions++; break; case useAVL_recursive: avltree.insert(str); num_insertions++; break; case useAVL_iterative: avltree.insert_iter(str); num_insertions++; break; } } // stop the timer finish_time = clock(); total_time += (finish_time - start_time); cout << "Processor time for insertions: "; cout << total_time * 1000 / CLOCKS_PER_SEC << " ms." << endl; cout << "Number of insertions: " << num_insertions << endl; // Print out (in sorted order) each string, along with // its frequency, to outfile_stream } // Close the file streams. if (infile_stream) { infile_stream.close(); } if (outfile_stream) { outfile_stream.close(); } cout << argv[0] << " is done." << endl; }