aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSyndamia <kamen@syndamia.com>2024-04-22 17:30:29 +0300
committerSyndamia <kamen@syndamia.com>2024-04-22 17:30:29 +0300
commitf4642325f58172e85b9e41e35f69e8bea46f78c6 (patch)
tree038377e89be336cbb91469dd526cebbfcfe02df5
parentf3406754705e508f57768d3d0d8e457ff5b7620c (diff)
downloadoop-2023-solutions-f4642325f58172e85b9e41e35f69e8bea46f78c6.tar
oop-2023-solutions-f4642325f58172e85b9e41e35f69e8bea46f78c6.tar.gz
oop-2023-solutions-f4642325f58172e85b9e41e35f69e8bea46f78c6.zip
[w9] Added solutions to ex 1-2
-rw-r--r--week09/Exercise1/Printer.cpp50
-rw-r--r--week09/Exercise1/Printer.h15
-rw-r--r--week09/Exercise2/Date.cpp71
-rw-r--r--week09/Exercise2/Date.h18
-rw-r--r--week09/Exercise2/main.cpp29
5 files changed, 183 insertions, 0 deletions
diff --git a/week09/Exercise1/Printer.cpp b/week09/Exercise1/Printer.cpp
new file mode 100644
index 0000000..b8e6195
--- /dev/null
+++ b/week09/Exercise1/Printer.cpp
@@ -0,0 +1,50 @@
+#include "Printer.h"
+#include <cstring>
+
+void Printer::free() {
+ delete[] model;
+}
+
+void Printer::copyFrom(const Printer& other) {
+ this->model = new char[strlen(other.model) + 1];
+ strcpy(this->model, other.model);
+ this->printedPages = other.printedPages;
+}
+
+Printer::Printer() {
+ model = nullptr;
+ printedPages = 0;
+}
+
+Printer::~Printer() {
+ free();
+}
+
+Printer::Printer(const Printer& other) {
+ copyFrom(other);
+}
+
+Printer& Printer::operator=(const Printer& other) {
+ if (this != &other) {
+ free();
+ copyFrom(other);
+ }
+ return *this;
+}
+
+Printer::Printer(Printer&& other) {
+ this->model = other.model;
+ other.model = nullptr;
+ this->printedPages = other.printedPages;
+}
+
+Printer& Printer::operator=(Printer&& other) {
+ if (this != &other) {
+ free();
+
+ this->model = other.model;
+ other.model = nullptr;
+ this->printedPages = other.printedPages;
+ }
+ return *this;
+}
diff --git a/week09/Exercise1/Printer.h b/week09/Exercise1/Printer.h
new file mode 100644
index 0000000..52a737e
--- /dev/null
+++ b/week09/Exercise1/Printer.h
@@ -0,0 +1,15 @@
+class Printer {
+ char* model;
+ unsigned printedPages;
+
+ void free();
+ void copyFrom(const Printer& other);
+
+public:
+ Printer();
+ ~Printer();
+ Printer(const Printer& other);
+ Printer& operator=(const Printer& other);
+ Printer(Printer&& other);
+ Printer& operator=(Printer&& other);
+};
diff --git a/week09/Exercise2/Date.cpp b/week09/Exercise2/Date.cpp
new file mode 100644
index 0000000..22cb5de
--- /dev/null
+++ b/week09/Exercise2/Date.cpp
@@ -0,0 +1,71 @@
+#include "Date.h"
+#include <iostream>
+#include <fstream>
+
+Date::Date() {
+ day = month = year = 0;
+}
+
+Date::Date(unsigned day, unsigned month, unsigned year) {
+ this->day = day;
+ this->month = month;
+ this->year = year;
+}
+
+void Date::print() {
+ std::cout << day << " " << month << " " << year << std::endl;
+}
+
+void Date::StoreText(const char* outFileName) {
+ std::ofstream outFile(outFileName, std::ios::app);
+ if (!outFile.is_open()) {
+ throw "Couldn't open file!";
+ }
+
+ outFile << year << " " << month << " " << day << std::endl;
+
+ outFile.close();
+}
+
+void Date::StoreBinary(const char* outFileName) {
+ std::ofstream outFile(outFileName, std::ios::binary | std::ios::app);
+ if (!outFile.is_open()) {
+ throw "Couldn't open file!";
+ }
+
+ outFile.write((const char*)&year, sizeof(year));
+ outFile.write((const char*)&month, sizeof(month));
+ outFile.write((const char*)&day, sizeof(day));
+
+ outFile.close();
+}
+
+void Date::LoadText(const char* inFileName) {
+ std::fstream file(inFileName, std::ios::in | std::ios::out);
+ if (!file.is_open()) {
+ throw "Couldn't open file!";
+ }
+
+ file >> year >> month >> day;
+
+ while (!file.fail()) {
+ file.put(' ');
+ file.seekg(-2, std::ios::cur);
+ }
+ file.clear();
+
+ file.close();
+}
+
+void Date::LoadBinary(const char* inFileName) {
+ std::ifstream inFile(inFileName);
+ if (!inFile.is_open()) {
+ throw "Couldn't open file!";
+ }
+
+ inFile.read((char*)&year, sizeof(year));
+ inFile.read((char*)&month, sizeof(month));
+ inFile.read((char*)&day, sizeof(day));
+
+ inFile.close();
+}
diff --git a/week09/Exercise2/Date.h b/week09/Exercise2/Date.h
new file mode 100644
index 0000000..d280c71
--- /dev/null
+++ b/week09/Exercise2/Date.h
@@ -0,0 +1,18 @@
+#pragma once
+
+class Date {
+ unsigned day;
+ unsigned month;
+ unsigned year;
+
+public:
+ Date();
+ Date(unsigned day, unsigned month, unsigned year);
+
+ void print();
+
+ void StoreText(const char* outFileName);
+ void StoreBinary(const char* outFileName);
+ void LoadText(const char* inFileName);
+ void LoadBinary(const char* inFileName);
+};
diff --git a/week09/Exercise2/main.cpp b/week09/Exercise2/main.cpp
new file mode 100644
index 0000000..981e252
--- /dev/null
+++ b/week09/Exercise2/main.cpp
@@ -0,0 +1,29 @@
+#include "Date.h"
+
+int main() {
+ Date d1(5, 12, 2012);
+ Date d2(18, 3, 1999);
+ Date d3(10, 1, 2020);
+ Date d4(30, 7, 1975);
+ Date d5(2, 8, 1300);
+
+ d1.StoreText("dates.txt");
+ d2.StoreText("dates.txt");
+ d3.StoreText("dates.txt");
+ d4.StoreText("dates.txt");
+ d5.StoreText("dates.txt");
+
+ Date nd1, nd2, nd3, nd4, nd5;
+
+ nd1.LoadText("dates.txt");
+ nd2.LoadText("dates.txt");
+ nd3.LoadText("dates.txt");
+ nd4.LoadText("dates.txt");
+ nd5.LoadText("dates.txt");
+
+ nd5.print();
+ nd4.print();
+ nd3.print();
+ nd2.print();
+ nd1.print();
+}