From 77df09397e7fc517dd387d8e46d993ad30f69853 Mon Sep 17 00:00:00 2001 From: Syndamia Date: Thu, 29 Dec 2022 10:12:20 +0200 Subject: [SDP/P/exc] Added file reading --- .../Project/exc/Box.cpp | 27 +++++++-- .../Project/exc/Box.h | 13 ++-- .../Project/exc/boxesList.txt | 11 ++++ .../Project/exc/main.cpp | 69 ++++++++++++++++++++-- 4 files changed, 106 insertions(+), 14 deletions(-) create mode 100644 Year 2/Structures and data algorithms/Project/exc/boxesList.txt diff --git a/Year 2/Structures and data algorithms/Project/exc/Box.cpp b/Year 2/Structures and data algorithms/Project/exc/Box.cpp index 844a337..85f65ff 100644 --- a/Year 2/Structures and data algorithms/Project/exc/Box.cpp +++ b/Year 2/Structures and data algorithms/Project/exc/Box.cpp @@ -1,24 +1,39 @@ #include "Box.h" +Box::Box(const std::string& name) : name(name), items(), subboxes() +{} +Box::Box(const std::string& name, const std::deque& items, const std::deque subboxes) : name(name), items(items), subboxes(subboxes) +{} + +std::string& Box::get_name() { + return name; +} + +void Box::pushItem(std::string item) { + items.push_back(item); +} + +void Box::pushSubbox(Box* subbox) { + subboxes.push_back(subbox); +} + void Box::removeUselessSubBoxes() { - for (auto i = subboxes.begin(); i != subboxes.end(); ++i) { + for (auto i = subboxes.begin(), end = subboxes.end(); i != end; ++i) { + (*i)->removeUselessSubBoxes(); if ((*i)->items.empty() && (*i)->subboxes.size() < 2) { if ((*i)->subboxes.empty()) { - subboxes.erase(i); + subboxes.erase(i); // затова държа самия итератор continue; } - *i = (*i)->subboxes[0]; } - // else - // (*i)->removeUselessSubBoxes(); } } std::ostream& Box::printWithInd(std::ostream& ostr, std::string indent) { ostr << indent << name << std::endl; for (std::string& i : items) - ostr << indent << "+ " << i << std::endl; + ostr << indent << " + " << i << std::endl; indent += " "; for (Box* sb : subboxes) diff --git a/Year 2/Structures and data algorithms/Project/exc/Box.h b/Year 2/Structures and data algorithms/Project/exc/Box.h index 2a0c71b..c77ed6c 100644 --- a/Year 2/Structures and data algorithms/Project/exc/Box.h +++ b/Year 2/Structures and data algorithms/Project/exc/Box.h @@ -2,19 +2,24 @@ #define BOX #include -#include +#include #include #include class Box { std::string name; - std::vector items; - std::vector subboxes; + std::deque items; + std::deque subboxes; std::ostream& printWithInd(std::ostream&, std::string); public: - Box(const std::string& name, const std::vector& items, const std::vector subboxes) : name(name), items(items), subboxes(subboxes) {} + Box(const std::string& name); + Box(const std::string& name, const std::deque& items, const std::deque subboxes); + + std::string& get_name(); + void pushItem(std::string); + void pushSubbox(Box*); void removeUselessSubBoxes(); std::ostream& print(std::ostream&); diff --git a/Year 2/Structures and data algorithms/Project/exc/boxesList.txt b/Year 2/Structures and data algorithms/Project/exc/boxesList.txt new file mode 100644 index 0000000..20d0d7c --- /dev/null +++ b/Year 2/Structures and data algorithms/Project/exc/boxesList.txt @@ -0,0 +1,11 @@ +10 +PlovdivBox 2 Magnet Book 2 ArtBox TheatreBox +ArtBox 0 1 Drawings +Drawings 1 OldPlovdiv 0 +TheatreBox 0 1 TheatreSouvenirs +TheatreSouvenirs 0 2 Plates Bags +Plates 1 DecorativePlate 0 +Bags 0 0 +StaraZagoraBox 1 Postcard 2 Figurines Cups +Figurines 1 AncientFigurine 0 +Cups 1 TraditionalCup 0 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 +#include +#include 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 boxes; + std::deque 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; + } } -- cgit v1.3.1-26-gb76a