add hashtable statistics, fix hashtable remove call

This commit is contained in:
mljoshi 2024-06-16 21:14:18 -07:00
parent 6bf9b6c9ee
commit 3a0c73cd1c

115
main.cpp
View File

@ -35,9 +35,10 @@
using std::cin, std::cout, std::string, std::vector; using std::cin, std::cout, std::string, std::vector;
void processInput(char command, HashTable<CPU>& table, BinarySearchTree<string>& tree, Stack<CPU>& stack); void processInput(char command, HashTable<CPU> &table, BinarySearchTree<string> &tree, Stack<CPU> &stack);
int main() { int main()
{
// Print help table for commands // Print help table for commands
printHelp(); printHelp();
@ -46,14 +47,18 @@ int main() {
Stack<CPU> undoStack; Stack<CPU> undoStack;
char command = ' '; char command = ' ';
while (command != 'Q') { while (command != 'Q')
{
cout << "Enter an option (H - for help): "; cout << "Enter an option (H - for help): ";
cin >> command; cin >> command;
command = toupper(command, std::locale()); command = toupper(command, std::locale());
// Temporary try catch block to handle unimplemented commands // Temporary try catch block to handle unimplemented commands
try { try
{
processInput(command, cpuTable, cpuTree, undoStack); processInput(command, cpuTable, cpuTree, undoStack);
} catch (std::logic_error& e) { }
catch (std::logic_error &e)
{
cout << e.what() << '\n'; cout << e.what() << '\n';
} }
} }
@ -70,49 +75,55 @@ void deleteCPU(HashTable<CPU> &hashTable, BinarySearchTree<string> &tree, Stack<
void undoDelete(HashTable<CPU> &hashTable, BinarySearchTree<string> &tree, Stack<CPU> &undoStack); void undoDelete(HashTable<CPU> &hashTable, BinarySearchTree<string> &tree, Stack<CPU> &undoStack);
void processInput(char command, HashTable<CPU> &cpuTable, BinarySearchTree<string> &cpuTree, Stack<CPU> &undoStack) { void processInput(char command, HashTable<CPU> &cpuTable, BinarySearchTree<string> &cpuTree, Stack<CPU> &undoStack)
switch (command) { {
case 'H': switch (command)
printHelp(); {
break; case 'H':
case 'I': // Insert a new record printHelp();
insertCPU(cpuTable, cpuTree); break;
break; case 'I': // Insert a new record
case 'F': // File input: add data from a file insertCPU(cpuTable, cpuTree);
throw std::logic_error("Not yet implemented: File input: add data from a file"); break;
break; case 'F': // File input: add data from a file
case 'D': // Delete one record throw std::logic_error("Not yet implemented: File input: add data from a file");
deleteCPU(cpuTable, cpuTree, undoStack); break;
break; case 'D': // Delete one record
case 'U': // Undo delete deleteCPU(cpuTable, cpuTree, undoStack);
undoDelete(cpuTable, cpuTree, undoStack); break;
break; case 'U': // Undo delete
case 'L': // List all CPUs sorted by primary key undoDelete(cpuTable, cpuTree, undoStack);
throw std::logic_error("Not yet implemented: List all CPUs sorted by primary key"); break;
break; case 'L': // List all CPUs sorted by primary key
case 'S': // Search for a CPU by the primary key throw std::logic_error("Not yet implemented: List all CPUs sorted by primary key");
throw std::logic_error("Not yet implemented: Search for a CPU by the primary key"); break;
break; case 'S': // Search for a CPU by the primary key
case 'W': // Write data to a file throw std::logic_error("Not yet implemented: Search for a CPU by the primary key");
throw std::logic_error("Not yet implemented: Write data to a file"); break;
break; case 'W': // Write data to a file
case 'T': // Hashtable statistics throw std::logic_error("Not yet implemented: Write data to a file");
throw std::logic_error("Not yet implemented: Hashtable statistics"); break;
break; case 'T': // Hashtable statistics
case 'P': // Print indented tree // throw std::logic_error("Not yet implemented: Hashtable statistics");
throw std::logic_error("Not yet implemented: Print indented tree"); cout << "Load factor: " << cpuTable.getLoadFactor() << std::endl;
break; cout << "Total number of collisions: " << cpuTable.getTotalCollisions() << std::endl;
case 'Z': // Display names of team members cout << "Longest collision path: " << cpuTable.getMaxCollisions() << std::endl;
printTeam(); break;
break; case 'P': // Print indented tree
case 'Q': // Quit throw std::logic_error("Not yet implemented: Print indented tree");
break; break;
default: case 'Z': // Display names of team members
cout << "Invalid command. Press 'H' to view available commands.\n"; printTeam();
break;
case 'Q': // Quit
break;
default:
cout << "Invalid command. Press 'H' to view available commands.\n";
} }
} }
void insertCPU(HashTable<CPU> &hashTable, BinarySearchTree<string> &tree) { void insertCPU(HashTable<CPU> &hashTable, BinarySearchTree<string> &tree)
{
string cpuId; string cpuId;
int releaseYear, coreCount; int releaseYear, coreCount;
string architecture; string architecture;
@ -133,26 +144,30 @@ void insertCPU(HashTable<CPU> &hashTable, BinarySearchTree<string> &tree) {
tree.insert(cpuId); tree.insert(cpuId);
} }
void deleteCPU(HashTable<CPU> &hashTable, BinarySearchTree<string> &tree, Stack<CPU> &undoStack) { void deleteCPU(HashTable<CPU> &hashTable, BinarySearchTree<string> &tree, Stack<CPU> &undoStack)
{
string cpuId; string cpuId;
cout << "Enter CPU ID to delete: "; cout << "Enter CPU ID to delete: ";
cin >> cpuId; cin >> cpuId;
CPU cpu(cpuId, 0, 0, "", 0.0); CPU cpu(cpuId, 0, 0, "", 0.0);
CPU cpuFound; CPU cpuFound;
while (!hashTable.search(cpuFound, cpu, key_to_index)) { while (!hashTable.search(cpuFound, cpu, key_to_index))
{
cout << "CPU ID not found. Enter a valid CPU ID: "; cout << "CPU ID not found. Enter a valid CPU ID: ";
cin >> cpuId; cin >> cpuId;
cpu = CPU(cpuId, 0, 0, "", 0.0); cpu = CPU(cpuId, 0, 0, "", 0.0);
} }
hashTable.remove(cpuFound, cpu, key_to_index);
undoStack.push(cpuFound); undoStack.push(cpuFound);
hashTable.remove(cpu, key_to_index);
tree.remove(cpuId); tree.remove(cpuId);
cout << "CPU ID \"" << cpuId << "\" deleted.\n"; cout << "CPU ID \"" << cpuId << "\" deleted.\n";
} }
void undoDelete(HashTable<CPU> &hashTable, BinarySearchTree<string> &tree, Stack<CPU> &undoStack) { void undoDelete(HashTable<CPU> &hashTable, BinarySearchTree<string> &tree, Stack<CPU> &undoStack)
if (undoStack.isEmpty()) { {
if (undoStack.isEmpty())
{
cout << "No deletions to undo.\n"; cout << "No deletions to undo.\n";
return; return;
} }