diff options
Added todays oop practice
1 files changed, 155 insertions, 0 deletions
| -rw-r--r-- | Year 1/Object-oriented Programming/04-Practice-15.02.2022.cpp | 155 |
diff --git a/Year 1/Object-oriented Programming/04-Practice-15.02.2022.cpp b/Year 1/Object-oriented Programming/04-Practice-15.02.2022.cpp new file mode 100644 index 0000000..4434162 --- /dev/null +++ b/Year 1/Object-oriented Programming/04-Practice-15.02.2022.cpp @@ -0,0 +1,155 @@ +#include <iostream> +#include <fstream> +#include <cstring> + +// Exercise 1 + +int fileSize(std::ifstream& file) { + int currPos = file.tellg(); + file.seekg(0, std::ios::end); + int size = file.tellg(); + file.seekg(currPos, std::ios::beg); + return size; +} + +void duplicate(const char* filePath, const char* newFile, int n) { + std::ifstream inFile(filePath); + int inFileSize = fileSize(inFile); + + std::ofstream outFile(newFile); + for (int i = 0; i < inFileSize * n; i++) { + outFile.put(inFile.get()); + + if (inFile.tellg() == inFileSize) + inFile.seekg(0, std::ios::beg); + } + + outFile.close(); + inFile.close(); +} + +void ex1() { + duplicate("dupeSrc.txt", "dupeDest.txt", 3); +} + +// Exercise 2 + +struct Task { + private: + char* name; + char* description; + int points; + + void setStringProp(char*& prop, const char* newVal, int valSize) { + delete[] prop; + prop = new char[valSize]; + for (int i = 0; i < valSize; i++) + prop[i] = newVal[i]; + } + + public: + Task() { + Task("", "", 0); + } + Task(const char* name, const char* desc, int points) { + this->set_name(name, strlen(name)); + this->set_description(desc, strlen(desc)); + this->set_points(points); + } + + ~Task() { + delete[] this->name; + delete[] this->description; + } + + void writeToFile(std::ofstream& file) { + file.write(this->name, sizeof(this->name)); + file.write(this->description, sizeof(this->description)); + file.write((const char*)&this->points, sizeof(this->points)); + } + +#pragma region + void set_name(const char* newName, int newNameSize) { + this->setStringProp(this->name, newName, newNameSize); + } + const char* get_name() const { + return this->name; + } + + void set_description(const char* newDesc, int newDescSize) { + this->setStringProp(this->description, newDesc, newDescSize); + } + const char* get_description() const { + return this->description; + } + + void set_points(int points) { + this->points = points; + } + int get_points() const { + return this->points; + } +#pragma endregion +}; + +const int EXAM_TASK_CNT = 3; + +struct Exam { + private: + Task tasks[EXAM_TASK_CNT]; + int passingPoints; + + public: + Exam(const Task* elems) { + for (int i = 0; i < EXAM_TASK_CNT; i++) + this->tasks[i] = elems[i]; + } + + void writeToFile(std::ofstream& file) { + file.write((const char*)this, sizeof(Exam)); + } + void writeToFile(const char* fileName) { + std::ofstream file(fileName, std::ios::binary); + this->writeToFile(file); + file.close(); + } + + void readFromFile(std::ifstream& file) { + // for (int i = 0; i < EXAM_TASK_CNT; i++) + // file.read(this->tasks[i],sizeof(this->tasks)); + } + void readFromFile(const char* fileName) { + std::ifstream file(fileName, std::ios::binary); + this->readFromFile(file); + file.close(); + } + + void set_passingPoints(int points) { + this->passingPoints = points; + } + int get_passingPoints() const { + return this->passingPoints; + } + + int getMax() { + int acc = 0; + for (int i = 0; i < EXAM_TASK_CNT; i++) + acc += this->tasks[i].get_points(); + return acc; + } +}; + +void writeToFile(const char* fileName, Exam& exam) { + std::ofstream file(fileName); + +} + +void ex2() { + Task t1("Wow1", "Bruh1", 5), t2("Wow2", "Bruh2", 5), t3("Wow3", "Bruh3", 5), tasks[EXAM_TASK_CNT] = { t1, t2, t3 }; + Exam ex(tasks); + ex.writeToFile("test.txt"); +} + +int main() { + ex2(); +} |
