Question: Modify the given code as follows: Check the command line arguments. A file is specified as -f textfile. If a file has not been specified,

Modify the given code as follows:

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.

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.

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.

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.

#include <...>

using namespace std;

typedef unsigned int uint;

template

class hash_table {

public:

hash_table(int N=9999);

void insert(const Tkey &);

private:

int hash(const Tkey &);

vector table;

};

template

hash_table::hash_table(int N) {

table.assign(N, Tkey());

}

template

void hash_table::insert(const Tkey &key) {

int index = hash(key);

cout << setw(20) << left << key

<< "index "

<< setw(3) << right << index;

if (table[index] == key) {

cout << " -- key repeat (" << table[index] << ")";

} else if (table[index] == Tkey()) {

table[index] = key;

} else {

cout << " -- collision (" << table[index] << ")";

}

cout << " ";

}

Hint: Keys that hash to the index produces collisions. Above we detect and report them. Next handout provides

work arounds.

template<>

int hash_table::hash(const int &key) {

return (uint)key % table.size();

}

template<>

int hash_table::hash(const float &key) {

return *(uint *)&key % table.size();

}

template<>

int hash_table::hash(const string &key) {

uint index = 0;

const char *p = key.c_str();

while (*p)

index = ((index << 5) | (index >> 27)) + *p++;

return index % table.size();

}

int main(int argc, char *argv[]) {

hash_table H;

string key;

while (cin >> key)

H.insert(key);

}

Hint: The odd-looking type casting for hashing of floats serves to ensure full use of the underlying 32-bit data.

Hint: The binary logic used when hashing strings serves to implement cyclic shifting to prevent loss of bits as

more and more chars are added to the index. The 5 MSB bits become LSB bits. The remaining 27 bits are shifted

left to become the new MSB bits.

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!