From 1f66b519891735c594cca9b8926eb85934a90182 Mon Sep 17 00:00:00 2001 From: Iurii Tatishchev Date: Fri, 14 Jun 2024 14:35:58 -0700 Subject: [PATCH] implement delete and undoDelete, minor logic fixes --- .idea/vcs.xml | 1 + CPU.h | 4 ++- HashTable.h | 4 +-- main.cpp | 87 +++++++++++++++++++++++++-------------------------- utils.cpp | 33 +++++++++++++++++++ utils.h | 4 +++ 6 files changed, 86 insertions(+), 47 deletions(-) diff --git a/.idea/vcs.xml b/.idea/vcs.xml index 6c0b863..288b36b 100644 --- a/.idea/vcs.xml +++ b/.idea/vcs.xml @@ -2,5 +2,6 @@ + \ No newline at end of file diff --git a/CPU.h b/CPU.h index ff3c6c1..32e4783 100644 --- a/CPU.h +++ b/CPU.h @@ -13,6 +13,8 @@ private: double baseClock; public: + CPU() : cpuId(""), releaseYear(0), coreCount(0), architecture(""), baseClock(0.0) {}; + CPU(std::string id, int year, int cores, std::string arch, double clock) : cpuId(std::move(id)), releaseYear(year), @@ -20,7 +22,7 @@ public: architecture(std::move(arch)), baseClock(clock) {}; - std::string getCpuId() const; + std::string getCpuId() const { return cpuId; }; int getReleaseYear() const; diff --git a/HashTable.h b/HashTable.h index b047e75..922f6fe 100644 --- a/HashTable.h +++ b/HashTable.h @@ -30,9 +30,9 @@ public: bool insert(const T &item, int h(const T&, int)) { throw std::logic_error("Not implemented: HashTable.insert()"); }; - bool remove(const T &item, int h(const T&), int) { throw std::logic_error("Not implemented: HashTable.remove()"); }; + bool remove(const T &item, int h(const T&, int)) { throw std::logic_error("Not implemented: HashTable.remove()"); }; - bool search(const T &item, int h(const T&), int) { throw std::logic_error("Not implemented: HashTable.search()"); }; + bool search(T& itemOut, const T &item, int h(const T&, int)) { throw std::logic_error("Not implemented: HashTable.search()"); }; int getTotalCollisions() const { throw std::logic_error("Not implemented: HashTable.getTotalCollisions()"); }; diff --git a/main.cpp b/main.cpp index 991e8ab..314c065 100644 --- a/main.cpp +++ b/main.cpp @@ -35,58 +35,24 @@ using std::cin, std::cout, std::string, std::vector; -void printHelp() { - static const vector helpWidths = {5, 40}; - static const vector> helpData = { - {"Key", "Command"}, - {"H", "Help"}, - {"I", "Insert a new record"}, - {"F", "File input: add data from a file"}, - {"D", "Delete one record"}, - {"U", "Undo delete"}, - {"L", "List all CPUs sorted by primary key"}, - {"S", "Search for a CPU by the primary key"}, - {"W", "Write data to a file"}, - {"T", "Hashtable statistics"}, - {"Q", "Quit"}, - {"P", "Hidden print option (do not show it in the menu: print indented tree)"}, - {"Z", "Hidden option (do not show it in the menu: display names of team members)"}, - }; - printTable(helpWidths, helpData); -} - -void printTeam() { - static const vector teamWidths = {40}; - static const vector> teamData = { - {"Team Members"}, - {"Kevin Cremin"}, - {"Kevin Galvan Serrano"}, - {"Joshiro Lawrence"}, - {"Tuhin Mondal"}, - {"Iurii Tatishchev"}, - }; - printTable(teamWidths, teamData); -} - -void processInput(char command, HashTable& table, BinarySearchTree& tree, Stack& stack); - -CPU readCPUInfo(); +void processInput(char command, HashTable& table, BinarySearchTree& tree, Stack& stack); int main() { // Print help table for commands printHelp(); HashTable cpuTable; - BinarySearchTree cpuTree; + BinarySearchTree cpuTree; Stack undoStack; char command = ' '; while (command != 'Q') { cout << "Enter an option (H - for help): "; cin >> command; + command = toupper(command, std::locale()); // Temporary try catch block to handle unimplemented commands try { - processInput(toupper(command, std::locale()), cpuTable, cpuTree, undoStack); + processInput(command, cpuTable, cpuTree, undoStack); } catch (std::logic_error& e) { cout << e.what() << '\n'; } @@ -98,9 +64,13 @@ int main() { return 0; } -void insertCPU(HashTable &hashTable, BinarySearchTree &tree); +void insertCPU(HashTable &hashTable, BinarySearchTree &tree); -void processInput(char command, HashTable &cpuTable, BinarySearchTree &cpuTree, Stack &undoStack) { +void deleteCPU(HashTable &hashTable, BinarySearchTree &tree, Stack &undoStack); + +void undoDelete(HashTable &hashTable, BinarySearchTree &tree, Stack &undoStack); + +void processInput(char command, HashTable &cpuTable, BinarySearchTree &cpuTree, Stack &undoStack) { switch (command) { case 'H': printHelp(); @@ -112,10 +82,10 @@ void processInput(char command, HashTable &cpuTable, BinarySearchTree throw std::logic_error("Not yet implemented: File input: add data from a file"); break; case 'D': // Delete one record - throw std::logic_error("Not yet implemented: Delete one record"); + deleteCPU(cpuTable, cpuTree, undoStack); break; case 'U': // Undo delete - throw std::logic_error("Not yet implemented: Undo delete"); + undoDelete(cpuTable, cpuTree, undoStack); break; case 'L': // List all CPUs sorted by primary key throw std::logic_error("Not yet implemented: List all CPUs sorted by primary key"); @@ -142,7 +112,7 @@ void processInput(char command, HashTable &cpuTable, BinarySearchTree } } -void insertCPU(HashTable &hashTable, BinarySearchTree &tree) { +void insertCPU(HashTable &hashTable, BinarySearchTree &tree) { string cpuId; int releaseYear, coreCount; string architecture; @@ -160,5 +130,34 @@ void insertCPU(HashTable &hashTable, BinarySearchTree &tree) { CPU cpu(cpuId, releaseYear, coreCount, architecture, baseClock); hashTable.insert(cpu, key_to_index); - tree.insert(cpu); + tree.insert(cpuId); +} + +void deleteCPU(HashTable &hashTable, BinarySearchTree &tree, Stack &undoStack) { + string cpuId; + cout << "Enter CPU ID to delete: "; + cin >> cpuId; + CPU cpu(cpuId, 0, 0, "", 0.0); + CPU cpuFound; + while (!hashTable.search(cpuFound, cpu, key_to_index)) { + cout << "CPU ID not found. Enter a valid CPU ID: "; + cin >> cpuId; + cpu = CPU(cpuId, 0, 0, "", 0.0); + } + undoStack.push(cpuFound); + hashTable.remove(cpu, key_to_index); + tree.remove(cpuId); + + cout << "CPU ID \"" << cpuId << "\" deleted.\n"; +} + +void undoDelete(HashTable &hashTable, BinarySearchTree &tree, Stack &undoStack) { + if (undoStack.isEmpty()) { + cout << "No deletions to undo.\n"; + return; + } + CPU cpu = undoStack.pop(); + hashTable.insert(cpu, key_to_index); + tree.insert(cpu.getCpuId()); + cout << "Undo deletion of CPU ID \"" << cpu.getCpuId() << "\".\n"; } diff --git a/utils.cpp b/utils.cpp index 8ba08b3..0e081c6 100644 --- a/utils.cpp +++ b/utils.cpp @@ -69,3 +69,36 @@ void printTable(const vector& widths, const vector>& data) { } cout << "\b┘\n"; } + +void printHelp() { + static const vector helpWidths = {5, 40}; + static const vector> helpData = { + {"Key", "Command"}, + {"H", "Help"}, + {"I", "Insert a new record"}, + {"F", "File input: add data from a file"}, + {"D", "Delete one record"}, + {"U", "Undo delete"}, + {"L", "List all CPUs sorted by primary key"}, + {"S", "Search for a CPU by the primary key"}, + {"W", "Write data to a file"}, + {"T", "Hashtable statistics"}, + {"Q", "Quit"}, + {"P", "Hidden print option (do not show it in the menu: print indented tree)"}, + {"Z", "Hidden option (do not show it in the menu: display names of team members)"}, + }; + printTable(helpWidths, helpData); +} + +void printTeam() { + static const vector teamWidths = {40}; + static const vector> teamData = { + {"Team Members"}, + {"Kevin Cremin"}, + {"Kevin Galvan Serrano"}, + {"Joshiro Lawrence"}, + {"Tuhin Mondal"}, + {"Iurii Tatishchev"}, + }; + printTable(teamWidths, teamData); +} diff --git a/utils.h b/utils.h index 395bd18..0599f65 100644 --- a/utils.h +++ b/utils.h @@ -9,4 +9,8 @@ void printRow(const vector &widths, const vector& row); void printTable(const vector& widths, const vector>& data); +void printHelp(); + +void printTeam(); + #endif //INC_08_TEAM_PROJECT_UTILS_H