Summary Files Commits Branches @commit @diff @stats
path: root / Year 1 / Object-oriented Programming / 04-Practice-15.02.2022.cpp
diff options
context:
space:
mode:
Commitc14dbd632f05225eab2a51ca982f1b63b7ac744a (patch)
Author
 Syndamia <kamen.d.mladenov@protonmail.com>
2022-03-16 17:19:03 +0200
Committer
 Syndamia <kamen.d.mladenov@protonmail.com>
2022-03-16 17:19:03 +0200
Files086a1a2f274bec1055190d057be9d1df94a2e691 /Year 1/Object-oriented Programming/04-Practice-15.02.2022.cpp
uni-projects-c14dbd632f05225eab2a51ca982f1b63b7ac744a.tar
uni-projects-c14dbd632f05225eab2a51ca982f1b63b7ac744a.tar.gz
uni-projects-c14dbd632f05225eab2a51ca982f1b63b7ac744a.zip
Parent2a14dc9db8696067c467b60f0a96527f5f10ea52 (diff)
Finished 2nd exercise from yesterdays practice
1 files changed, 68 insertions, 28 deletions
-rw-r--r--Year 1/Object-oriented Programming/04-Practice-15.02.2022.cpp96
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
index 4434162..2b3912b 100644
--- a/Year 1/Object-oriented Programming/04-Practice-15.02.2022.cpp
+++ b/Year 1/Object-oriented Programming/04-Practice-15.02.2022.cpp
@@ -36,24 +36,30 @@ void ex1() {
struct Task {
private:
- char* name;
- char* description;
+ char* name = nullptr;
+ size_t nameLen;
+ char* description = nullptr;
+ size_t descriptionLen;
int points;
- void setStringProp(char*& prop, const char* newVal, int valSize) {
+ void setStringProp(char*& prop, size_t& propLen, const char* newVal, int valSize) {
delete[] prop;
+
prop = new char[valSize];
for (int i = 0; i < valSize; i++)
prop[i] = newVal[i];
+ propLen = valSize;
}
public:
Task() {
- Task("", "", 0);
+ this->set_name("", 1);
+ this->set_description("", 1);
+ this->set_points(0);
}
Task(const char* name, const char* desc, int points) {
- this->set_name(name, strlen(name));
- this->set_description(desc, strlen(desc));
+ this->set_name(name, strlen(name) + 1); // +1 so we also capture the '\0'
+ this->set_description(desc, strlen(desc) + 1); // +1 so we also capture the '\0'
this->set_points(points);
}
@@ -62,34 +68,60 @@ struct Task {
delete[] this->description;
}
- void writeToFile(std::ofstream& file) {
- file.write(this->name, sizeof(this->name));
- file.write(this->description, sizeof(this->description));
+ void writeToFile(std::ofstream& file) const {
+ file.write((const char*)&this->nameLen, sizeof(this->nameLen));
+ file.write(this->name, nameLen);
+ file.write((const char*)&this->descriptionLen, sizeof(this->descriptionLen));
+ file.write(this->description, descriptionLen);
file.write((const char*)&this->points, sizeof(this->points));
}
+ void writeToFile(const char* name) const {
+ std::ofstream file(name);
+ this->writeToFile(file);
+ file.close();
+ }
+
+ void readFromFile(std::ifstream& file) {
+ file.read((char*)&this->nameLen, sizeof(this->nameLen));
+ file.read(this->name, this->nameLen);
+ file.read((char*)&this->descriptionLen, sizeof(this->descriptionLen));
+ file.read(this->description, this->descriptionLen);
+ file.read((char*)&this->points, sizeof(this->points));
+ }
+ void readFromFile(const char* fileName) {
+ std::ifstream file(fileName);
+ this->readFromFile(file);
+ file.close();
+ }
-#pragma region
void set_name(const char* newName, int newNameSize) {
- this->setStringProp(this->name, newName, newNameSize);
+ this->setStringProp(this->name, this->nameLen, newName, newNameSize);
}
const char* get_name() const {
return this->name;
}
+ int get_nameLen() const {
+ return this->nameLen;
+ }
+
void set_description(const char* newDesc, int newDescSize) {
- this->setStringProp(this->description, newDesc, newDescSize);
+ this->setStringProp(this->description, this->descriptionLen, newDesc, newDescSize);
}
const char* get_description() const {
return this->description;
}
+ int get_descriptionLen() const {
+ return this->descriptionLen;
+ }
+
void set_points(int points) {
this->points = points;
}
int get_points() const {
return this->points;
}
-#pragma endregion
};
const int EXAM_TASK_CNT = 3;
@@ -100,23 +132,31 @@ struct Exam {
int passingPoints;
public:
- Exam(const Task* elems) {
- for (int i = 0; i < EXAM_TASK_CNT; i++)
- this->tasks[i] = elems[i];
+ Exam() {}
+ Exam(const Task* elems, int passingPoints) {
+ for (int i = 0; i < EXAM_TASK_CNT; i++) {
+ this->tasks[i].set_name(elems[i].get_name(), elems[i].get_nameLen());
+ this->tasks[i].set_description(elems[i].get_description(), elems[i].get_descriptionLen());
+ this->tasks[i].set_points(elems[i].get_points());
+ }
+ this->set_passingPoints(passingPoints);
}
- void writeToFile(std::ofstream& file) {
- file.write((const char*)this, sizeof(Exam));
+ void writeToFile(std::ofstream& file) const {
+ file.write((const char*)&this->passingPoints, sizeof(this->passingPoints));
+ for (int i = 0; i < EXAM_TASK_CNT; i++)
+ this->tasks[i].writeToFile(file);
}
- void writeToFile(const char* fileName) {
+ void writeToFile(const char* fileName) const {
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));
+ file.read((char*)&this->passingPoints, sizeof(this->passingPoints));
+ for (int i = 0; i < EXAM_TASK_CNT; i++)
+ this->tasks[i].readFromFile(file);
}
void readFromFile(const char* fileName) {
std::ifstream file(fileName, std::ios::binary);
@@ -139,15 +179,15 @@ struct Exam {
}
};
-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);
+ Task tasks[EXAM_TASK_CNT] = { Task("Wow1", "Bruh1", 1), Task("Wow2", "Bruh2", 2), Task("Wow3", "Bruh3", 3) };
+ Exam ex(tasks, 4);
ex.writeToFile("test.txt");
+
+ Exam ex1;
+ ex1.readFromFile("test.txt");
+ ex1.writeToFile("test1.txt");
+ std::cout << ex1.getMax() << std::endl;
}
int main() {