Summary Files Commits Branches @commit @diff @stats
path: root / Year 1 / Object-oriented Programming
diff options
context:
space:
mode:
Commite3ae4cd3affc234e3127d5aa69c34f1c420b964d (patch)
Author
 Syndamia <kamen.d.mladenov@protonmail.com>
2022-03-05 15:54:29 +0200
Committer
 Syndamia <kamen.d.mladenov@protonmail.com>
2022-03-05 15:54:29 +0200
Files2804784cedb61534f6fa34a7fc518f273db185d6 /Year 1/Object-oriented Programming
uni-projects-e3ae4cd3affc234e3127d5aa69c34f1c420b964d.tar
uni-projects-e3ae4cd3affc234e3127d5aa69c34f1c420b964d.tar.gz
uni-projects-e3ae4cd3affc234e3127d5aa69c34f1c420b964d.zip
Parent2d8b66aa122ad7c2c73a778160176e26a8de4ec0 (diff)
Added solutions from oop seminar 2
diff --git a/Year 1/Object-oriented Programming/Solutions Sem. 02/README.md b/Year 1/Object-oriented Programming/Solutions Sem. 02/README.md
new file mode 100644
index 0000000..21d0707
--- /dev/null
+++ b/Year 1/Object-oriented Programming/Solutions Sem. 02/README.md
@@ -0,0 +1,3 @@
+My solutions to all of the exercises from Seminar 01.
+
+[Task descriptions](https://github.com/Angeld55/Object-oriented_programming_FMI/tree/master/Sem.%2002#%D0%B7%D0%B0%D0%B4%D0%B0%D1%87%D0%B8)
diff --git a/Year 1/Object-oriented Programming/Solutions Sem. 02/exercise01.cpp b/Year 1/Object-oriented Programming/Solutions Sem. 02/exercise01.cpp
new file mode 100644
index 0000000..a9517ca
--- /dev/null
+++ b/Year 1/Object-oriented Programming/Solutions Sem. 02/exercise01.cpp
@@ -0,0 +1,22 @@
+#include <iostream> // cin, cout
+#include <fstream> // ifstream
+
+const int LINE_BUFFER_SIZE = 1024;
+
+int main() {
+ std::ifstream sourceFile("exercise01.cpp");
+
+ if (!sourceFile.is_open()) {
+ std::cout << "Error: Couldn't open file!" << std::endl;
+ return -1;
+ }
+
+ char buffer[LINE_BUFFER_SIZE];
+ while (!sourceFile.eof()) {
+ sourceFile.getline(buffer, LINE_BUFFER_SIZE);
+ std::cout << buffer;
+ if (!sourceFile.eof()) std::cout << std::endl;
+ }
+
+ sourceFile.close();
+}
diff --git a/Year 1/Object-oriented Programming/Solutions Sem. 02/exercise02.cpp b/Year 1/Object-oriented Programming/Solutions Sem. 02/exercise02.cpp
new file mode 100644
index 0000000..984a10b
--- /dev/null
+++ b/Year 1/Object-oriented Programming/Solutions Sem. 02/exercise02.cpp
@@ -0,0 +1,32 @@
+#include <iostream> // cin, cout
+#include <fstream> // ifstream, ofstream
+
+const int LINE_BUFFER_SIZE = 1024;
+const int FILEPATH_SIZE = 4096;
+
+int main() {
+ char inName[FILEPATH_SIZE], outName[FILEPATH_SIZE];
+ std::cin >> inName >> outName;
+
+ std::ifstream inFile(inName);
+ if (!inFile.is_open()) {
+ std::cout << "Error: File could not be opened!" << std::endl;
+ return -1;
+ }
+
+ std::ofstream outFile(outName);
+ if (!outFile.is_open()) {
+ std::cout << "Error: File could not be opened!" << std::endl;
+ return -1;
+ }
+
+ char buffer[LINE_BUFFER_SIZE];
+ while (!inFile.eof()) {
+ inFile.getline(buffer, LINE_BUFFER_SIZE);
+ outFile << buffer;
+ if (!inFile.eof()) outFile << std::endl;
+ }
+
+ inFile.close();
+ outFile.close();
+}
diff --git a/Year 1/Object-oriented Programming/Solutions Sem. 02/exercise03.cpp b/Year 1/Object-oriented Programming/Solutions Sem. 02/exercise03.cpp
new file mode 100644
index 0000000..07e7c9e
--- /dev/null
+++ b/Year 1/Object-oriented Programming/Solutions Sem. 02/exercise03.cpp
@@ -0,0 +1,167 @@
+#include <iostream> // cin, cout
+#include <fstream> // ifstream, ofstream
+
+const int LINE_BUFFER_SIZE = 1024;
+const int MAX_STUDENT_PROP_SIZE = 2048;
+
+bool strNotEqual(const char* a, const char* b) {
+ for (int i = 0; a[i] != '\0' || b[i] != '\0'; i++)
+ if (a[i] != b[i]) return true;
+ return false;
+}
+
+/* Student */
+
+struct Student {
+ char FirstName[MAX_STUDENT_PROP_SIZE];
+ char LastName[MAX_STUDENT_PROP_SIZE];
+ char Email[MAX_STUDENT_PROP_SIZE];
+ char FN[MAX_STUDENT_PROP_SIZE];
+};
+
+void printStudent(Student st) {
+ std::cout << "Name = " << st.FirstName << " " << st.LastName << ", Email: " << st.Email << ", FN: " << st.FN << std::endl;
+}
+
+Student parseStudent(const char* str) {
+ Student toRet = {};
+ char* prop = toRet.FirstName;
+
+ for (int i = 0, j = 0; str[i] != '\0'; i++) {
+ if (str[i] == ',') {
+ if (prop == toRet.FirstName) prop = toRet.LastName;
+ else if (prop == toRet.LastName) prop = toRet.Email;
+ else if (prop == toRet.Email) prop = toRet.FN;
+ j = 0;
+ }
+ else prop[j++] = str[i];
+ }
+ return toRet;
+}
+
+/* StudentsList */
+
+struct StudentsList {
+ Student* students;
+ int count;
+ int length;
+};
+
+bool resizeStudentsList(StudentsList& sl, int newSize) {
+ if (newSize < sl.count) return false; // We cant fit all of the old students in the new students array
+
+ Student* temp = new Student[newSize];
+ for (int i = 0; i < sl.count; i++)
+ temp[i] = sl.students[i];
+
+ delete[] sl.students;
+ sl.students = temp;
+
+ return true;
+}
+
+void addStudents(StudentsList& sl, Student st) {
+ if (sl.count == sl.length) {
+ sl.length = (!sl.length) ? 2 : sl.length << 1;
+ resizeStudentsList(sl, sl.length);
+ }
+
+ sl.students[sl.count++] = st;
+}
+
+int findStudentIndByFN(const StudentsList& sl, const char* FN) {
+ for (int i = 0; i < sl.count; i++)
+ if (strNotEqual(sl.students[i].FN, FN)) return i;
+
+ std::cout << "Error: No student with FN of " << FN << "!" << std::endl;
+ return -1;
+}
+
+StudentsList getStudentsListFromFile(const char* fileName) {
+ std::ifstream inFile(fileName);
+ if (!inFile.is_open()) {
+ std::cout << "Error: Could not open file!" << std::endl;
+ return {};
+ }
+
+ StudentsList sl = {};
+
+ char buffer[LINE_BUFFER_SIZE];
+ while (!inFile.eof()) {
+ inFile.getline(buffer, LINE_BUFFER_SIZE);
+ if (buffer[0] != '\0')
+ addStudents(sl, parseStudent(buffer));
+ }
+ return sl;
+}
+
+/* Commands */
+
+bool isCommand(const char* inputBuffer, const char* command) { // The buffer could get dirty with leftover data, so we check only if the beginning matches
+ for (int i = 0; command[i] != '\0'; i++)
+ if (inputBuffer[i] != command[i]) return false;
+
+ return true;
+}
+
+void printStudentByFN(const StudentsList& sl, const char* FN) {
+ int stInd = findStudentIndByFN(sl, FN);
+ if (stInd >= 0) printStudent(sl.students[stInd]);
+}
+
+void replaceStudentEmail(const StudentsList& sl, const char* FN, const char* newEmail) {
+ int stdInd = findStudentIndByFN(sl, FN);
+ if (stdInd < 0) return;
+
+ Student& st = sl.students[stdInd];
+ for (int i = 0; st.Email[i] != '\0' || newEmail[i] != '\0'; i++)
+ st.Email[i] = (newEmail[i] == '\0') ? '\0' : newEmail[i];
+}
+
+void saveToFile(const StudentsList& sl, const char* newFileName) {
+ std::ofstream newFile(newFileName);
+ if (!newFile.is_open()) {
+ std::cout << "Error: File could not be opened!" << std::endl;
+ return;
+ }
+
+ for (int i = 0; i < sl.count; i++) {
+ Student st = sl.students[i];
+ newFile << st.FirstName << "," << st.LastName << "," << st.Email << "," << st.FN << std::endl;
+ }
+
+ newFile.close();
+}
+
+/* Main */
+
+int main() {
+ char inputBuffer[LINE_BUFFER_SIZE];
+ std::cout << "Open file:" << std::endl << ">";
+ std::cin >> inputBuffer;
+
+ StudentsList readSL = getStudentsListFromFile(inputBuffer);
+ if (readSL.students == nullptr)
+ return -1;
+
+ do {
+ std::cout << ">";
+ std::cin >> inputBuffer;
+
+ if (isCommand(inputBuffer, "print")) {
+ std::cin >> inputBuffer;
+ printStudentByFN(readSL, inputBuffer);
+ }
+ else if (isCommand(inputBuffer, "edit")) {
+ char tempFNBuffer[LINE_BUFFER_SIZE];
+ std::cin >> tempFNBuffer >> inputBuffer;
+ replaceStudentEmail(readSL, tempFNBuffer, inputBuffer);
+ }
+ else if (isCommand(inputBuffer, "save")) {
+ std::cin >> inputBuffer;
+ saveToFile(readSL, inputBuffer);
+ }
+ } while (!isCommand(inputBuffer, "end"));
+
+ delete[] readSL.students;
+}
diff --git a/Year 1/Object-oriented Programming/Solutions Sem. 02/list.csv b/Year 1/Object-oriented Programming/Solutions Sem. 02/list.csv
new file mode 100644
index 0000000..5f0d066
--- /dev/null
+++ b/Year 1/Object-oriented Programming/Solutions Sem. 02/list.csv
@@ -0,0 +1,10 @@
+Gosho,Ivanov,givanov@gmail.com,3MI0600069
+Petio,Draganov,pen15@abv.bg,0MI1300123
+Georgi,Nedqlkov,gone@mail.eu,9MI0303002
+Yorgus,Yorgus,yorgus@bg.com,1MI0100001
+Konstantin,Abrahildraganovichev,ivo@gmail.com,0MI0000000
+Tester,Testov,test@test.com,7MI1234000
+Simeon,Milchev,nomiko@nomiko.co.uk,2MI0200202
+Momchilcho,Mishov,cheese@uni-sofia.bg,5MI0800033
+Petrokan,Mitov,pemi69@abv.bg,4MI0660103
+Prakash,Prakash,ivo@it-kariera.bg,0MI0100420