Summary Files Commits Branches @commit @diff @stats
path: root / Year 2 / Structures and data algorithms / Project / exc / main.cpp
diff options
context:
space:
mode:
Commit77df09397e7fc517dd387d8e46d993ad30f69853 (patch)
Author
 Syndamia <kamen@syndamia.com>
2022-12-29 10:12:20 +0200
Committer
 Syndamia <kamen@syndamia.com>
2022-12-29 10:12:20 +0200
Filesb38c4fcd01f748f780f03f34bc51730ddf728ade /Year 2/Structures and data algorithms/Project/exc/main.cpp
uni-projects-77df09397e7fc517dd387d8e46d993ad30f69853.tar
uni-projects-77df09397e7fc517dd387d8e46d993ad30f69853.tar.gz
uni-projects-77df09397e7fc517dd387d8e46d993ad30f69853.zip
Parentb6e654657da058e9a438ec4583fac8028758dfb4 (diff)
[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.cpp69
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;
+ }
}