1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
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();
}
|