Question: Please add the following to the code: 1) Check the command line arguments. A file is specified as -f textfile. If a file has not

Please add the following to the code:

1) Check the command line arguments. A file is specified as "-f textfile". If a file has not been specified, exit the program with a meaningful error message.

2) Open the input file specified by argv[2] and enter into an endless loop that reads and processes the text lines. Save each text line in a cache (e.g., vector) for retrieval later on. Then change all so-called punctuation characters, i.e., (, ), {, }, and ., to a blank space. Specifically, wrap std::ispunct(), which can handle the detection (see "man ispunct"), inside a function to be called replace_punctuation() that you pass along to std::transform() which sweeps thru the text line characters. Have function replace_punctuation() return either the character tested (give as its input argument) or a blank space. Finally, use a string stream to parse the resulting text line into words which you insert into a hash table along with the current line number. Close the input file when EOF is reached.

3) Add a second endless loop that prompts the user for words to search for. Print all line numbers and the corresponding text for each successfull search. Print nothing for an unsuccessfull search. Use the vector of line numbers returned by hash_table::find() to do this. Look up the text in the above mentioned data cache for each line number. See output examples below.

4) Make no assumptions about the number of lines in the input file. Make no assumptions about the number of characters or words on each line. The test files may be pure ascii text, html code, or even a C++ program. Your code should work regardless of the type of ascii data stored in the file.

Given Code:

#include <...> using namespace std; typedef unsigned int uint; template class hash_table { public: hash_table(); void insert(const Tkey &); private: int hash(const Tkey &); int nextprime(int); int qprobe(const Tkey &); void resize(); int num_inuse; int max_inuse; vector table; }; template hash_table::hash_table() { int N = 23; table.assign(N, Tkey()); num_inuse = 0; max_inuse = N/2; // quadratic probe max value } template void hash_table::insert(const Tkey &key) { int index = qprobe(key); if (table[index] == Tkey()) { table[index] = key; if (++num_inuse >= max_inuse) resize(); } } See previous page for hash() function template code. template int hash_table::nextprime(int N) { int i = 2; while (i*i <= N) { if (N%i == 0) { N++; i = 1; } i++; } return max(3,N); } template int hash_table::qprobe(const Tkey &key) { int index = hash(key); int k = 0; while (table[index] != Tkey() && table[index] != key) { index += 2*(++k) - 1; index = index % table.size(); } return index; } template void hash_table::resize() { vector tmp_table; for (int i=0; i<(int)table.size(); i++) { if (table[i] != Tkey()) tmp_table.push_back(table[i]); } int N = nextprime(2*table.size()); table.assign(N, Tkey()); num_inuse = 0; max_inuse = N/2; for (int i=0; i<(int)tmp_table.size(); i++) { Tkey &key = tmp_table[i]; table[ qprobe(key) ] = key; num_inuse++; } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!