Website Summary Files Commits Branches @commit @diff @stats
path: root / Sem. 04 / task1 / uni-task.cpp
blob: 0b534c08412e50f134c4b032c264964c8e81b701 (plain) (blame)
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
#include <iostream>
#include<cstring>
#include<fstream>
#pragma warning(disable : 4996)

struct Student 
{
	char* name;
	int fn;
	int gradesCount;
	double averageGrade;
};

struct University 
{
	char name[32];
	Student* collection;
	size_t studentsCount;
};

void print(const Student& st) 
{
	std::cout << std::endl << "======STUDENT======" << std::endl;
	std::cout << "Name: " << st.name << std::endl << "Faculcy number:" << st.fn <<
		std::endl << "Grades: " << st.gradesCount << std::endl << "Avarage: " << st.averageGrade << std::endl;
}

Student createStudent(const char* name, int fn, int gradesCount, double avGrade) 
{
	Student obj;

	size_t nameLen = strlen(name);

	obj.name = new char[nameLen + 1];
	strcpy(obj.name, name);

	obj.fn = fn;
	obj.gradesCount = gradesCount;
	obj.averageGrade = avGrade;

	return obj;
}

void freeStudent(Student& s) 
{
	delete[] s.name;

	s.averageGrade = s.fn = s.gradesCount = 0;

}

void saveStudentToFile(std::ofstream& f, const Student& st) 
{
	size_t nameLen = strlen(st.name);

	f.write((const char*)&nameLen, sizeof(nameLen));  //first we write the size of the name!
	f.write(st.name, nameLen);

	f.write((const char*)&st.fn, sizeof(st.fn));
	f.write((const char*)&st.gradesCount, sizeof(st.gradesCount));
	f.write((const char*)&st.averageGrade, sizeof(st.averageGrade));
}

Student readStudentFromFile(std::ifstream& f) 
{
	Student res;

	size_t nameLen;

	f.read((char*)&nameLen, sizeof(nameLen)); //first we read the size of the name!

	res.name = new char[nameLen + 1];
	f.read(res.name, nameLen);
	res.name[nameLen] = '\0';

	f.read((char*)&res.fn, sizeof(res.fn));
	f.read((char*)&res.gradesCount, sizeof(res.gradesCount));
	f.read((char*)&res.averageGrade, sizeof(res.averageGrade));

	return res;
}

University createUniversity(const char* _name, const Student* collection, const size_t& studentCount) 
{
	University uni;

	strcpy(uni.name, _name);
	uni.collection = new Student[studentCount];
	uni.studentsCount = studentCount;

	for (size_t i = 0; i < studentCount; i++) 
	{
		uni.collection[i] = collection[i];
	}

	return uni;
}

void saveUniversityToFile(std::ofstream& outStream, const University& Uni) 
{
	//Writing the name of the university first:
	outStream.write(Uni.name, 32);

	//Then we write the students count:
	outStream.write((const char*)(&Uni.studentsCount), sizeof(Uni.studentsCount));

	//Writing every single student:
	size_t count = Uni.studentsCount;
	for (size_t i = 0; i < count; i++) 
	{
		saveStudentToFile(outStream, Uni.collection[i]);
	}
}

University readUniFromFile(std::ifstream& inFile) 
{
	//to return:
	University uni;

	//We follow the same read order as we wrote them:

	//name:
	inFile.read((char*)(&uni.name), 32);

	//count:
	inFile.read((char*)(&uni.studentsCount), sizeof(uni.studentsCount));

	//students:
	size_t studentsCount = uni.studentsCount;
	uni.collection = new Student[studentsCount];

	for (size_t i = 0; i < studentsCount; i++) 
	{
		uni.collection[i] = readStudentFromFile(inFile);
	}

	return uni;
}

void printUni(const University& uni) 
{
	size_t studentsCount = uni.studentsCount;

	std::cout << std::endl << "----------" << uni.name << "----------";

	for (size_t i = 0; i < studentsCount; i++)
	{
		print(uni.collection[i]);
		std::cout << std::endl;
	}
	std::cout << "-------------------------";
}

void freeUni(University& uni) 
{
	for (int i = 0; i < uni.studentsCount; i++)
		freeStudent(uni.collection[i]);

	strcpy(uni.name, "");
	uni.studentsCount = 0;
}

void changeStudentName(Student& st, const char* newName)
{
	delete[] st.name;
	st.name = new char[strlen(newName) + 1];
	strcpy(st.name, newName);
}

void changeName(University* uniCollection, const int& uniCount, const char* uni, const int& fn, const char* newName) 
{
	int uniFound = -1;
	int studentFound = -1;

	for (int i = 0; i < uniCount; i++) 
	{
		if (strcmp(uniCollection[i].name, uni) != 0) 
		{
			continue;
		}


		uniFound = i;
		size_t studentsCount = uniCollection[i].studentsCount;

		for (size_t j = 0; j < studentsCount; j++) 
		{
			if (uniCollection[i].collection[j].fn != fn)
			{
				continue;
			}

			studentFound = j;
			changeStudentName(uniCollection[i].collection[j], newName);
			break;
		}
		break;
	}

	if (uniFound != -1 && studentFound != -1) 
	{
		std::cout << std::endl << "Changed: " << std::endl;
		print(uniCollection[uniFound].collection[studentFound]);
	}
	else 
	{
		std::cout << std::endl << "No such student in this university" << std::endl;
	}
}

