diff options
[SDP/P/exc] Added file reading
1 files changed, 65 insertions, 4 deletions
| -rw-r--r-- | Year 2/Structures and data algorithms/Project/exc/main.cpp | 69 |
diff --git a/Year 2/Structures and data algorithms/Project/exc/main.cpp b/Year 2/Structures and data algorithms/Project/exc/main.cpp index e9d7032..fa640c9 100644 --- a/Year 2/Structures and data algorithms/Project/exc/main.cpp +++ b/Year 2/Structures and data algorithms/Project/exc/main.cpp @@ -1,9 +1,70 @@ #include "Box.h" #include <iostream> +#include <fstream> +#include <exception> int main() { - Box plovdivBox("PlovdivBox", { "Magnet", "Book" }, { new Box("ArtBox", {}, { new Box("Drawings", { "OldPlovdiv" }, {}) }), new Box("TheatreBox", {}, { new Box("TheatreSouvenirs", {}, { new Box("Plates", { "DecorativePlate" }, {}), new Box("Bags", {}, {}) }) })}); - plovdivBox.print(std::cout) << std::endl; - plovdivBox.removeUselessSubBoxes(); - plovdivBox.print(std::cout) << std::endl; + std::ifstream inFile("boxesList.txt"); + if (!inFile.is_open()) + throw std::runtime_error("No file named boxesList.txt exists!"); + + unsigned b = 0; + inFile >> b; + + std::deque<Box*> boxes; + std::deque<Box*> bigBoxes; + + std::string name; + unsigned n = 0; + + Box* current = nullptr; + Box* cSubbox = nullptr; + while (b-- > 0 && !inFile.eof()) { + inFile >> name; + + // Get or create a box with the given name + current = nullptr; + for (Box* cb : boxes) { + if (cb->get_name() == name) { + current = cb; + } + } + if (current == nullptr) { + current = new Box(name); + boxes.push_back(current); + bigBoxes.push_back(current); + } + + // Add items to box + inFile >> n; // Item count + while (n-- > 0) { + inFile >> name; + current->pushItem(name); + } + + // Add subboxes + inFile >> n; // Subboxes count + while (n-- > 0) { + inFile >> name; + + // Find or create new box + cSubbox = nullptr; + for (Box* cb : boxes) { + if (cb->get_name() == name) + cSubbox = cb; + } + if (cSubbox == nullptr) { + cSubbox = new Box(name); + boxes.push_back(cSubbox); + } + + current->pushSubbox(cSubbox); + } + } + inFile.close(); + + for (Box* cb : bigBoxes) { + cb->removeUselessSubBoxes(); + cb->print(std::cout) << std::endl; + } } |
