From f4642325f58172e85b9e41e35f69e8bea46f78c6 Mon Sep 17 00:00:00 2001 From: Syndamia Date: Mon, 22 Apr 2024 17:30:29 +0300 Subject: [w9] Added solutions to ex 1-2 --- week09/Exercise1/Printer.cpp | 50 +++++++++++++++++++++++++++++++ week09/Exercise1/Printer.h | 15 ++++++++++ week09/Exercise2/Date.cpp | 71 ++++++++++++++++++++++++++++++++++++++++++++ week09/Exercise2/Date.h | 18 +++++++++++ week09/Exercise2/main.cpp | 29 ++++++++++++++++++ 5 files changed, 183 insertions(+) create mode 100644 week09/Exercise1/Printer.cpp create mode 100644 week09/Exercise1/Printer.h create mode 100644 week09/Exercise2/Date.cpp create mode 100644 week09/Exercise2/Date.h create mode 100644 week09/Exercise2/main.cpp (limited to 'week09') 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 + +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 +#include + +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(); +} -- cgit v1.2.3