Website Summary Files Commits Branches @commit @diff @stats
path: root / pract SI
diff options
context:
space:
mode:
Commit042b00609eb951f806b13159eccf30562fa4b650 (patch)
Author
 Stoycho Kyosev <68247567+stoychoX@users.noreply.github.com>
2022-03-18 16:36:34 +0200
Committer
 GitHub <noreply@github.com>
2022-03-18 16:36:34 +0200
Filesdbab4b5754a10788551e3ce49e52e2dda1ac0273 /pract SI
Object-oriented_programming_FMI-042b00609eb951f806b13159eccf30562fa4b650.tar
Object-oriented_programming_FMI-042b00609eb951f806b13159eccf30562fa4b650.tar.gz
Object-oriented_programming_FMI-042b00609eb951f806b13159eccf30562fa4b650.zip
Parent8231ebd39951082a17dac75ac7468e18441da68f (diff)
Create solution.cpp
1 files changed, 169 insertions, 0 deletions
-rw-r--r--pract SI/week4/solution.cpp169
diff --git a/pract SI/week4/solution.cpp b/pract SI/week4/solution.cpp
new file mode 100644
index 0000000..5fd348d
--- /dev/null
+++ b/pract SI/week4/solution.cpp
@@ -0,0 +1,169 @@
+#include<iostream>
+#include<fstream>
+#include<cstring>
+
+struct task {
+ char* name;
+ char* problem;
+ int points;
+
+ task() : name(nullptr), problem(nullptr), points(0) {}
+
+ //task() {
+ // this->name = nullptr;
+ // this->problem = nullptr;
+ // this->points = 0;
+ //}
+
+ void setTaskName(const char* nName) {
+ delete[] name; // Изтриваме старото име за да избегнем отечка на памет
+
+ int length = strlen(nName); // Вземаме дължината на новото име
+ name = new char[length + 1]; // Заделяме точно колкото памет ни трябва
+
+ // Еквивалентно на strcpy, просто копираме символ по символ
+ for (int i = 0; i < length; i++)
+ name[i] = nName[i];
+ name[length] = '\0';
+ }
+
+ // Аналогично с setTaskName
+ void setTaskProblem(const char* nProblem) {
+ delete[] problem;
+
+ int length = strlen(nProblem);
+ problem = new char[length + 1];
+
+ for (int i = 0; i < length; i++)
+ problem[i] = nProblem[i];
+ problem[length] = '\0';
+ }
+
+ void writeToFile(std::ofstream& outFile) const {
+ int nameLength = strlen(name);
+ int problemLength = strlen(problem);
+
+ outFile.write((const char*)&nameLength, sizeof(nameLength));
+ outFile.write(name, nameLength);
+
+ outFile.write((const char*)&problemLength, sizeof(problemLength));
+ outFile.write(problem, problemLength);
+
+ outFile.write((const char*)&points, sizeof(points));
+ }
+
+ void readFromFile(std::ifstream& inFile) {
+ int nameLength = 0;
+ int problemLength = 0;
+
+ // Какво ще стане ако не изтрием паметта?
+ delete[] name;
+ delete[] problem;
+
+ inFile.read((char*)&nameLength, sizeof(nameLength));
+ name = new char[nameLength + 1];
+ inFile.read(name, nameLength);
+ name[nameLength] = '\0';
+
+ inFile.read((char*)&problemLength, sizeof(problemLength));
+ problem = new char[problemLength + 1];
+ inFile.read(problem, problemLength);
+ problem[problemLength] = '\0';
+
+ inFile.read((char*)&points, sizeof(points));
+ }
+
+ void setPoints(int nPoints) {
+ points = nPoints;
+ }
+
+ int getPoints() const {
+ return points;
+ }
+
+ void showTask() const {
+ std::cout << "Name: " << name << std::endl <<
+ "Problem to be solved: \n" << problem <<
+ std::endl << "Points: " << points << std::endl;
+ }
+
+ ~task() {
+ delete[] problem;
+ delete[] name;
+ }
+};
+
+const int TASKS_COUNT = 3;
+
+struct exam {
+ task data[TASKS_COUNT];
+ int minPoints;
+
+ exam() : minPoints(0) {}
+
+ void setMin(int nMin) {
+ // Може да си добавите и допълнителна валидация.
+ minPoints = nMin;
+ }
+
+ int getMaxPoints() const {
+ int result = 0;
+ for (int i = 0; i < TASKS_COUNT; i++)
+ result += data[i].getPoints();
+ return result;
+ }
+
+ void writeToFile(const char* filePath) const {
+ std::ofstream outFile(filePath, std::ios::binary);
+
+ if (!outFile)
+ return;
+
+ for (int i = 0; i < TASKS_COUNT; i++)
+ data[i].writeToFile(outFile);
+ }
+
+ void readFromFile(const char* filePath) {
+ std::ifstream inFile(filePath, std::ios::binary);
+
+ if (!inFile)
+ return;
+
+ for (int i = 0; i < TASKS_COUNT; i++)
+ data[i].readFromFile(inFile);
+ }
+
+ void showTasks() const {
+ for (int i = 0; i < TASKS_COUNT; i++)
+ data[i].showTask();
+ }
+};
+
+int main() {
+ exam OOP;
+
+ OOP.data[0].setTaskName("First task");
+ OOP.data[0].setTaskProblem("Easy problem");
+ OOP.data[0].setPoints(0x19);
+
+ OOP.data[1].setTaskName("Second task");
+ OOP.data[1].setTaskProblem("Another easy problem");
+ OOP.data[1].setPoints(0x19);
+
+ OOP.data[2].setTaskName("Third task");
+ OOP.data[2].setTaskProblem("Solvable problem");
+ OOP.data[2].setPoints(0x19);
+
+ OOP.setMin(0x4b);
+
+ std::cout << "What we created: " << std::endl;
+ OOP.showTasks();
+
+ // Saving to file
+ OOP.writeToFile("data.bat");
+
+ exam cloneOOP;
+ cloneOOP.readFromFile("data.bat");
+ std::cout << std::endl << "What we read: " << std::endl;
+ cloneOOP.showTasks();
+}