void addStudentGrade(Student& st, const int& grade)
{
	double newAvarageGrade = (double)((st.averageGrade * st.gradesCount) + grade) / (double)(st.gradesCount + 1);
	st.averageGrade = newAvarageGrade;
	st.gradesCount++;
}

void addGrade(University* uniCollection, const int& uniCount, const char* uni, const int& fn, const int& grade) 
{
	int uniFound = -1;
	int studentFound = -1;

	for (int i = 0; i < uniCount; i++)
	{
		if (strcmp(uniCollection[i].name, uni) != 0) 
		{
			continue;
		}

		uniFound = i;
		size_t studentsCount = uniCollection[i].studentsCount;

		for (size_t j = 0; j < studentsCount; j++) 
		{
			if (uniCollection[i].collection[j].fn != fn)
			{
				continue;
			}

			studentFound = j;
			addStudentGrade(uniCollection[i].collection[j], grade);
			break;
		}
		break;
	}

	if (uniFound != -1 && studentFound != -1) 
	{
		std::cout << std::endl << "Added: " << std::endl;
		print(uniCollection[uniFound].collection[studentFound]);
	}
	else
	{
		std::cout << std::endl << "No such student in this university" << std::endl;
	}
}

double getSuccess(const University uni) 
{
	double avarageGrade = 0;
	size_t studentsCount = uni.studentsCount;

	for (size_t i = 0; i < studentsCount; i++) 
	{
		avarageGrade += uni.collection[i].averageGrade;
	}

	avarageGrade = avarageGrade / (double)(studentsCount);

	return avarageGrade;
}

void swapUniversities(University* uniCollection, const int& first, const int& second) 
{
	University temp = uniCollection[first];
	uniCollection[first] = uniCollection[second];
	uniCollection[second] = temp;
}

void sort(University* uniCollection, size_t unisCount) 
{
	//selection sort
	for (int pos = 0; pos < unisCount - 1; pos++) 
	{
		int minimalIndex = pos;
		
		for (int i = pos; i < unisCount; i++) 
		{
			int currSuccess = getSuccess(uniCollection[minimalIndex]);
			if (getSuccess(uniCollection[i]) > currSuccess) 
			{
				minimalIndex = i;
			}
		}

		if (minimalIndex != pos)
			swapUniversities(uniCollection, pos, minimalIndex);
	}
}

int main() 
{
	//simple example:

	//creating university database:---------------
	University* uniCollection = new University[3];

	//FMI
	Student collection[3];
	collection[0] = createStudent("Angel", 81111, 6, 3);
	collection[1] = createStudent("Dimitar", 82222, 6, 2);
	collection[2] = createStudent("Anton", 83333, 6, 2);

	University FMI = createUniversity("FMI", collection, 3);

	//TU
	Student collectionTwo[4];
	collectionTwo[0] = createStudent("Vesko", 31244, 6, 6);
	collectionTwo[1] = createStudent("Ivan", 42342, 6, 6);
	collectionTwo[2] = createStudent("Mariano", 13433, 6, 6);
	collectionTwo[3] = createStudent("Gabriela", 43234, 6, 6);

	University TU = createUniversity("TU", collectionTwo, 4);

	//UNWE
	Student collectionThree[5];
	collectionThree[0] = createStudent("Chavdar", 13579, 6, 4);
	collectionThree[1] = createStudent("Stoino", 24680, 6, 4);
	collectionThree[2] = createStudent("Miss Playmate", 24567, 6, 4);
	collectionThree[3] = createStudent("ex fmi student", 12343, 6, 4);
	collectionThree[4] = createStudent("bukvalno vseki", 13333, 6, 5);

	University UNWE = createUniversity("UNWE", collectionThree, 5);

	uniCollection[0] = FMI;
	uniCollection[1] = TU;
	uniCollection[2] = UNWE;
	//-------------------------------------------------------------

	//functions:
	std::ofstream outFile("path.txt", std::ios::binary);
	saveUniversityToFile(outFile, uniCollection[1]);
	if (outFile.good() == 0)
	{
		std::cout << "Error saving the file";
	}

	outFile.close();

	std::ifstream inFile("path.txt", std::ios::binary);
	University example = readUniFromFile(inFile);

	if (inFile.good() == 0) 
	{
		std::cout << "Error reading the file";
	}

	inFile.close();

	printUni(example);

	changeName(uniCollection, 3, "FMI", 83333, "Antoan");
	addGrade(uniCollection, 3, "FMI", 81111, 6);

	sort(uniCollection, 3);

	std::cout << "\nSorted:\n";

	for (int i = 0; i < 3; i++)
	{
		std::cout << i + 1 << ". " << uniCollection[i].name << std::endl;
	}

	for (int i = 0; i < 3; i++) 
	{
		freeUni(uniCollection[i]);
	}
}