Question: For this computer assignment, you are to write a C++ program to create, search, print, and manage an item inventory. fix the problems with this
For this computer assignment, you are to write a C++ program to create, search, print, and manage an item inventory. fix the problems with this code.
#include "assignment8.h" #include #include #include #include #include
using namespace std;
struct Entry { std::string key; std::string description;
Entry() { key = "---"; } };
class HT { private: std::vector* hTable; int table_size; int item_count; int hashing(const std::string&); public: HT(int size); ~HT(); bool insert(const Entry&); int search(const std::string&); bool remove(const std::string&); void print(); };
Entry* get_entry(const string& line) { // this will create an entry to hold key and description. Entry* parsed_line = new Entry;
parsed_line->key = line.substr(2, 3); parsed_line->description = line.substr(6, line.length() - 6); return parsed_line; }
string get_key(const string& line) { // this will take an input and return the item key string gKey = line.substr(2, 3); return gKey; }
HT::HT(int s = 11) { // default constructor. The hash table hTable = new vector(s); table_size = s; item_count = 0; }
HT::~HT() { // the destructor for Hash Table delete hTable; }
int HT::search(const string& key) { //public function that searches the hash table for a record with // the given key int keyIndex = hashing(key); for (int i = 0; i < table_size; i++) { if ((*hTable)[keyIndex].key == key) return keyIndex; keyIndex = (keyIndex + 1) % table_size; } return -1; }
bool HT::insert(const Entry& e) {
//public member function to inserts item e in the hash table. It first // checks if the items key already exists in the hash table by calling the // search() function. int keyIndex = hashing(e.key);
string sKey = e.key; if (search(sKey) != -1) { cerr << "Unable to insert - identical key found" << endl; return false; } else if (item_count == table_size) { cerr << "Unable to insert - the table is full." << endl; return false; }
else { int i; for (i = 0; i < table_size; i++) { if ((*hTable)[keyIndex].key == "---" || (*hTable)[keyIndex].key == "+++") { (*hTable)[keyIndex] = e; item_count++;
return true; } keyIndex = keyIndex + 1 % table_size; } } return false; }
bool HT::remove(const string& s) {
// is public member function for remove it simply removes an item with // the given key value if (search(s) == -1) { return false; }
else { int keyIndex = search(s);
(*hTable)[keyIndex].key = "+++"; item_count--; return true; } }
void HT::print() { //public member function for printing the existing entries in the // hash table cout << endl << "----Hash Table-----" << endl;
for (int i = 0; i < table_size; i++) { if ((*hTable)[i].key != "---" && (*hTable)[i].key != "+++") { cout << right << setw(2) << i << ": "; cout << (*hTable)[i].key << " "; cout << (*hTable)[i].description << endl; } } cout << "---------------------------" << endl << endl; }
int HT::hashing(const string& key) { //compute sum return ((int)key[0] + (int)key[1] + (int)key[2]) % table_size; }
int main(int argc, char** argv) { if (argc < 2) { cerr << "argument: file-name "; return 1; }
HT ht; ifstream infile(argv[1], ios::in); string line; infile >> line; while (!infile.eof()) { if (line[0] == 'A') { Entry* e = get_entry(line); ht.insert(*e); delete e; } else { string key = get_key(line); if (line[0] == 'D') { cout << "Removing " << key << "... "; bool removed = ht.remove(key); if (removed) cout << key << " is removed successfully... "; else cout << key << " does not exist, no key is removed... "; } else if (line[0] == 'S') { int found = ht.search(key); if (found < 0) cout << key << " does not exit in the hash table ..." << endl << endl; else cout << key << " is found at table position " << found << endl << endl; } else if (line[0] == 'P') { cout << " Displaying the table: " << endl; ht.print(); } else cerr << "wrong input: " << line << endl; } infile >> line;
}
infile.close(); return 0; }
#ifndef assignment8.h_H #define assignment8.h_H #include #include
struct Entry { std::string key; std::string description;
Entry() { key = "---"; } };
class HT { private: std::vector* hTable; int table_size; int item_count; int hashing(const std::string&); public: HT(int size); ~HT(); bool insert(const Entry&); int search(const std::string&); bool remove(const std::string&); void print(); }; #endif
//A : AB1 : IDs //A : AB3 : name //A : CA0 : keys //A : BA2 : book //A : AC0 : grades //A : AF3 : comments //P : //S : AB3 //S : BA2 //S : AF4 //S : AA2 //D : AF3 //D : GA4 //D : FA3 //D : BA3 //D : AB3 //D : AC0 //P : //S : BA2 //S : AB2 //S : AD3 //A : AC0 : grades //A : GA0 : year //A : BB0 : graduation //D : AB1 //P : //S : BA0 //S : BB0
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
