aboutsummaryrefslogtreecommitdiff
path: root/week09/Exam1/Drone.cpp
diff options
context:
space:
mode:
authorSyndamia <kamen@syndamia.com>2024-04-22 13:52:55 +0300
committerSyndamia <kamen@syndamia.com>2024-04-22 13:52:55 +0300
commitf7ed9a8a6c31b17d54d2f37cc0f12b64f8e4b6d2 (patch)
tree866abf46779f63a2fd1c8cfc70f7ff5f3bc11752 /week09/Exam1/Drone.cpp
parenta2e284b0056075e2365deaa2455be567c3b3c945 (diff)
downloadoop-2023-solutions-f7ed9a8a6c31b17d54d2f37cc0f12b64f8e4b6d2.tar
oop-2023-solutions-f7ed9a8a6c31b17d54d2f37cc0f12b64f8e4b6d2.tar.gz
oop-2023-solutions-f7ed9a8a6c31b17d54d2f37cc0f12b64f8e4b6d2.zip
[w9] Added exercise descriptions and solution to exam 1
Diffstat (limited to 'week09/Exam1/Drone.cpp')
-rw-r--r--week09/Exam1/Drone.cpp140
1 files changed, 140 insertions, 0 deletions
diff --git a/week09/Exam1/Drone.cpp b/week09/Exam1/Drone.cpp
new file mode 100644
index 0000000..6895a74
--- /dev/null
+++ b/week09/Exam1/Drone.cpp
@@ -0,0 +1,140 @@
+#include "Drone.h"
+#include <cstring>
+#include <fstream>
+#include <iostream>
+
+/* Голяма четворка */
+
+void Drone::free() {
+ delete[] id;
+}
+void Drone::copyFrom(const Drone& other) {
+ id = new char[strlen(other.id) + 1];
+ strcpy(id, other.id);
+ strcpy(generatedPath, other.generatedPath);
+ position = other.position;
+}
+
+Drone::Drone() {
+ id = nullptr;
+ generatedPath[0] = '\0';
+ position = 0;
+}
+Drone::~Drone() {
+ free();
+}
+Drone::Drone(const Drone& other) {
+ copyFrom(other);
+}
+Drone& Drone::operator=(const Drone& other) {
+ if (this != &other) {
+ free();
+ copyFrom(other);
+ }
+ return *this;
+}
+
+/* Move семантики */
+
+Drone::Drone(Drone&& other) {
+ id = other.id;
+ other.id = nullptr;
+
+ strcpy(generatedPath, other.generatedPath);
+ position = other.position;
+}
+
+Drone& Drone::operator=(Drone&& other) {
+ if (this != &other) {
+ free();
+
+ id = other.id;
+ other.id = nullptr;
+
+ strcpy(generatedPath, other.generatedPath);
+ position = other.position;
+ }
+ return *this;
+}
+
+/* Метод от 1.2 */
+void Drone::printGeneratedPath() {
+ std::ifstream inFile(generatedPath, std::ios::binary);
+ if (!inFile.is_open()) {
+ throw "Could not open generatedPath!"; // ок е и вместо това да се направи "return;"
+ }
+
+ while(!inFile.eof()) {
+ double x, y;
+ // Не е казано формата на този файл какъв е, затова допускаме най-простия възможен:
+ // една голяма последователност от double-и, първия е х1, втория е y1, третия е x2, четвъртия е y2 и так. нат.
+ inFile.read((char*)&x, sizeof(x));
+ inFile.read((char*)&y, sizeof(y));
+
+ std::cout << x << ", " << y << std::endl;
+ }
+
+ inFile.close();
+}
+
+/* Метод от 1.2 */
+Coordinate Drone::moveWithOneStep() {
+ std::ifstream inFile(generatedPath, std::ios::binary);
+ if (!inFile.is_open()) {
+ throw "Could not open generatedPath!"; // ок е вместо това да бъде "return;"
+ }
+
+ double x, y;
+ for (int i = 0; i <= position + 1 && !inFile.eof(); i++) {
+ inFile.read((char*)&x, sizeof(x));
+ inFile.read((char*)&y, sizeof(y));
+ }
+ // Ако във файла не съществува координат на position+1
+ if (inFile.eof()) {
+ inFile.close();
+ throw "No next position";
+ }
+
+ std::cout << x << ", " << y << std::endl;
+
+ position++;
+ inFile.close();
+ return { x, y }; // Еквивалентно на "return Coordinate({ x, y });"
+}
+
+/* Помощна за 2.2, метод play */
+// Кодът е почти еднакъв с moveWithOneStep, обаче при moveWithOneStep първо местим и връщаме новата позиция,
+// докато тук връщаме сегашната и след това местим
+// Хубаво е логиката да се изкара в някаква обща член-функция, но като за контролно така е коректно
+Coordinate Drone::positionAndMove() {
+ std::ifstream inFile(generatedPath, std::ios::binary);
+ if (!inFile.is_open()) {
+ throw "Could not open generatedPath!"; // ок е вместо това да бъде "return;"
+ }
+
+ double x, y;
+ // vvvvvvvvvvv ето тук е единствената разлика с moveWithOneStep
+ for (int i = 0; i <= position && !inFile.eof(); i++) {
+ inFile.read((char*)&x, sizeof(x));
+ inFile.read((char*)&y, sizeof(y));
+ }
+ // Ако във файла не съществува координат на position+1
+ if (inFile.eof()) {
+ inFile.close();
+ throw "No next position";
+ }
+
+ std::cout << x << ", " << y << std::endl;
+
+ position++;
+ inFile.close();
+ return { x, y }; // Еквивалентно на "return Coordinate({ x, y });"
+}
+
+/* Помощна за 3.1 */
+Drone::Drone(const char* id, char generatedPath[64], int position) {
+ this->id = new char[strlen(id)+1];
+ strcpy(this->id, id);
+ strcpy(this->generatedPath, generatedPath);
+ this->position = position;
+}