aboutsummaryrefslogtreecommitdiff
path: root/week09
diff options
context:
space:
mode:
authorSyndamia <kamen@syndamia.com>2024-05-07 22:17:32 +0300
committerSyndamia <kamen@syndamia.com>2024-05-07 22:17:32 +0300
commitcbd6d3aa606b18dde3f203e6edfab04935bbbd59 (patch)
treeca4288a359e7061ce74f16f3e8f05f7d40ade95f /week09
parent88f15e35713c9632216931d26443dc588238732f (diff)
downloadoop-2023-solutions-cbd6d3aa606b18dde3f203e6edfab04935bbbd59.tar
oop-2023-solutions-cbd6d3aa606b18dde3f203e6edfab04935bbbd59.tar.gz
oop-2023-solutions-cbd6d3aa606b18dde3f203e6edfab04935bbbd59.zip
[w9] Added rough solution to ex 3
Diffstat (limited to 'week09')
-rw-r--r--week09/Exercise3/Train.cpp132
-rw-r--r--week09/Exercise3/Train.h29
-rw-r--r--week09/Exercise3/TrainNetwork.cpp73
-rw-r--r--week09/Exercise3/TrainNetwork.h12
-rw-r--r--week09/Exercise3/main.cpp8
5 files changed, 254 insertions, 0 deletions
diff --git a/week09/Exercise3/Train.cpp b/week09/Exercise3/Train.cpp
new file mode 100644
index 0000000..68d4206
--- /dev/null
+++ b/week09/Exercise3/Train.cpp
@@ -0,0 +1,132 @@
+#include "Train.h"
+#include <iostream>
+#include <cstring>
+#include <fstream>
+
+void Train::free() {
+ delete[] regionsFileName;
+ delete[] currentRegion;
+}
+
+void Train::copyFrom(const Train& other) {
+ strcpy(this->model, other.model);
+ this->railID = other.railID;
+ this->regionsFileName = new char[strlen(other.regionsFileName) + 1];
+ strcpy(this->regionsFileName, other.regionsFileName);
+ this->currentRegion = new char[strlen(other.currentRegion) + 1];
+ strcpy(this->currentRegion, other.currentRegion);
+}
+
+Train::Train() {
+ model[0] = '\0';
+ railID = 0;
+ regionsFileName = currentRegion = nullptr;
+}
+
+Train::~Train() {
+ free();
+}
+
+Train::Train(const Train& other) {
+ copyFrom(other);
+}
+
+Train& Train::operator=(const Train& other) {
+ if (this != &other) {
+ free();
+ copyFrom(other);
+ }
+ return *this;
+}
+
+Train::Train(Train&& other) {
+ strcpy(this->model, other.model);
+ this->railID = other.railID;
+
+ this->regionsFileName = other.regionsFileName;
+ other.regionsFileName = nullptr;
+ this->currentRegion = other.currentRegion;
+ other.currentRegion = nullptr;
+}
+
+void Train::setCurrentRegion(std::ifstream& inFile) {
+ int start = inFile.tellg();
+ while (!inFile.eof() && inFile.peek() != '\n') {
+ inFile.get();
+ }
+ inFile.clear();
+
+ int length = inFile.tellg();
+ this->currentRegion = new char[length + 1];
+
+ inFile.seekg(start, std::ios::beg);
+ inFile.getline(this->currentRegion, length + 1);
+}
+
+Train::Train(const char model[128], unsigned railID, const char* regionsFileName) {
+ strcpy(this->model, model);
+ this->railID = railID;
+ this->regionsFileName = new char[strlen(regionsFileName) + 1];
+ strcpy(this->regionsFileName, regionsFileName);
+
+ std::ifstream inFile(regionsFileName);
+ if (!inFile.is_open()) {
+ throw "Couldn't open regions file!";
+ }
+
+ setCurrentRegion(inFile);
+
+ inFile.close();
+}
+
+// Обикаля сегашния ред във файла и сравнява всяка буква със съответната от str
+// Във всички случаи предвижва inFile напред към следващия ред
+bool fileStringEq(std::ifstream& inFile, const char* str) {
+ if (inFile.eof()) {
+ inFile.close();
+ throw -1;
+ }
+
+ bool equal = true;
+ while (!inFile.eof() && inFile.peek() != '\n') {
+ if (inFile.peek() != *str) {
+ equal = false;
+ }
+ inFile.get();
+ if (*str != '\0') str++;
+ }
+
+ if (!inFile.eof()) {
+ inFile.get();
+ }
+ return equal && *str == '\0';
+}
+
+void Train::TransferNextRegion() {
+ std::ifstream inFile(regionsFileName);
+ if (!inFile.is_open()) {
+ throw "Couldn't open regions file!";
+ }
+
+ while (!fileStringEq(inFile, currentRegion));
+ setCurrentRegion(inFile);
+
+ inFile.close();
+}
+
+void Train::SwitchRailLine(unsigned railID, const char* regionsFileName) {
+ this->railID = railID;
+ delete[] this->regionsFileName;
+
+ this->regionsFileName = new char[strlen(regionsFileName) + 1];
+ strcpy(this->regionsFileName, regionsFileName);
+
+ std::ifstream inFile(regionsFileName);
+ if (!inFile.is_open()) {
+ throw "Couldn't open regions file!";
+ }
+
+ setCurrentRegion(inFile);
+
+ inFile.close();
+}
diff --git a/week09/Exercise3/Train.h b/week09/Exercise3/Train.h
new file mode 100644
index 0000000..6b90477
--- /dev/null
+++ b/week09/Exercise3/Train.h
@@ -0,0 +1,29 @@
+#pragma once
+#include <iostream>
+
+class Train {
+ char model[128];
+ unsigned railID;
+ char* regionsFileName;
+ char* currentRegion;
+
+ void free();
+ void copyFrom(const Train& other);
+
+ void setCurrentRegion(std::ifstream& inFile);
+
+public:
+ Train();
+ ~Train();
+ Train(const Train& other);
+ Train& operator=(const Train& other);
+ Train(Train&& other);
+ Train& operator=(Train&& other);
+
+ Train(const char model[127], unsigned railID, const char* regionsFileName);
+
+ void TransferNextRegion();
+ void SwitchRailLine(unsigned railID, const char* regionsFileName);
+
+ friend class TrainNetwork;
+};
diff --git a/week09/Exercise3/TrainNetwork.cpp b/week09/Exercise3/TrainNetwork.cpp
new file mode 100644
index 0000000..cf9b6a9
--- /dev/null
+++ b/week09/Exercise3/TrainNetwork.cpp
@@ -0,0 +1,73 @@
+#include "TrainNetwork.h"
+#include <cstring>
+#include <fstream>
+
+bool TrainNetwork::HaveCollided(int index1, int index2) {
+ if (trains[index1].railID != trains[index2].railID) {
+ return false;
+ }
+ if (strcmp(trains[index1].currentRegion, trains[index2].currentRegion) != 0) {
+ return false;
+ }
+
+ std::ifstream regionFile(trains[index1].regionsFileName);
+ if (!regionFile.is_open()) {
+ throw "Couldn't open region file!";
+ }
+
+ // Обикаляме файла от края към началото и сравняваме всяка буква със съответната от сегашния регион
+ // Тоест сравняваме последния ред във файла със сегашния
+ regionFile.seekg(-1, std::ios::end); // Често текстови редактори слагат едно \n в края на файла, затова го пропускаме
+ // Това и да го направите и да не го е все едно
+ int regionIndex = strlen(trains[index1].currentRegion) - 1;
+ while (!regionFile.fail() && regionFile.peek() != '\n' && regionIndex >= 0) {
+ // Щом последния ред (последната локация) се различава от сегашната, то
+ // сегашната ще се намира някъде другаде във файла и ще има колизия
+ if (regionFile.peek() != trains[index1].currentRegion[regionIndex]) {
+ regionFile.close();
+ return true;
+ }
+ regionIndex--;
+ regionFile.seekg(-1, std::ios::cur);
+ }
+
+ bool lastLineEqualsCurrent = (regionFile.tellg() == -1 || regionFile.peek() == '\n') && regionIndex == 0;
+ regionFile.close();
+
+ return !lastLineEqualsCurrent;
+}
+
+int TrainNetwork::RunTrains() {
+ bool anyMovedForward = true;
+ while (anyMovedForward) {
+ anyMovedForward = false;
+
+ for (int i = 0; i < 512; i++) {
+ if (collided[i]) continue;
+
+ for (int j = 0; j < 512; j++) {
+ if (collided[j] || j == i) continue;
+
+ if (HaveCollided(i, j)) {
+ collided[i] = collided[j] = true;
+ break;
+ }
+ }
+
+ if (collided[i]) continue;
+
+ try {
+ trains[i].TransferNextRegion();
+ anyMovedForward = true;
+ }
+ catch (int error) {
+ }
+ }
+ }
+
+ int sum = 0;
+ for (int i = 0; i < 512; i++) {
+ sum += collided[i];
+ }
+ return sum;
+}
diff --git a/week09/Exercise3/TrainNetwork.h b/week09/Exercise3/TrainNetwork.h
new file mode 100644
index 0000000..2d40879
--- /dev/null
+++ b/week09/Exercise3/TrainNetwork.h
@@ -0,0 +1,12 @@
+#pragma once
+#include "Train.h"
+
+class TrainNetwork {
+ char id[8];
+ Train trains[512];
+ bool collided[512];
+
+public:
+ bool HaveCollided(int index1, int index2);
+ int RunTrains();
+};
diff --git a/week09/Exercise3/main.cpp b/week09/Exercise3/main.cpp
new file mode 100644
index 0000000..d9ebd6f
--- /dev/null
+++ b/week09/Exercise3/main.cpp
@@ -0,0 +1,8 @@
+#include "Train.h"
+
+int main() {
+ Train t("abc", 1, "train.txt");
+ t.TransferNextRegion();
+ t.TransferNextRegion();
+ t.TransferNextRegion();
+}