Question
C++11 Table class implementation using std::vector and chained hashing. any other solutions are wrong Write table.h to define class Table as it is used in
C++11 Table class implementation using std::vector and chained hashing. any other solutions are wrong
Write table.h to define class Table as it is used in the demonstration program named tabledemo.cpp. Write table.cpp to implement your class Table. You may use tools from any of the standard libraries except
One constructor that builds an empty Table designed to hold a maximum number of entries equal to the only parameter. This parameter should have a default value of 100: Table(unsigned int max_entries = 100)
Another constructor that builds a Table designed to hold the number of entries specified by the first parameter, and puts that many entries into the Table by reading them one at a time from the input stream that is the second parameter: Table(unsigned int entries, std::istream& input) Do not input more than the specified number of entries. If you always read all of input, you will lose points for not satisfying this requirement.
Two (overloaded) member functions named put, each of which puts a new Entry into the Table: void put(unsigned int key, std::string data) void put(Entry e) The first of these functions creates a new Entry to put in the Table. The second one puts a copy of the parameter in the Table. In cases where the Table already contains an Entry with the given key, these functions act to update the Entry for that key. The Table is not allowed to contain duplicate keys.
A constant member function named get that returns the string associated with the parameter: std::string get(unsigned int key) const This function returns an empty string if the Table has no Entry with the given key.
A member function named remove that removes the Entry containing the given key: bool remove(unsigned int key) This function returns true if it removes an Entry, or false if the Table has no such Entry.
A non-member output function that overloads the << operator to print the Table: std::ostream& operator<< (std::ostream& out, const Table& t) This function is expected to print each Entry in the Table to a separate line of the given output stream in the order of their key values.
Your class may define other functions too, either public or private ones. In particular, you will probably want to add a hashing function to transform the key values - in our version, function hashkey is a private function. And of course, your class must define the private data a Table object stores.
HINT: You will probably want to use chained hashing for this lab. That means that each row in your Table will be of type std::vector
//THIS IS ENTRY.H FILE provided
#ifndef entry_h
#define entry_h
// entry.h - defines class Entry for CS 32 PA1
#include
#include
class Entry {
public:
// constructor
Entry(unsigned int key = 0, std::string data = "");
// access and mutator functions
unsigned int get_key() const;
std::string get_data() const;
static unsigned int access_count();
void set_key(unsigned int k);
void set_data(std::string d);
// operator conversion function simplifies comparisons
operator unsigned int () const;
// input and output friends
friend std::istream& operator>>
(std::istream& inp, Entry &e);
friend std::ostream& operator<<
(std::ostream& out, Entry &e);
private:
unsigned int key;
std::string data;
static unsigned int accesses;
};
#endif /* entry_h */
//THIS IS ENTRY.CPP File provided
//
// entry.cpp
// Lab05
//
// entry.cpp - implements class Entry
#include
#include
#include "entry.h"
using namespace std;
unsigned int Entry::accesses = 0;
Entry::Entry(unsigned int key, std::string data)
: key(key), data(data) { }
unsigned int Entry::get_key() const
{ ++accesses; return key; }
std::string Entry::get_data() const
{ return data; }
unsigned int Entry::access_count()
{ return accesses; }
void Entry::set_key(unsigned int k)
{ key = k; }
void Entry::set_data(std::string d)
{ data = d; }
Entry::operator unsigned int () const
{ return get_key(); }
istream& operator>> (istream& inp, Entry &e) {
inp >> e.key;
// get data in two parts to handle white space
string first_word, rest_of_line;
inp >> first_word;
getline(inp, rest_of_line);
e.data = first_word + rest_of_line;
return inp;
}
ostream& operator<< (ostream& out, Entry &e) {
out << e.get_key() << ": " << e.get_data();
return out;
}
//THIS IS tabledemo.cpp file provided
//
// tabledemo.cpp
// Lab05
// tabledemo.cpp - demonstration program for Table
// cmc, 4/4/2016
#include
#include
#include
#include "table.h"
using namespace std;
unsigned int user_get(Table &t);
unsigned int user_remove(Table &t);
int main() {
cout << "Demonstrate very small table ";
Table t(5);
t.put(7, "seven");
t.put(9, "nine");
t.put(17, "Seventeen");
t.put(4, "four");
t.put(36, "Thirty-six");
cout << t;
cout << "key accesses: " << Entry::access_count() << endl;
cout << " Now demonstrate default size table ";
Table t2;
Entry e;
unsigned int count = 0;
cout << "Enter up to 100 entries for a new table. "
<< "(enter 0 key and random data to quit) ";
do {
cin >> e;
if (e.get_key() == 0) break;
t2.put(e);
++count;
} while(count < 100);
cout << t2;
cout << "Try removing some entries (enter 0 to quit) ";
while (user_remove(t2) != 0)
;
cout << t2;
cout << " Finally demonstrate larger table ";
ifstream input;
input.open("fips.txt");
if (!input.good()) {
cout << "No fips.txt in current directory. Quitting ";
return 1;
}
Table t3(3142, input);
cout << "Try getting some entries by FIPS code keys "
<< "(enter 0 key to quit) ";
while (user_get(t3) != 0)
;
cout << "Print large table to sortedfips.txt? ";
char ans;
cin >> ans;
if (ans == 'Y' || ans == 'y') {
unsigned int start = Entry::access_count();
ofstream out;
out.open("sortedfips.txt");
out << t3;
out.close();
cout << "done writing to sortedfips.txt ... required "
<< Entry::access_count() - start << " accesses ";
}
return 0;
}
unsigned int user_get(Table &t) {
unsigned int key;
cout << "Enter key to get: ";
cin >> key;
if (key != 0) {
unsigned int start = Entry::access_count();
cout << "data at key " << key << ": " << t.get(key) << endl;
cout << "(accesses: " <<
Entry::access_count() - start << ") ";
}
return key;
}
unsigned int user_remove(Table &t) {
unsigned int key;
cout << "Enter key to remove: ";
cin >> key;
if (key != 0) {
unsigned int start = Entry::access_count();
if (t.remove(key))
cout << "removed key: " << key << endl;
else
cout << "did not find key: " << key << endl;
cout << "(accesses: "
<< Entry::access_count() - start << ") ";
}
return key;
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started