diff options
| Commit | 77df09397e7fc517dd387d8e46d993ad30f69853 (patch) | |
|---|---|---|
| Author | 2022-12-29 10:12:20 +0200 | |
| Committer | 2022-12-29 10:12:20 +0200 | |
| Files | b38c4fcd01f748f780f03f34bc51730ddf728ade | |
| uni-projects-77df09397e7fc517dd387d8e46d993ad30f69853.tar uni-projects-77df09397e7fc517dd387d8e46d993ad30f69853.tar.gz uni-projects-77df09397e7fc517dd387d8e46d993ad30f69853.zip | ||
| Parent | b6e654657da058e9a438ec4583fac8028758dfb4 (diff) | |
[SDP/P/exc] Added file reading
4 files changed, 106 insertions, 14 deletions
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<std::string>& items, const std::deque<Box*> 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 <string> -#include <vector> +#include <deque> #include <ostream> #include <istream> class Box { std::string name; - std::vector<std::string> items; - std::vector<Box*> subboxes; + std::deque<std::string> items; + std::deque<Box*> subboxes; std::ostream& printWithInd(std::ostream&, std::string); public: - Box(const std::string& name, const std::vector<std::string>& items, const std::vector<Box*> subboxes) : name(name), items(items), subboxes(subboxes) {} + Box(const std::string& name); + Box(const std::string& name, const std::deque<std::string>& items, const std::deque<Box*> 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 <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; + } } |
