aboutsummaryrefslogtreecommitdiff
path: root/week07/Exercise5.cpp
diff options
context:
space:
mode:
authorSyndamia <kamen@syndamia.com>2024-04-04 20:11:12 +0300
committerSyndamia <kamen@syndamia.com>2024-04-04 20:11:12 +0300
commita2e284b0056075e2365deaa2455be567c3b3c945 (patch)
tree8a842d1f2ffb9f00989b39d2ab7a7ade1a858d81 /week07/Exercise5.cpp
parent44d085f265583f0e3cbef294bbe2c8e300aaa452 (diff)
downloadoop-2023-solutions-a2e284b0056075e2365deaa2455be567c3b3c945.tar
oop-2023-solutions-a2e284b0056075e2365deaa2455be567c3b3c945.tar.gz
oop-2023-solutions-a2e284b0056075e2365deaa2455be567c3b3c945.zip
[w7] Added exercise descriptions and solutions
Diffstat (limited to 'week07/Exercise5.cpp')
-rw-r--r--week07/Exercise5.cpp180
1 files changed, 180 insertions, 0 deletions
diff --git a/week07/Exercise5.cpp b/week07/Exercise5.cpp
new file mode 100644
index 0000000..8187771
--- /dev/null
+++ b/week07/Exercise5.cpp
@@ -0,0 +1,180 @@
+#include "Exercise5.h"
+#include <fstream>
+#include <iostream>
+#include <cstring>
+
+/* Private */
+
+void TransportTicket::free() {
+ delete[] source;
+ delete[] destination;
+}
+void TransportTicket::copyFrom(const TransportTicket& other) {
+ this->id = other.id;
+ this->source = new char[strlen(other.source)+1];
+ strcpy(this->source, other.source);
+ this->destination = new char[strlen(other.destination)+1];
+ strcpy(this->destination, other.destination);
+ this->price = other.price;
+}
+
+/* Public */
+
+TransportTicket::TransportTicket(unsigned id, const char* source, const char* destination, float price) {
+ this->id = id;
+ this->source = new char[strlen(source)+1];
+ strcpy(this->source, source);
+ this->destination = new char[strlen(destination)+1];
+ strcpy(this->destination, destination);
+ this->price = price;
+}
+void TransportTicket::Print() {
+ std::cout << id << " " << source << " " << destination << " " << price << std::endl;
+}
+
+TransportTicket::TransportTicket() {
+ id = price = 0;
+ source = destination = nullptr;
+}
+TransportTicket::~TransportTicket() {
+ free();
+}
+TransportTicket::TransportTicket(const TransportTicket& other) {
+ copyFrom(other);
+}
+TransportTicket& TransportTicket::operator=(const TransportTicket& other) {
+ if (this != &other) {
+ free();
+ copyFrom(other);
+ }
+ return *this;
+}
+TransportTicket::TransportTicket(TransportTicket&& other) {
+ this->id = other.id;
+ this->source = other.source;
+ this->destination = other.destination;
+ this->price = other.price;
+
+ other.source = nullptr;
+ other.destination = nullptr;
+}
+TransportTicket& TransportTicket::operator=(TransportTicket&& other) {
+ if (this != &other) {
+ free();
+
+ this->id = other.id;
+ this->source = other.source;
+ this->destination = other.destination;
+ this->price = other.price;
+
+ other.destination = nullptr;
+ other.source = nullptr;
+ }
+ return *this;
+}
+
+void TransportTicket::SaveText(const char* fileName) {
+ std::ofstream outFile(fileName);
+ if (!outFile.is_open()) {
+ return;
+ }
+
+ outFile << id << ',' << source << ',' << destination << ',' << price;
+ outFile.close();
+}
+
+unsigned distanceUntilComma(std::ifstream& inFile) {
+ unsigned start = inFile.tellg();
+ while (inFile.peek() != ',') {
+ inFile.get();
+ }
+ unsigned len = (unsigned)inFile.tellg() - start + 1;
+ inFile.seekg(start, std::ios::beg);
+ return len;
+}
+void TransportTicket::LoadText(const char* fileName) {
+ std::ifstream inFile(fileName);
+ if (!inFile.is_open()) {
+ return;
+ }
+
+ free();
+
+ inFile >> id;
+ inFile.ignore();
+
+ unsigned sourceLen = distanceUntilComma(inFile);
+ source = new char[sourceLen+1];
+ inFile.get(source, sourceLen);
+ source[sourceLen] = '\0';
+
+ inFile.ignore();
+
+ unsigned destinationLen = distanceUntilComma(inFile);
+ destination = new char[destinationLen+1];
+ inFile.get(destination, destinationLen);
+ destination[destinationLen] = '\0';
+
+ inFile.ignore();
+ inFile >> price;
+
+ inFile.close();
+}
+
+void TransportTicket::SaveBinary(const char* fileName) {
+ std::ofstream outFile(fileName, std::ios::binary);
+ if (!outFile.is_open()) {
+ return;
+ }
+
+ outFile.write((const char*)&id, sizeof(id));
+ outFile.write((const char*)&price, sizeof(price));
+
+ unsigned sourceLen = strlen(source)+1;
+ outFile.write((const char*)&sourceLen, sizeof(sourceLen));
+ outFile.write(source, sizeof(char) * sourceLen);
+
+ unsigned destinationLen = strlen(destination)+1;
+ outFile.write((const char*)&destinationLen, sizeof(destinationLen));
+ outFile.write(destination, sizeof(char) * destinationLen);
+
+ outFile.close();
+}
+void TransportTicket::LoadBinary(const char* fileName) {
+ std::ifstream inFile(fileName, std::ios::binary);
+ if (!inFile.is_open()) {
+ return;
+ }
+
+ free();
+
+ inFile.read((char*)&id, sizeof(id));
+ inFile.read((char*)&price, sizeof(price));
+
+ unsigned sourceLen = 0;
+ inFile.read((char*)&sourceLen, sizeof(sourceLen));
+ source = new char[sourceLen+1];
+ inFile.read(source, sizeof(char) * sourceLen);
+
+ unsigned destinationLen = 0;
+ inFile.read((char*)&destinationLen, sizeof(destinationLen));
+ destination = new char[destinationLen+1];
+ inFile.read(destination, sizeof(char) * destinationLen);
+
+ inFile.close();
+}
+
+int main() {
+ TransportTicket t1(193, "Sofia", "Bourgas", 15.50);
+ t1.SaveText("t1.txt");
+
+ TransportTicket t2(782, "Sofia", "Plovdiv", 8.99);
+ t2.SaveBinary("t2.dat");
+
+ TransportTicket t3;
+ t3.LoadText("t1.txt");
+ t3.Print();
+
+ t3.LoadBinary("t2.dat");
+ t3.Print();
+}